Skip to content

Commit b3fce5d

Browse files
committed
feat(criterion_compat): add walltime support
Signed-off-by: not-matthias <[email protected]>
1 parent 5619eed commit b3fce5d

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

crates/criterion_compat/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ categories = [
1717
]
1818
keywords = ["codspeed", "benchmark", "criterion"]
1919
[dependencies]
20-
criterion = { package = "codspeed-criterion-compat-walltime", path = "criterion-0.5.1", version = "0.5.1", default-features = false }
20+
criterion = { package = "codspeed-criterion-compat-walltime", path = "./criterion-0.5.1", version = "0.5.1", default-features = false }
2121
codspeed = { path = "../codspeed", version = "=2.8.1" }
2222
colored = "2.1.0"
2323

crates/criterion_compat/criterion-0.5.1/src/analysis/mod.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,60 @@ pub(crate) fn common<M: Measurement, T: ?Sized>(
256256
);
257257
}
258258
}
259+
260+
if criterion.should_save_baseline() && std::env::var("CODSPEED_ENV").is_ok() {
261+
codspeed::collect_walltime_results(id, criterion, &iters, avg_times);
262+
}
263+
}
264+
265+
mod codspeed {
266+
use crate::{measurement::Measurement, report::BenchmarkId, Criterion};
267+
268+
/// WARNING: Keep URI generation in sync with `codspeed-criterion-compat::compat::group::run_bench`
269+
pub fn create_uri_and_name<M: Measurement>(
270+
id: &BenchmarkId,
271+
c: &Criterion<M>,
272+
) -> (String, String) {
273+
let mut bench_name = id.group_id.clone();
274+
if let Some(ref function_name) = id.function_id {
275+
bench_name.push_str(&format!("::{}", function_name));
276+
}
277+
if let Some(ref parameter) = id.value_str {
278+
bench_name.push_str(&format!("[{}]", parameter));
279+
}
280+
281+
let git_relative_file_path = codspeed::utils::get_git_relative_path(&c.current_file);
282+
let uri = format!(
283+
"{}::{}::{}",
284+
git_relative_file_path.to_string_lossy(),
285+
&c.macro_group,
286+
bench_name
287+
);
288+
289+
(uri, bench_name)
290+
}
291+
292+
pub(crate) fn collect_walltime_results<M: Measurement>(
293+
id: &BenchmarkId,
294+
c: &Criterion<M>,
295+
iters: &[f64],
296+
avg_times: &[f64],
297+
) {
298+
let (uri, bench_name) = create_uri_and_name(id, c);
299+
300+
let avg_iter_per_round = iters.iter().sum::<f64>() / iters.len() as f64;
301+
let max_time_ns = Some(c.config.measurement_time.as_nanos());
302+
let times_ns = avg_times.iter().map(|t| *t as u128).collect();
303+
304+
::codspeed::walltime::collect_raw_walltime_results(
305+
"criterion",
306+
bench_name,
307+
uri,
308+
avg_iter_per_round as u32,
309+
max_time_ns,
310+
times_ns,
311+
);
312+
}
259313
}
260314

261315
fn base_dir_exists(id: &BenchmarkId, baseline: &str, output_directory: &Path) -> bool {

crates/criterion_compat/criterion-0.5.1/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,26 @@ pub struct Criterion<M: Measurement = WallTime> {
375375
profiler: Box<RefCell<dyn Profiler>>,
376376
connection: Option<MutexGuard<'static, Connection>>,
377377
mode: Mode,
378+
379+
current_file: String,
380+
macro_group: String,
381+
}
382+
383+
pub use ::codspeed::abs_file; // Required by the `criterion_group!` macro
384+
mod codspeed {
385+
use crate::{measurement::Measurement, Criterion};
386+
387+
impl<M: Measurement> Criterion<M> {
388+
#[doc(hidden)]
389+
pub fn set_current_file(&mut self, file: impl Into<String>) {
390+
self.current_file = file.into();
391+
}
392+
393+
#[doc(hidden)]
394+
pub fn set_macro_group(&mut self, macro_group: impl Into<String>) {
395+
self.macro_group = macro_group.into();
396+
}
397+
}
378398
}
379399

380400
/// Returns the Cargo target directory, possibly calling `cargo metadata` to
@@ -445,6 +465,8 @@ impl Default for Criterion {
445465
.as_ref()
446466
.map(|mtx| mtx.lock().unwrap()),
447467
mode: Mode::Benchmark,
468+
current_file: String::new(),
469+
macro_group: String::new(),
448470
};
449471

450472
if criterion.connection.is_some() {
@@ -477,6 +499,8 @@ impl<M: Measurement> Criterion<M> {
477499
profiler: self.profiler,
478500
connection: self.connection,
479501
mode: self.mode,
502+
current_file: self.current_file,
503+
macro_group: self.macro_group,
480504
}
481505
}
482506

crates/criterion_compat/criterion-0.5.1/src/macros.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ macro_rules! criterion_group {
6868
let mut criterion: $crate::Criterion<_> = $config
6969
.configure_from_args();
7070
$(
71+
if std::env::var("CODSPEED_ENV").is_ok() {
72+
criterion.set_current_file($crate::abs_file!());
73+
criterion.set_macro_group(format!("{}::{}", stringify!($name), stringify!($target)));
74+
}
7175
$target(&mut criterion);
7276
)+
7377
}

crates/criterion_compat/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub use codspeed::codspeed_uri;
22

33
#[cfg(not(codspeed))]
44
mod compat_criterion {
5+
pub use codspeed::abs_file;
56
pub use criterion::*;
67
}
78

0 commit comments

Comments
 (0)