Skip to content

Instantly share code, notes, and snippets.

@kingluo
Created May 17, 2021 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingluo/8ccd88b53e9d2878391dbb91ad1f4751 to your computer and use it in GitHub Desktop.
Save kingluo/8ccd88b53e9d2878391dbb91ad1f4751 to your computer and use it in GitHub Desktop.
benchmark warp and routerify-lite when they need to match 50 urls
use hyper::{Body, Request, Response, Server};
use routerify_lite::{RequestExt, Router, RouterService};
use std::{convert::Infallible, net::SocketAddr};
async fn home_handler(_: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new(Body::from("Home page")))
}
async fn hello_handler(req: Request<Body>) -> Result<Response<Body>, Infallible> {
let user = req.param("name").unwrap();
Ok(Response::new(Body::from(format!("Hello {}", user))))
}
fn router() -> Router<Body, Infallible> {
Router::new()
.get("/14532", home_handler)
.get("/2641", home_handler)
.get("/24128", home_handler)
.get("/21974", home_handler)
.get("/21771", home_handler)
.get("/17909", home_handler)
.get("/1778", home_handler)
.get("/11175", home_handler)
.get("/29658", home_handler)
.get("/2499", home_handler)
.get("/32295", home_handler)
.get("/17474", home_handler)
.get("/27978", home_handler)
.get("/7040", home_handler)
.get("/1745", home_handler)
.get("/6699", home_handler)
.get("/5203", home_handler)
.get("/30461", home_handler)
.get("/1070", home_handler)
.get("/32392", home_handler)
.get("/15607", home_handler)
.get("/15214", home_handler)
.get("/29681", home_handler)
.get("/27266", home_handler)
.get("/13039", home_handler)
.get("/30515", home_handler)
.get("/16470", home_handler)
.get("/24890", home_handler)
.get("/15952", home_handler)
.get("/937", home_handler)
.get("/31181", home_handler)
.get("/5052", home_handler)
.get("/15482", home_handler)
.get("/5655", home_handler)
.get("/30798", home_handler)
.get("/23670", home_handler)
.get("/529", home_handler)
.get("/17640", home_handler)
.get("/32572", home_handler)
.get("/21929", home_handler)
.get("/26275", home_handler)
.get("/4053", home_handler)
.get("/30860", home_handler)
.get("/25513", home_handler)
.get("/6361", home_handler)
.get("/25521", home_handler)
.get("/14841", home_handler)
.get("/12506", home_handler)
.get("/21137", home_handler)
.get("/14334", home_handler)
.get("/hello/:name", hello_handler)
.build()
}
#[tokio::main]
async fn main() {
let router = router();
// Create a Service from the router above to handle incoming requests.
let service = RouterService::new(router);
// The address on which the server will be listening.
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
// Create a server by passing the created service to `.serve` method.
let server = Server::bind(&addr).serve(service);
println!("App is running on: {}", addr);
if let Err(err) = server.await {
eprintln!("Server error: {}", err);
}
}
#![deny(warnings)]
#![recursion_limit = "256"]
use warp::Filter;
#[tokio::main]
async fn main() {
let route_14532 = warp::path("14532").map(|| "Hello, World");
let route_2641 = warp::path("2641").map(|| "Hello, World");
let route_24128 = warp::path("24128").map(|| "Hello, World");
let route_21974 = warp::path("21974").map(|| "Hello, World");
let route_21771 = warp::path("21771").map(|| "Hello, World");
let route_17909 = warp::path("17909").map(|| "Hello, World");
let route_1778 = warp::path("1778").map(|| "Hello, World");
let route_11175 = warp::path("11175").map(|| "Hello, World");
let route_29658 = warp::path("29658").map(|| "Hello, World");
let route_2499 = warp::path("2499").map(|| "Hello, World");
let route_32295 = warp::path("32295").map(|| "Hello, World");
let route_17474 = warp::path("17474").map(|| "Hello, World");
let route_27978 = warp::path("27978").map(|| "Hello, World");
let route_7040 = warp::path("7040").map(|| "Hello, World");
let route_1745 = warp::path("1745").map(|| "Hello, World");
let route_6699 = warp::path("6699").map(|| "Hello, World");
let route_5203 = warp::path("5203").map(|| "Hello, World");
let route_30461 = warp::path("30461").map(|| "Hello, World");
let route_1070 = warp::path("1070").map(|| "Hello, World");
let route_32392 = warp::path("32392").map(|| "Hello, World");
let route_15607 = warp::path("15607").map(|| "Hello, World");
let route_15214 = warp::path("15214").map(|| "Hello, World");
let route_29681 = warp::path("29681").map(|| "Hello, World");
let route_27266 = warp::path("27266").map(|| "Hello, World");
let route_13039 = warp::path("13039").map(|| "Hello, World");
let route_30515 = warp::path("30515").map(|| "Hello, World");
let route_16470 = warp::path("16470").map(|| "Hello, World");
let route_24890 = warp::path("24890").map(|| "Hello, World");
let route_15952 = warp::path("15952").map(|| "Hello, World");
let route_937 = warp::path("937").map(|| "Hello, World");
let route_31181 = warp::path("31181").map(|| "Hello, World");
let route_5052 = warp::path("5052").map(|| "Hello, World");
let route_15482 = warp::path("15482").map(|| "Hello, World");
let route_5655 = warp::path("5655").map(|| "Hello, World");
let route_30798 = warp::path("30798").map(|| "Hello, World");
let route_23670 = warp::path("23670").map(|| "Hello, World");
let route_529 = warp::path("529").map(|| "Hello, World");
let route_17640 = warp::path("17640").map(|| "Hello, World");
let route_32572 = warp::path("32572").map(|| "Hello, World");
let route_21929 = warp::path("21929").map(|| "Hello, World");
let route_26275 = warp::path("26275").map(|| "Hello, World");
let route_4053 = warp::path("4053").map(|| "Hello, World");
let route_30860 = warp::path("30860").map(|| "Hello, World");
let route_25513 = warp::path("25513").map(|| "Hello, World");
let route_6361 = warp::path("6361").map(|| "Hello, World");
let route_25521 = warp::path("25521").map(|| "Hello, World");
let route_14841 = warp::path("14841").map(|| "Hello, World");
let route_12506 = warp::path("12506").map(|| "Hello, World");
let route_21137 = warp::path("21137").map(|| "Hello, World");
let route_14334 = warp::path("14334").map(|| "Hello, World");
let routes = (route_14532)
.or(route_2641)
.or(route_24128)
.or(route_21974)
.or(route_21771)
.or(route_17909)
.or(route_1778)
.or(route_11175)
.or(route_29658)
.or(route_2499)
.or(route_32295)
.or(route_17474)
.or(route_27978)
.or(route_7040)
.or(route_1745)
.or(route_6699)
.or(route_5203)
.or(route_30461)
.or(route_1070)
.or(route_32392)
.or(route_15607)
.or(route_15214)
.or(route_29681)
.or(route_27266)
.or(route_13039)
.or(route_30515)
.or(route_16470)
.or(route_24890)
.or(route_15952)
.or(route_937)
.or(route_31181)
.or(route_5052)
.or(route_15482)
.or(route_5655)
.or(route_30798)
.or(route_23670)
.or(route_529)
.or(route_17640)
.or(route_32572)
.or(route_21929)
.or(route_26275)
.or(route_4053)
.or(route_30860)
.or(route_25513)
.or(route_6361)
.or(route_25521)
.or(route_14841)
.or(route_12506)
.or(route_21137)
.or(route_14334);
let hello = warp::path!("hello" / String).map(|name| format!("Hello {}", name));
let routes = routes.or(hello);
warp::serve(routes).run(([0, 0, 0, 0], 8080)).await;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment