use open_lark::core::{
config::Config,
performance::{BenchmarkConfig, ClientComparison, HttpBenchmark, OptimizedHttpConfig},
};
use std::collections::HashMap;
use tracing::{info, Level};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
info!("🚀 开始HTTP客户端性能基准测试");
let benchmark_config = BenchmarkConfig {
concurrent_connections: 20,
requests_per_connection: 50,
warmup_requests: 10,
target_url: "https://httpbin.org/json".to_string(),
headers: {
let mut headers = HashMap::new();
headers.insert("User-Agent".to_string(), "open-lark-benchmark".to_string());
headers
},
};
let default_config = OptimizedHttpConfig::default();
let production_config = OptimizedHttpConfig::production();
let high_throughput_config = OptimizedHttpConfig::high_throughput();
let low_latency_config = OptimizedHttpConfig::low_latency();
let ultra_fast_config = OptimizedHttpConfig {
pool_max_idle_per_host: 300,
pool_idle_timeout: std::time::Duration::from_secs(300),
connect_timeout: std::time::Duration::from_millis(1000),
request_timeout: std::time::Duration::from_secs(5),
http2_prior_knowledge: true,
http2_adaptive_window: true,
gzip: false,
brotli: false,
user_agent: "open-lark-ultra-fast".to_string(),
};
let configs = vec![
("默认配置", default_config),
("生产环境", production_config),
("高吞吐量", high_throughput_config),
("低延迟", low_latency_config),
("极速自定义", ultra_fast_config),
];
info!("📊 开始配置性能比较测试...");
match ClientComparison::compare_configurations(configs, benchmark_config.clone()).await {
Ok(results) => {
info!("✅ 所有配置测试完成");
print_recommendations(&results);
}
Err(e) => {
eprintln!("❌ 配置比较测试失败: {}", e);
}
}
info!("🔧 演示在Config中集成优化配置...");
demonstrate_config_integration().await?;
info!("🎉 性能基准测试完成!");
Ok(())
}
fn print_recommendations(results: &[(String, open_lark::core::performance::PerformanceMetrics)]) {
info!("╔══════════════════════════════════════════════════════════════════════════╗");
info!("║ 📊 配置推荐 ║");
info!("╚══════════════════════════════════════════════════════════════════════════╝");
if let Some((best_throughput_name, best_throughput)) = results.iter().max_by(|a, b| {
a.1.requests_per_second
.partial_cmp(&b.1.requests_per_second)
.unwrap()
}) {
info!(
"🏆 最佳吞吐量: {} ({:.1} RPS)",
best_throughput_name, best_throughput.requests_per_second
);
info!(" 推荐场景: 批量数据处理、后台同步任务");
}
if let Some((best_latency_name, best_latency)) = results.iter().min_by(|a, b| {
a.1.avg_response_time_ms
.partial_cmp(&b.1.avg_response_time_ms)
.unwrap()
}) {
info!(
"🚀 最低延迟: {} ({:.2}ms 平均延迟)",
best_latency_name, best_latency.avg_response_time_ms
);
info!(" 推荐场景: 实时API调用、用户交互功能");
}
if let Some((most_reliable_name, most_reliable)) = results
.iter()
.min_by(|a, b| a.1.error_rate.partial_cmp(&b.1.error_rate).unwrap())
{
info!(
"🛡️ 最高可靠性: {} ({:.2}% 错误率)",
most_reliable_name, most_reliable.error_rate
);
info!(" 推荐场景: 关键业务流程、金融交易");
}
info!("");
info!("📋 使用建议:");
info!(" 1. 开发/测试环境: 使用默认配置");
info!(" 2. 生产环境 (通用): 使用生产环境配置");
info!(" 3. 高并发场景: 使用高吞吐量配置");
info!(" 4. 实时应用: 使用低延迟配置");
info!(" 5. 特殊需求: 基于基准测试结果定制配置");
}
async fn demonstrate_config_integration() -> Result<(), Box<dyn std::error::Error>> {
info!("创建使用生产环境优化配置的SDK配置...");
let config = Config::builder()
.app_id("test_app_id")
.app_secret("test_app_secret")
.production_http_client()?
.build();
info!("✅ 生产环境配置创建成功");
let custom_http_config = OptimizedHttpConfig {
pool_max_idle_per_host: 150,
pool_idle_timeout: std::time::Duration::from_secs(120),
connect_timeout: std::time::Duration::from_secs(3),
request_timeout: std::time::Duration::from_secs(20),
http2_prior_knowledge: true,
http2_adaptive_window: true,
gzip: true,
brotli: true,
user_agent: "my-custom-app/1.0".to_string(),
};
let _custom_config = Config::builder()
.app_id("test_app_id")
.app_secret("test_app_secret")
.optimized_http_client(custom_http_config)?
.build();
info!("✅ 自定义优化配置创建成功");
info!("配置引用计数: {}", config.reference_count());
info!("HTTP客户端池配置: 已优化");
Ok(())
}
#[allow(dead_code)]
async fn run_detailed_benchmark() -> Result<(), Box<dyn std::error::Error>> {
info!("🔍 运行详细基准测试...");
let config = OptimizedHttpConfig::production();
let client = config.build_client()?;
let benchmark_config = BenchmarkConfig {
concurrent_connections: 50,
requests_per_connection: 200,
warmup_requests: 50,
target_url: "https://httpbin.org/json".to_string(),
headers: HashMap::new(),
};
let benchmark = HttpBenchmark::new(client, benchmark_config);
let metrics = benchmark
.run_benchmark()
.await
.map_err(|e| format!("Benchmark failed: {}", e))?;
info!("📈 详细基准测试结果:");
metrics.print_report();
Ok(())
}