#[macro_use] extern crate timeit; fn main() -> Result<(), Box<dyn std::error::Error>> { timeit!({ let _ = reqwest::blocking::get("http://localhost/")? .text()?; }); Ok(()) }
use chttp::prelude::*; #[macro_use] extern crate timeit; fn main() -> Result<(), Box<dyn std::error::Error>> { timeit!({ let _ = chttp::get("http://localhost/")? .text()?; }); Ok(()) }
import requests %timeit requests.get('http://localhost/').text
输出:
reqwest 10 loops: 40.283526 ms
chttp 1000 loops: 264.017153 s
requests 1.56 ms ± 27 s per loop (mean ± std. dev. of 7 runs, 1000 loops each)
reqwest 换成 http1.0 协议也不变
localhost 是本地 nginx
这性能比 python requsts 都慢了几十倍,有点夸张
![]() | 1 yuhangch 2022-11-09 08:49:37 +08:00 因为 blocking ? |
![]() | 2 ayang23 OP @yuhangch 按说都是从本地 nginx 返回的数据,速度很快的,我没测 async 的性能。如果是因为 blocking, 那可能就是 blocking 的设计有问题了 |
![]() | 5 RRyo 2022-11-09 09:14:22 +08:00 对比下 ureq? |
![]() | 6 kuviki 2022-11-09 09:14:30 +08:00 ![]() 可能初始化配置比较耗时?可以试下先在 timeit 外 Client::new() 再请求 |
![]() | 9 DTCPSS 2022-11-09 11:24:15 +08:00 ![]() 看文档,每次调用`reqwest::get()` 都会创建一个 Client ,这个方法只是为了一次性调用方便。要多次调用最好显式创建一个 Client 然后复用。 |