reqres 1.0.0

A pure Rust async HTTP client library based on Tokio with HTTP/2, connection pooling, proxy, cookie, compression, benchmarks, and comprehensive tests
Documentation
use criterion::{black_box, criterion_group, criterion_main, Criterion, BenchmarkId};
use reqres::{Client, CookieJar};
use std::time::Duration;

/// 基准测试:简单的 GET 请求(HTTP/1.1)
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;
        })
    });
}

/// 基准测试:HTTPS 请求
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;
        })
    });
}

/// 基准测试:HTTP/2 请求
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;
        })
    });
}

/// 基准测试:连接池 vs 无连接池
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;
                }
            })
        },
    );
}

/// 基准测试:Cookie 管理
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"));
        })
    });

    // 构建 Cookie 头
    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);