最快的 Rust web 框架

隨着 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” 的基準測試。在更復雜的場景中,獲勝的優勢可能沒有那麼大。

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/8F_dUx5W5fnx2f_kFY8YmQ