use criterion::{black_box, criterion_group, criterion_main, Criterion, BenchmarkId};
use reqres::{Client, CookieJar};
use std::time::Duration;
fn benchmark_get_request(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
c.bench_function("get_request", |b| {
b.to_async(&rt).iter(|| async {
let client = Client::builder()
.http1_only()
.build()
.unwrap();
let _ = client.get("http://httpbin.org/get").await;
})
});
}
fn benchmark_https_request(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
c.bench_function("https_request", |b| {
b.to_async(&rt).iter(|| async {
let client = Client::builder()
.http1_only()
.build()
.unwrap();
let _ = client.get("https://httpbin.org/get").await;
})
});
}
fn benchmark_http2_request(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
c.bench_function("http2_request", |b| {
b.to_async(&rt).iter(|| async {
let client = Client::new().unwrap();
let _ = client.get("https://httpbin.org/get").await;
})
});
}
fn benchmark_connection_pooling(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let iterations = 10;
c.bench_with_input(
BenchmarkId::new("pooling", iterations),
&iterations,
|b, &iterations| {
b.to_async(&rt).iter(|| async {
let client = Client::builder()
.enable_pooling()
.http1_only()
.build()
.unwrap();
for _ in 0..iterations {
let _ = client.get("https://httpbin.org/get").await;
}
})
},
);
c.bench_with_input(
BenchmarkId::new("no_pooling", iterations),
&iterations,
|b, &iterations| {
b.to_async(&rt).iter(|| async {
let client = Client::builder()
.disable_pooling()
.http1_only()
.build()
.unwrap();
for _ in 0..iterations {
let _ = client.get("https://httpbin.org/get").await;
}
})
},
);
}
fn benchmark_cookie_operations(c: &mut Criterion) {
let mut group = c.benchmark_group("cookie");
group.bench_function("insert", |b| {
b.iter(|| {
let mut jar = CookieJar::new();
jar.insert(black_box("test".to_string()), black_box("value".to_string()));
})
});
group.bench_function("get", |b| {
let mut jar = CookieJar::new();
jar.insert("test".to_string(), "value".to_string());
b.iter(|| {
let _ = jar.get(black_box("test"));
})
});
group.bench_function("build_header", |b| {
let mut jar = CookieJar::new();
for i in 0..10 {
jar.insert(format!("cookie_{}", i), format!("value_{}", i));
}
b.iter(|| {
let _ = jar.build_cookie_header();
})
});
group.finish();
}
fn benchmark_compression(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
c.bench_function("compression_enabled", |b| {
b.to_async(&rt).iter(|| async {
let client = Client::builder()
.enable_compression()
.http1_only()
.build()
.unwrap();
let _ = client.get("https://httpbin.org/gzip").await;
})
});
}
fn benchmark_timeout(c: &mut Criterion) {
let mut group = c.benchmark_group("timeout");
group.bench_function("10s", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
b.to_async(&rt).iter(|| async {
let client = Client::builder()
.timeout(Duration::from_secs(10))
.http1_only()
.build()
.unwrap();
let _ = client.get("https://httpbin.org/get").await;
})
});
group.bench_function("30s", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
b.to_async(&rt).iter(|| async {
let client = Client::builder()
.timeout(Duration::from_secs(30))
.http1_only()
.build()
.unwrap();
let _ = client.get("https://httpbin.org/get").await;
})
});
group.finish();
}
fn benchmark_client_creation(c: &mut Criterion) {
let mut group = c.benchmark_group("client_creation");
group.bench_function("default", |b| {
b.iter(|| {
let _ = Client::new().unwrap();
})
});
group.bench_function("builder", |b| {
b.iter(|| {
let _ = Client::builder()
.http2_prior_knowledge()
.enable_compression()
.enable_pooling()
.build()
.unwrap();
})
});
group.finish();
}
criterion_group!(
benches,
benchmark_get_request,
benchmark_https_request,
benchmark_http2_request,
benchmark_connection_pooling,
benchmark_cookie_operations,
benchmark_compression,
benchmark_timeout,
benchmark_client_creation
);
criterion_main!(benches);