reqwest 的性能为啥这么差?

2022-11-09 08:45:18 +08:00
 ayang23

1. reqwest

#[macro_use]
extern crate timeit;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    timeit!({
        let _ = reqwest::blocking::get("http://localhost/")?
            .text()?;
    });
    Ok(())
}

2. chttp

use chttp::prelude::*;

#[macro_use]
extern crate timeit;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    timeit!({
        let _ = chttp::get("http://localhost/")?
            .text()?;
    });
    Ok(())
}

3. python requests

import requests
%timeit requests.get('http://localhost/').text

输出:

  1. reqwest 10 loops: 40.283526 ms

  2. chttp 1000 loops: 264.017153 µs

  3. requests 1.56 ms ± 27 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

reqwest 换成 http1.0 协议也不变

localhost 是本地 nginx

这性能比 python requsts 都慢了几十倍,有点夸张

2896 次点击
所在节点    Rust
10 条回复
yuhangch
2022-11-09 08:49:37 +08:00
因为 blocking ?
ayang23
2022-11-09 08:52:16 +08:00
@yuhangch 按说都是从本地 nginx 返回的数据,速度很快的,我没测 async 的性能。如果是因为 blocking, 那可能就是 blocking 的设计有问题了
ayang23
2022-11-09 08:53:04 +08:00
@ayang23 我再测一下 async
ayang23
2022-11-09 09:12:41 +08:00
测了一下,结果和 blocking 一样
@yuhangch
RRyo
2022-11-09 09:14:22 +08:00
对比下 ureq?
kuviki
2022-11-09 09:14:30 +08:00
可能初始化配置比较耗时?可以试下先在 timeit 外 Client::new() 再请求
ayang23
2022-11-09 09:19:03 +08:00
@RRyo ureq 543.6769919999999 µs 也很快
ayang23
2022-11-09 09:22:15 +08:00
@kuviki 你说的是对的
移出去后只有 635.9667900000001 µs

原来是这样啊
DTCPSS
2022-11-09 11:24:15 +08:00
看文档,每次调用`reqwest::get()` 都会创建一个 Client ,这个方法只是为了一次性调用方便。要多次调用最好显式创建一个 Client 然后复用。
ayang23
2022-11-09 21:20:36 +08:00
@DTCPSS 对,这个 Client 比较重

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/893742

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX