< 返回版块

yuequan1997 发表于 2019-09-02 21:55

Tags:rust

Rocket和Actix-Web的异步性能测试

Rust目前最火的两个web框架就是Actix-Web和Rocket, 众所周知,Rocket的优势在于易用性,Actix-web在于性能,最近,Rocket的人员正在迁移到异步后端。因此,作者想看看异步分支和主分支如何的性能如何同时和Actix-Web进行比较是很有趣的。

测试使用的项目

用Rocket编写的hello world应用程序

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

#[get("/")]
fn index() -> String {
    "Hello, world!".to_string()
}

fn main() {
    rocket::ignite().mount("/", routes![index]).launch();
}

Cargo.toml的差异(同步和异步)

[dependencies]
rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async" }

同步

[dependencies]
rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "master" }

下面的程序用于测试Actix-Web

use actix_web::{web, App, HttpServer, Responder};

fn index() -> impl Responder {
    "Hello, World".to_string()
}

fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(web::resource("/").to(index)))
        .bind("127.0.0.1:8000")?
        .run()
}

我还引入了Wrap

use warp::{self, path, Filter};

fn main() {
    let hello = path!("hello")
        .map(|| "Hello, world!");

    warp::serve(hello)
        .run(([127, 0, 0, 1], 8000));
}

结果

我运行了二个应用程序使用了 cargo run --release 并用了wrk -t20 -c1000 -d30s http://localhost:8000

Rocket 同步

Running 30s test @ http://localhost:8000
  20 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     7.14ms   61.41ms   1.66s    97.97%
    Req/Sec     5.15k     1.45k   14.87k    74.03%
  3076813 requests in 30.10s, 428.40MB read
Requests/sec: 102230.30
Transfer/sec:     14.23MB

Rocket 异步

Running 30s test @ http://localhost:8000
  20 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     4.34ms    3.06ms 211.14ms   79.00%
    Req/Sec    11.15k     1.81k   34.11k    79.08%
  6669116 requests in 30.10s, 0.91GB read
Requests/sec: 221568.27
Transfer/sec:     31.06MB

Actix-Web

Running 30s test @ http://localhost:8000
  20 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     3.82ms    5.58ms 249.57ms   86.55%
    Req/Sec    24.09k     5.27k   69.99k    72.52%
  14385279 requests in 30.10s, 1.71GB read
Requests/sec: 477955.05
Transfer/sec:     58.34MB

Warp

  20 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     4.23ms    8.50ms 428.96ms   93.33%
    Req/Sec    20.38k     6.09k   76.63k    74.57%
  12156483 requests in 30.10s, 1.47GB read
Requests/sec: 403896.10
Transfer/sec:     50.07MB

结论

虽然async Rocket的性能仍然不如Actix-Web,但是async极大地提高了它的性能。

我非常希望看到Rocket的性能提高到这样的程度:作为一名开发人员,从此就不需要在易用性和性能之间做出选择。

rustversion - 根据rustc编译器版本进行条件编译

这个类库提供了宏的扩展,能够根据rustc编译器版本进行条件编译的宏。

使用案例

#[rustversion::since(1.33)]
use std::pin::Pin;

#[rustversion::since(1.33)]
impl<P: MyTrait> MyTrait for Pin<P> {
    /* ... */
}


#[rustversion::attr(before(1.33), repr(packed))]
#[rustversion::attr(since(1.33), repr(packed(2)))]
struct Six(i16, i32);

fn main() {
    println!("{}", std::mem::align_of::<Six>());
}

更多信息可以前往GitHub了解一哈

mobi-rs - 用于解析和操作.mobi格式的Rust库

一个用rust编写的从.mobi格式电子书中提取数据的库

使用案例

访问基本信息

use mobi::Mobi;
fn main() {
    let m = Mobi::init(Path::new("/home/wojtek/Downloads/lotr.mobi")).unwrap();
    let title = m.title().unwrap();
    let author = m.author().unwrap();
    let publisher = m.publisher().unwrap();
    let desc = m.description().unwrap();
    let isbn = m.isbn().unwrap();
    let pub_date = m.publish_date().unwrap();
    let contributor = m.contributor().unwrap();
    println!("{}\n{}\n{}\n{}\n{}\n{}\n{}\n", title, author, publisher, isbn, pub_date, desc, contributor);
    // Access Headers
    let header = m.header; // Normal Header
    let pdheader = m.palmdoc; // PalmDOC Header
    let mheader = m.mobi; // MOBI Header
    let exth = m.exth // Extra Header
}

输出

The Fellowship of the Ring
J. R. R. Tolkien
Houghton Mifflin
9780618574940
2005-07-15T07:00:00+00:00
SUMMARY: For over fifty years, J.R.R.............
calibre (0.7.23) [http://calibre-ebook.com]

更多信息可以前往GitHub了解一哈

persy - 用Rust编写的简单事务性存储引擎

Persy是一个用rust编写的事务性存储引擎。

示例

use persy::{Persy,Config};
//...
Persy::create("./open.persy")?;
let persy = Persy::open("./open.persy",Config::new())?;
let mut tx = persy.begin()?;
persy.create_segment(&mut tx, "seg")?;
let data = vec![1;20];
persy.insert_record(&mut tx, "seg", &data)?;
let prepared = persy.prepare_commit(tx)?;
persy.commit(prepared)?;
for (_id, content) in persy.scan("seg")? {
    assert_eq!(content[0], 20);
    //....
}

更多信息可以前往GitLab了解一哈


From 日报小组 月泉

日报订阅地址:

独立日报订阅地址:

社区学习交流平台订阅:

评论区

写评论
Mike Tang 2019-10-23 09:24

warp也是一个异步web框架

orangeagain 2019-10-23 02:44

哇,cool 小白有几个问题,就是warp是什么意思? actixweb是同步还是异步?

1 共 2 条评论, 1 页