Skip to content

Commit 3bc23d8

Browse files
committed
feat(criterion_compat): add walltime support
1 parent e4bd2bc commit 3bc23d8

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,59 @@ 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+
pub fn create_uri_and_name<M: Measurement>(
269+
id: &BenchmarkId,
270+
c: &Criterion<M>,
271+
) -> (String, String) {
272+
let mut bench_name = id.group_id.clone();
273+
if let Some(ref function_name) = id.function_id {
274+
bench_name.push_str(&format!("::{}", function_name));
275+
}
276+
if let Some(ref parameter) = id.value_str {
277+
bench_name.push_str(&format!("[{}]", parameter));
278+
}
279+
280+
let git_relative_file_path = codspeed::utils::get_git_relative_path(&c.current_file);
281+
let uri = format!(
282+
"{}::{}::{}",
283+
git_relative_file_path.to_string_lossy(),
284+
&c.macro_group,
285+
bench_name
286+
);
287+
288+
(uri, bench_name)
289+
}
290+
291+
pub(crate) fn collect_walltime_results<M: Measurement>(
292+
id: &BenchmarkId,
293+
c: &Criterion<M>,
294+
iters: &[f64],
295+
avg_times: &[f64],
296+
) {
297+
let (uri, bench_name) = create_uri_and_name(id, c);
298+
299+
let avg_iter_per_round = iters.iter().sum::<f64>() / iters.len() as f64;
300+
let max_time_ns = Some(c.config.measurement_time.as_nanos());
301+
let times_ns = avg_times.iter().map(|t| *t as u128).collect();
302+
303+
::codspeed::walltime::collect_raw_walltime_results(
304+
"criterion",
305+
bench_name,
306+
uri,
307+
avg_iter_per_round as u32,
308+
max_time_ns,
309+
times_ns,
310+
);
311+
}
259312
}
260313

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

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,25 @@ pub struct Criterion<M: Measurement = WallTime> {
367367
profiler: Box<RefCell<dyn Profiler>>,
368368
connection: Option<MutexGuard<'static, Connection>>,
369369
mode: Mode,
370+
371+
current_file: String,
372+
macro_group: String,
373+
}
374+
375+
mod codspeed {
376+
use crate::{measurement::Measurement, Criterion};
377+
378+
impl<M: Measurement> Criterion<M> {
379+
#[doc(hidden)]
380+
pub fn set_current_file(&mut self, file: impl Into<String>) {
381+
self.current_file = file.into();
382+
}
383+
384+
#[doc(hidden)]
385+
pub fn set_macro_group(&mut self, macro_group: impl Into<String>) {
386+
self.macro_group = macro_group.into();
387+
}
388+
}
370389
}
371390

372391
/// Returns the Cargo target directory, possibly calling `cargo metadata` to
@@ -437,6 +456,8 @@ impl Default for Criterion {
437456
.as_ref()
438457
.map(|mtx| mtx.lock().unwrap()),
439458
mode: Mode::Benchmark,
459+
current_file: String::new(),
460+
macro_group: String::new(),
440461
};
441462

442463
if criterion.connection.is_some() {
@@ -469,6 +490,8 @@ impl<M: Measurement> Criterion<M> {
469490
profiler: self.profiler,
470491
connection: self.connection,
471492
mode: self.mode,
493+
current_file: self.current_file,
494+
macro_group: self.macro_group,
472495
}
473496
}
474497

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(codspeed::abs_file!());
73+
criterion.set_macro_group(format!("{}::{}", stringify!($name), stringify!($target)));
74+
}
7175
$target(&mut criterion);
7276
)+
7377
}

0 commit comments

Comments
 (0)