forked from CodSpeedHQ/codspeed-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodspeed.rs
More file actions
80 lines (70 loc) · 2.07 KB
/
codspeed.rs
File metadata and controls
80 lines (70 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::measurement;
use colored::Colorize;
use std::ffi::CString;
pub use std::hint::black_box;
pub const WARMUP_RUNS: u32 = 5;
pub fn display_native_harness() {
println!("Harness: codspeed v{}", env!("CARGO_PKG_VERSION"),);
}
pub struct CodSpeed {
benchmarked: Vec<String>,
current_benchmark: CString,
group_stack: Vec<String>,
is_instrumented: bool,
}
impl CodSpeed {
pub fn new() -> Self {
let is_instrumented = measurement::is_instrumented();
if !is_instrumented {
println!(
"{} codspeed is enabled, but no performance measurement will be made since it's running in an unknown environment.",
"NOTICE:".to_string().bold()
);
}
measurement::set_metadata();
Self {
benchmarked: Vec::new(),
current_benchmark: CString::new("").expect("CString::new failed"),
group_stack: Vec::new(),
is_instrumented,
}
}
pub fn push_group(&mut self, group: &str) {
self.group_stack.push(group.to_string());
}
pub fn pop_group(&mut self) {
self.group_stack.pop();
}
#[inline(always)]
pub fn start_benchmark(&mut self, name: &str) {
self.current_benchmark = CString::new(name).expect("CString::new failed");
measurement::start();
}
#[inline(always)]
pub fn end_benchmark(&mut self) {
measurement::stop(&self.current_benchmark);
self.benchmarked
.push(self.current_benchmark.to_str().unwrap().to_string());
let action_str = if self.is_instrumented {
"Measured"
} else {
"Checked"
};
let group_str = if self.group_stack.is_empty() {
"".to_string()
} else {
format!(" (group: {})", self.group_stack.join("/"))
};
println!(
"{}: {}{}",
action_str,
self.current_benchmark.to_string_lossy(),
group_str
);
}
}
impl Default for CodSpeed {
fn default() -> Self {
Self::new()
}
}