2021-05-15 02:13:33 +02:00
|
|
|
use actix_web::{web, App, HttpResponse};
|
2020-01-25 00:05:25 +01:00
|
|
|
use awc::Client;
|
|
|
|
use criterion::{criterion_group, criterion_main, Criterion};
|
2020-05-18 04:47:20 +02:00
|
|
|
use futures_util::future::join_all;
|
2020-01-25 00:05:25 +01:00
|
|
|
|
|
|
|
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World";
|
|
|
|
|
|
|
|
// benchmark sending all requests at the same time
|
|
|
|
fn bench_async_burst(c: &mut Criterion) {
|
2020-07-10 23:35:22 +02:00
|
|
|
// We are using System here, since Runtime requires preinitialized tokio
|
|
|
|
// Maybe add to actix_rt docs
|
2021-02-07 02:00:40 +01:00
|
|
|
let rt = actix_rt::System::new();
|
2020-07-10 23:35:22 +02:00
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
let srv = rt.block_on(async {
|
2021-04-02 09:26:59 +02:00
|
|
|
actix_test::start(|| {
|
2021-02-12 00:03:17 +01:00
|
|
|
App::new()
|
|
|
|
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
2021-01-04 00:47:04 +01:00
|
|
|
})
|
2020-01-25 00:05:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
let url = srv.url("/");
|
|
|
|
|
|
|
|
c.bench_function("get_body_async_burst", move |b| {
|
|
|
|
b.iter_custom(|iters| {
|
2021-02-12 22:52:58 +01:00
|
|
|
rt.block_on(async {
|
|
|
|
let client = Client::new().get(url.clone()).freeze().unwrap();
|
|
|
|
|
|
|
|
let start = std::time::Instant::now();
|
|
|
|
// benchmark body
|
2020-01-25 00:05:25 +01:00
|
|
|
|
|
|
|
let burst = (0..iters).map(|_| client.send());
|
2021-02-12 22:52:58 +01:00
|
|
|
let resps = join_all(burst).await;
|
|
|
|
|
|
|
|
let elapsed = start.elapsed();
|
2020-01-25 00:05:25 +01:00
|
|
|
|
2021-02-12 22:52:58 +01:00
|
|
|
// if there are failed requests that might be an issue
|
|
|
|
let failed = resps.iter().filter(|r| r.is_err()).count();
|
|
|
|
if failed > 0 {
|
|
|
|
eprintln!("failed {} requests (might be bench timeout)", failed);
|
|
|
|
};
|
2020-01-25 00:05:25 +01:00
|
|
|
|
2021-02-12 22:52:58 +01:00
|
|
|
elapsed
|
|
|
|
})
|
2020-01-25 00:05:25 +01:00
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(server_benches, bench_async_burst);
|
|
|
|
criterion_main!(server_benches);
|