use reqres::Client;
use std::time::Instant;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== reqres v0.4.0 Connection Pool Demo ===\n");
println!("\nTest 1: Pooling with HTTP/1.1\n");
let client_h1 = Client::builder()
.enable_pooling()
.http1_only() .build()?;
let url = "https://httpbin.org/get";
let iterations = 10;
let start = Instant::now();
for i in 1..=iterations {
println!(" Request {}...", i);
match client_h1.get(url).await {
Ok(response) => {
println!(" ✓ Status: {}", response.status());
}
Err(e) => {
eprintln!(" ✗ Request {} failed: {}", i, e);
}
}
}
let elapsed = start.elapsed();
println!(" Total time: {:.2}s", elapsed.as_secs_f64());
if let Some(stats) = client_h1.pool_stats().await {
println!(" Pool Hit Rate: {:.2}%", stats.total_hits as f64 / (stats.total_hits + stats.total_misses) as f64 * 100.0);
println!(" Total hits: {}, Total misses: {}", stats.total_hits, stats.total_misses);
}
println!("\n{}", "=".repeat(50));
println!("Test 2: Without pooling (new connection each request)\n");
let client_no_pool = Client::builder()
.disable_pooling() .http1_only()
.build()?;
let start2 = Instant::now();
for i in 1..=iterations {
println!(" Request {}...", i);
match client_no_pool.get(url).await {
Ok(response) => {
println!(" ✓ Status: {}", response.status());
}
Err(e) => {
eprintln!(" ✗ Request {} failed: {}", i, e);
}
}
}
let elapsed2 = start2.elapsed();
println!(" Total time: {:.2}s", elapsed2.as_secs_f64());
println!("\n{}", "=".repeat(50));
println!("Test 3: HTTP/2 (no pooling as HTTP/2 already supports multiplexing)\n");
let client_h2 = Client::new()?;
let start3 = Instant::now();
for i in 1..=5 {
println!(" Request {}...", i);
match client_h2.get(url).await {
Ok(response) => {
println!(" ✓ Status: {} ({})", response.status(), response.version);
}
Err(e) => {
eprintln!(" ✗ Request {} failed: {}", i, e);
}
}
}
let elapsed3 = start3.elapsed();
println!(" Total time: {:.2}s", elapsed3.as_secs_f64());
println!("\n{}", "=".repeat(50));
println!("Summary:");
println!(" Test 1 (HTTP/1.1 + Pooling): {:.2}s ({:.2}ms per request)",
elapsed.as_secs_f64(), elapsed.as_millis() as f64 / iterations as f64);
println!(" Test 2 (HTTP/1.1 + No Pooling): {:.2}s ({:.2}ms per request)",
elapsed2.as_secs_f64(), elapsed2.as_millis() as f64 / iterations as f64);
println!(" Test 3 (HTTP/2 + No Pooling): {:.2}s ({:.2}ms per request)",
elapsed3.as_secs_f64(), elapsed3.as_millis() as f64 / 5.0);
println!("\n=== Demo completed ===");
Ok(())
}