• 主流Rust web框架的性能大比较
  • 发布于 2个月前
  • 117 热度
    0 评论
随着Rust越来越受欢迎,选择正确的web框架变得至关重要。我们将使用“Hello World”基准测试来比较Actix、Axum、Rocket、Tide、Gotham、Nickel、Ntex和Poem这些Rust web框架的性能。

测试方法
为了确保公平和标准化的比较,我们对上述每个web框架进行了基本的“Hello World”基准测试。这涉及到创建一个最小的web服务器,它用“Hello, World!”消息发送给传入的HTTP请求。基准测试是在同一台机器上进行的,每个框架都对其速度,资源使用和易于实现进行了评估。负载测试用的是apache bench,我们测试的是release构建版本。

各框架的代码如下:
Actix
[dependencies]
actix-web = "4"
use actix_web::{get, App, HttpResponse, HttpServer, Responder};

#[get("/")]
async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hello world!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .service(hello)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}
Axum
[dependencies]
axum = "0.7.3"
tokio = { version = "1.0", features = ["full"] }
use axum::{response::Html, routing::get, Router};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(handler));

    let listener = tokio::net::TcpListener::bind("127.0.0.1:8080")
        .await
        .unwrap();
    println!("listening on {}", listener.local_addr().unwrap());
    axum::serve(listener, app).await.unwrap();
}

async fn handler() -> Html<&'static str> {
    Html("Hello world!")
}
Rocket
[dependencies]
rocket = "0.5.0"
#[macro_use] extern crate rocket;

#[get("/")]
fn hello() -> String {
    format!("Hello world!")
}

#[launch]
fn rocket() -> _ {

    let config = rocket::Config {
        port: 8080,
        log_level: rocket::config::LogLevel::Off,
        ..rocket::Config::debug_default()
    };

    rocket::custom(&config)
        .mount("/", routes![hello])
}
Tide
[dependencies]
tide = "0.16.0"
async-std = { version = "1.8.0", features = ["attributes"] }
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {

    let mut app = tide::new();

    app.at("/").get(|_| async { Ok("Hello world!") });
    app.listen("127.0.0.1:8080").await?;

    Ok(())
}
Gotham
[dependencies]
gotham = "0.7.2"
use gotham::state::State;

pub fn say_hello(state: State) -> (State, &'static str) {
    (state, "Hello world!")
}
// 启动一个服务器,并为我们收到的每个“请求”调用上面定义的“处理程序”。
pub fn main() {

    gotham::start("127.0.0.1:8080", || Ok(say_hello)).unwrap()

}
Ntex
[dependencies]
ntex = { version= "0.7.16", features = ["tokio"] }
use ntex::web;

#[web::get("/")]
async fn index() -> impl web::Responder {
    "Hello, World!"
}

#[ntex::main]
async fn main() -> std::io::Result<()> {

    web::HttpServer::new(|| 
        web::App::new()
        .service(index)
    )
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}
POEM
[dependencies]
poem = "1.3.59"
tokio = { features = ["rt-multi-thread", "macros"] }
use poem::{
    get, handler, listener::TcpListener, middleware::Tracing, EndpointExt, Route, Server,
};

#[handler]
fn hello() -> String {
    format!("Hello world!")
}

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {

    let app = Route::new().at("/", get(hello)).with(Tracing);
    Server::new(TcpListener::bind("0.0.0.0:8080"))
        .name("hello-world")
        .run(app)
        .await
}
测试结果
对于50、100和150个并发,每个测试总共执行了1000000个请求。结果如下表所示:
50并发

100并发

150并发

从结果看,Tide是最慢的(只能在12秒内完成1M请求,平均为159K Req /秒),Axum是最快的(可以在6秒内完成1M请求),所有Web框架的资源使用情况几乎相同。

注意:这只是基于什么都不做的“hello world”的基准测试。在更复杂的场景中,获胜的优势可能没有那么大。
用户评论