Rust web框架:Actix vs Axum性能对比

picture.image

在这篇文章中,我们将对比两个流行的Rust框架——Actix和Axum在简单的“Hello World”案例中的性能。Actix是一个广为人知的流行框架。相比之下,Axum则相对不那么流行,优势是axum是tokio官方出品,目前还在快速迭代中,注意:axum尚未发布正式版。

测试步骤

机器配置: MacBook Pro M2 with 16G of RAM.

软件版本:

  • Rust 1.73.0
  • Actix web 4.4.0
  • Axum 0.6.20

两个框架的“Hello World”HTTP服务器代码如下:

Actix

  
use actix_web::{get, App, HttpServer, Responder};  
  
#[get("/")]  
async fn index() -> impl Responder {  
 "Hello World!"  
}  
  
#[actix_web::main]  
async fn main() -> std::io::Result<()> {  
 HttpServer::new(|| App::new().service(index))  
 .bind(("127.0.0.1", 3000))?  
 .run()  
 .await  
}

Axum

  
use axum::{  
 routing::get,  
 Router,  
};  
  
#[tokio::main]  
async fn main() {  
 let app = Router::new().route("/", get(|| async { "Hello World!" }));  
  
 axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())  
 .serve(app.into_make_service())  
 .await  
 .unwrap();  
}
两个框架的测试都基于release模式
测试结果

所有测试均以5000万请求为基础,使用50、100和300个并发连接进行执行。以下是图表和表格形式的结果:

耗时(s)

picture.image

每秒请求数量

picture.image

最低延迟(ms)

picture.image

最大延迟(ms)

picture.image

平均内存使用(MB)

picture.image

平均cpu使用量(%)

picture.image

小结

这两个框架在基础测试用例中性能差距不大,可能在复杂场景中如数据库、文件服务器等操作下差异才会变得明显。萝卜白菜各有所爱,开发者个人使用可以两个都体验下。生产级使用目前还是以actix-web为主。目前我个人感觉是这俩伯仲之间,差异不大。

0
0
0
0
评论
未登录
暂无评论