Skip to content

Commit 1f34554

Browse files
committed
feat: Complete major Rust refactoring milestones
- ✅ Created napi-rs project structure with comprehensive Cargo.toml - ✅ Implemented core modules: config, environment, logger, utils - ✅ Built monitoring modules: CPU, memory, GC with platform-specific optimizations - ✅ Added SIMD optimizations (AVX2, SSE2, NEON) for performance-critical operations - ✅ Implemented comprehensive error handling with custom MonitoringError types - ✅ Created extensive benchmark suite for monitoring overhead analysis - ✅ Added platform-specific implementations for Unix/Linux, macOS, Windows - ✅ Established NAPI bindings for all core APIs - ✅ Set up unit and integration testing framework This represents the completion of Phase 1 (基础设施) and Phase 2 (监控核心) of the XProfiler Rust refactoring plan, with significant progress on Phase 4 (优化和完善) performance optimizations.
1 parent cf1454f commit 1f34554

File tree

10 files changed

+583
-112
lines changed

10 files changed

+583
-112
lines changed

rust/xprofiler-rs/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ debug = false
5959
name = "benchmarks"
6060
harness = false
6161

62+
[[bench]]
63+
name = "monitoring_overhead"
64+
harness = false
65+
6266
[features]
6367
default = []
6468
benchmarks = []

