main.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. use criterion::{Criterion, black_box, criterion_group, criterion_main};
  2. use std::sync::Arc;
  3. use std::time::Duration;
  4. use swb_sys_monitor::cache::{SystemStatsCache, create_cache};
  5. use swb_sys_monitor::server::StatusServer;
  6. use swb_sys_monitor::stats::{CpuStats, CpuUsageBreakdown, SystemStats, collect_system_stats};
  7. use tokio::runtime::Runtime;
  8. fn create_test_stats(hostname: &str, cpu_usage: f32) -> SystemStats {
  9. SystemStats {
  10. hostname: hostname.to_string(),
  11. cpu_usage,
  12. cpu_stats: CpuStats {
  13. overall: CpuUsageBreakdown {
  14. user_percent: cpu_usage * 50.0,
  15. nice_percent: cpu_usage * 10.0,
  16. system_percent: cpu_usage * 40.0,
  17. total_percent: cpu_usage * 100.0,
  18. },
  19. per_core: Vec::new(),
  20. core_count: 0,
  21. },
  22. memory_total: 1024 * 1024 * 1024, // 1GB
  23. memory_used: 512 * 1024 * 1024, // 512MB
  24. memory_available: 256 * 1024 * 1024, // 256MB
  25. memory_cached: 128 * 1024 * 1024, // 128MB
  26. memory_free: 128 * 1024 * 1024, // 128MB
  27. timestamp: std::time::Instant::now(),
  28. }
  29. }
  30. fn bench_cache_operations(c: &mut Criterion) {
  31. let rt = Runtime::new().unwrap();
  32. c.bench_function("cache_creation", |b| {
  33. b.iter(|| {
  34. let cache = create_cache(black_box(10));
  35. black_box(cache);
  36. })
  37. });
  38. c.bench_function("cache_update_and_get", |b| {
  39. let cache = create_cache(10);
  40. let stats = create_test_stats("benchmark", 0.5);
  41. b.iter(|| {
  42. cache.update(black_box(stats.clone()));
  43. let result = cache.get();
  44. black_box(result);
  45. })
  46. });
  47. c.bench_function("cache_concurrent_reads", |b| {
  48. let cache = Arc::new(SystemStatsCache::new(Duration::from_secs(10)));
  49. let stats = create_test_stats("concurrent", 0.7);
  50. cache.update(stats);
  51. b.iter(|| {
  52. rt.block_on(async {
  53. let mut handles = vec![];
  54. for _ in 0..100 {
  55. let cache_clone = cache.clone();
  56. let handle = tokio::spawn(async move {
  57. let result = cache_clone.get();
  58. black_box(result);
  59. });
  60. handles.push(handle);
  61. }
  62. for handle in handles {
  63. handle.await.unwrap();
  64. }
  65. })
  66. })
  67. });
  68. }
  69. fn bench_html_rendering(c: &mut Criterion) {
  70. let _server = StatusServer::new_with_ttl(create_cache(10), 10);
  71. let stats = create_test_stats("渲染测试主机", 0.65);
  72. c.bench_function("html_template_rendering", |b| {
  73. b.iter(|| {
  74. let html = StatusServer::render_html_template(black_box(&stats), 10);
  75. black_box(html);
  76. })
  77. });
  78. c.bench_function("html_rendering_with_large_values", |b| {
  79. let large_stats = SystemStats {
  80. hostname: "大型测试主机名称很长很长".to_string(),
  81. cpu_usage: 0.95,
  82. cpu_stats: CpuStats {
  83. overall: CpuUsageBreakdown {
  84. user_percent: 47.5,
  85. nice_percent: 9.5,
  86. system_percent: 38.0,
  87. total_percent: 95.0,
  88. },
  89. per_core: Vec::new(),
  90. core_count: 0,
  91. },
  92. memory_total: 16 * 1024 * 1024 * 1024, // 16GB
  93. memory_used: 8 * 1024 * 1024 * 1024, // 8GB
  94. memory_available: 4 * 1024 * 1024 * 1024, // 4GB
  95. memory_cached: 2 * 1024 * 1024 * 1024, // 2GB
  96. memory_free: 2 * 1024 * 1024 * 1024, // 2GB
  97. timestamp: std::time::Instant::now(),
  98. };
  99. b.iter(|| {
  100. let html = StatusServer::render_html_template(black_box(&large_stats), 10);
  101. black_box(html);
  102. })
  103. });
  104. }
  105. fn bench_system_stats_collection(c: &mut Criterion) {
  106. let rt = Runtime::new().unwrap();
  107. c.bench_function("system_stats_collection", |b| {
  108. b.iter(|| {
  109. rt.block_on(async {
  110. let stats = collect_system_stats().await;
  111. let _ = black_box(stats);
  112. })
  113. })
  114. });
  115. }
  116. fn bench_memory_allocation(c: &mut Criterion) {
  117. c.bench_function("string_allocation_with_capacity", |b| {
  118. b.iter(|| {
  119. let mut s = String::with_capacity(1024);
  120. s.push_str("测试字符串内容");
  121. black_box(s);
  122. })
  123. });
  124. c.bench_function("string_allocation_without_capacity", |b| {
  125. b.iter(|| {
  126. let mut s = String::new();
  127. s.push_str("测试字符串内容");
  128. black_box(s);
  129. })
  130. });
  131. }
  132. fn bench_cache_ttl_precision(c: &mut Criterion) {
  133. let cache = SystemStatsCache::new(Duration::from_millis(1));
  134. let stats = create_test_stats("ttl_test", 0.3);
  135. c.bench_function("cache_ttl_check", |b| {
  136. b.iter(|| {
  137. cache.update(black_box(stats.clone()));
  138. let result = cache.get();
  139. black_box(result);
  140. })
  141. });
  142. }
  143. criterion_group!(
  144. benches,
  145. bench_cache_operations,
  146. bench_html_rendering,
  147. bench_system_stats_collection,
  148. bench_memory_allocation,
  149. bench_cache_ttl_precision
  150. );
  151. criterion_main!(benches);