main.rs 4.5 KB

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