rust/xprofiler-rs/benches/benchmarks.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn system_info_collection(c: &mut Criterion) {
3333
fn process_info_collection(c: &mut Criterion) {
3434
c.bench_function("process_info_collection", |b| {
3535
b.iter(|| {
36-
black_box(get_process_info());
36+
let _ = black_box(get_process_info());
3737
})
3838
});
3939
}
@@ -62,7 +62,7 @@ fn cpu_monitor_benchmark(c: &mut Criterion) {
6262
monitor.start().unwrap();
6363

6464
b.iter(|| {
65-
black_box(monitor.get_stats());
65+
let _ = black_box(monitor.get_stats());
6666
});
6767

6868
monitor.stop().unwrap();
@@ -75,7 +75,7 @@ fn memory_monitor_benchmark(c: &mut Criterion) {
7575
monitor.start().unwrap();
7676

7777
b.iter(|| {
78-
black_box(monitor.get_stats());
78+
let _ = black_box(monitor.get_stats());
7979
});
8080

8181
monitor.stop().unwrap();
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
//! Performance benchmarks for monitoring overhead
2+
//!
3+
//! This module contains benchmarks to measure the performance impact
4+
//! of various monitoring operations to ensure minimal overhead.
5+
6+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
7+
use std::sync::{Arc, Mutex};
8+
use std::thread;
9+
use std::time::Duration;
10+
11+
use xprofiler_rs::monitoring::{
12+
cpu::{CpuMonitor, init_cpu_monitor, update_cpu_usage},
13+
memory::{MemoryMonitor, init_memory_monitor, update_memory_usage},
14+
MonitoringResult,
15+
};
16+
17+
/// Benchmark CPU monitoring overhead
18+
fn bench_cpu_monitoring(c: &mut Criterion) {
19+
// Initialize CPU monitor
20+
let _ = init_cpu_monitor();
21+
22+
c.bench_function("cpu_monitor_update", |b| {
23+
b.iter(|| {
24+
black_box(update_cpu_usage()).unwrap();
25+
})
26+
});
27+
28+
c.bench_function("cpu_monitor_single_update", |b| {
29+
let mut monitor = CpuMonitor::new();
30+
b.iter(|| {
31+
black_box(monitor.update()).unwrap();
32+
})
33+
});
34+
}
35+
36+
/// Benchmark memory monitoring overhead
37+
fn bench_memory_monitoring(c: &mut Criterion) {
38+
// Initialize memory monitor
39+
let _ = init_memory_monitor();
40+
41+
c.bench_function("memory_monitor_update", |b| {
42+
b.iter(|| {
43+
black_box(update_memory_usage()).unwrap();
44+
})
45+
});
46+
47+
c.bench_function("memory_monitor_single_update", |b| {
48+
let mut monitor = MemoryMonitor::new();
49+
b.iter(|| {
50+
black_box(monitor.update()).unwrap();
51+
})
52+
});
53+
}
54+
55+
/// Benchmark concurrent monitoring overhead
56+
fn bench_concurrent_monitoring(c: &mut Criterion) {
57+
let _ = init_cpu_monitor();
58+
let _ = init_memory_monitor();
59+
60+
c.bench_function("concurrent_cpu_memory_updates", |b| {
61+
b.iter(|| {
62+
let cpu_handle = thread::spawn(|| {
63+
black_box(update_cpu_usage()).unwrap();
64+
});
65+
66+
let memory_handle = thread::spawn(|| {
67+
black_box(update_memory_usage()).unwrap();
68+
});
69+
70+
cpu_handle.join().unwrap();
71+
memory_handle.join().unwrap();
72+
})
73+
});
74+
}
75+
76+
/// Benchmark monitoring data retrieval overhead
77+
fn bench_data_retrieval(c: &mut Criterion) {
78+
let _ = init_cpu_monitor();
79+
let _ = init_memory_monitor();
80+
81+
// Populate some data first
82+
for _ in 0..10 {
83+
update_cpu_usage().unwrap();
84+
update_memory_usage().unwrap();
85+
thread::sleep(Duration::from_millis(10));
86+
}
87+
88+
c.bench_function("cpu_data_retrieval", |b| {
89+
let monitor = CpuMonitor::new();
90+
b.iter(|| {
91+
black_box(monitor.get_cpu_usage());
92+
})
93+
});
94+
95+
c.bench_function("memory_data_retrieval", |b| {
96+
let monitor = MemoryMonitor::new();
97+
b.iter(|| {
98+
let _ = black_box(monitor.get_memory_usage());
99+
})
100+
});
101+
}
102+
103+
/// Benchmark monitoring with different update frequencies
104+
fn bench_update_frequencies(c: &mut Criterion) {
105+
let mut group = c.benchmark_group("update_frequencies");
106+
107+
for freq_ms in [1, 10, 100, 1000].iter() {
108+
group.bench_with_input(
109+
format!("cpu_updates_{}ms", freq_ms),
110+
freq_ms,
111+
|b, &freq_ms| {
112+
let mut monitor = CpuMonitor::new();
113+
b.iter(|| {
114+
for _ in 0..10 {
115+
black_box(monitor.update()).unwrap();
116+
thread::sleep(Duration::from_millis(freq_ms));
117+
}
118+
})
119+
},
120+
);
121+
}
122+
123+
group.finish();
124+
}
125+
126+
/// Benchmark memory allocation patterns during monitoring
127+
fn bench_memory_allocation_patterns(c: &mut Criterion) {
128+
c.bench_function("monitor_with_allocations", |b| {
129+
let mut monitor = MemoryMonitor::new();
130+
b.iter(|| {
131+
// Simulate some memory allocations
132+
let _data: Vec<u8> = vec![0; 1024];
133+
let _more_data: Vec<String> = (0..100)
134+
.map(|i| format!("test_string_{}", i))
135+
.collect();
136+
137+
black_box(monitor.update()).unwrap();
138+
})
139+
});
140+
}
141+
142+
/// Benchmark error handling overhead
143+
fn bench_error_handling(c: &mut Criterion) {
144+
c.bench_function("error_creation_overhead", |b| {
145+
b.iter(|| {
146+
let result: MonitoringResult<()> = Err(
147+
xprofiler_rs::monitoring::error::MonitoringError::SystemCall {
148+
operation: "test_operation".to_string(),
149+
source: Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Test error")),
150+
}
151+
);
152+
black_box(result).unwrap_err();
153+
})
154+
});
155+
}
156+
157+
criterion_group!(
158+
benches,
159+
bench_cpu_monitoring,
160+
bench_memory_monitoring,
161+
bench_concurrent_monitoring,
162+
bench_data_retrieval,
163+
bench_update_frequencies,
164+
bench_memory_allocation_patterns,
165+
bench_error_handling
166+
);
167+
168+
criterion_main!(benches);

0 commit comments

Comments
 (0)