Skip to content

Commit 3a1c877

Browse files
refactor(divan_fork): keep all codspeed code within one function
1 parent 968e262 commit 3a1c877

File tree

1 file changed

+76
-78
lines changed
  • crates/divan_compat/divan_fork/src

1 file changed

+76
-78
lines changed

crates/divan_compat/divan_fork/src/divan.rs

Lines changed: 76 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -310,27 +310,11 @@ impl Divan {
310310

311311
if should_compute_stats {
312312
let stats = bench_context.compute_stats();
313-
{
314-
let CodspeedBench { bench_name, uri } =
315-
generate_codspeed_bench_name(&bench_entry, bench_display_name);
316-
317-
let iter_per_round = bench_context.samples.sample_size;
318-
let times_ns: Vec<_> = bench_context
319-
.samples
320-
.time_samples
321-
.iter()
322-
.map(|s| s.duration.picos / 1_000)
323-
.collect();
324-
let max_time_ns = options.max_time.map(|t| t.as_nanos());
325-
::codspeed::walltime::collect_raw_walltime_results(
326-
"divan",
327-
bench_name,
328-
uri,
329-
iter_per_round,
330-
max_time_ns,
331-
times_ns,
332-
);
333-
};
313+
codspeed::collect_walltime_results(
314+
&bench_context,
315+
&bench_entry,
316+
bench_display_name,
317+
);
334318
tree_painter.borrow_mut().finish_leaf(
335319
is_last_thread_count,
336320
&stats,
@@ -371,74 +355,88 @@ impl Divan {
371355
}
372356
}
373357

374-
struct CodspeedBench {
375-
bench_name: String,
376-
uri: String,
377-
}
358+
mod codspeed {
359+
use crate::bench::BenchContext;
360+
use crate::entry::AnyBenchEntry;
378361

379-
/// Generates a Codspeed benchmark name and URI
380-
///
381-
/// WARNING: Keep in sync with `codspeed-divan-compat::uri::generate`
382-
/// Not worth doing the work of actually using the same code since this fork
383-
/// is temporary
384-
fn generate_codspeed_bench_name(
385-
bench_entry: &AnyBenchEntry,
386-
closure_bench_display_name: &str,
387-
) -> CodspeedBench {
388-
let bench_function_name = bench_entry.meta().display_name;
389-
390-
let (bench_type_name, bench_arg_name) = {
391-
let bench_function_or_type_name = bench_entry.display_name().to_string();
392-
393-
let type_name = if bench_function_or_type_name == bench_function_name {
394-
None
395-
} else {
396-
Some(bench_function_or_type_name)
397-
};
362+
pub(crate) fn collect_walltime_results(
363+
bench_context: &BenchContext,
364+
bench_entry: &AnyBenchEntry,
365+
closure_bench_display_name: &str,
366+
) {
367+
// WARNING: Keep URI generation in sync with `codspeed-divan-compat::uri::generate`
368+
// Not worth doing the work of actually using the same code since this fork is temporary
369+
let (bench_name, uri) = {
370+
let bench_function_name = bench_entry.meta().display_name;
371+
372+
let (bench_type_name, bench_arg_name) = {
373+
let bench_function_or_type_name = bench_entry.display_name().to_string();
398374

399-
let arg_name = match type_name.as_ref() {
400-
None => {
401-
if closure_bench_display_name == bench_function_name {
375+
let type_name = if bench_function_or_type_name == bench_function_name {
402376
None
403377
} else {
404-
Some(closure_bench_display_name)
378+
Some(bench_function_or_type_name)
379+
};
380+
381+
let arg_name = match type_name.as_ref() {
382+
None => {
383+
if closure_bench_display_name == bench_function_name {
384+
None
385+
} else {
386+
Some(closure_bench_display_name)
387+
}
388+
}
389+
Some(type_name) => {
390+
if closure_bench_display_name == type_name {
391+
None
392+
} else {
393+
Some(closure_bench_display_name)
394+
}
395+
}
396+
};
397+
398+
(type_name, arg_name)
399+
};
400+
401+
let mut bench_name = bench_function_name.to_string();
402+
403+
match (bench_type_name, bench_arg_name) {
404+
(None, None) => {}
405+
(Some(type_name), None) => {
406+
bench_name.push_str(format!("[{type_name}]").as_str());
405407
}
406-
}
407-
Some(type_name) => {
408-
if closure_bench_display_name == type_name {
409-
None
410-
} else {
411-
Some(closure_bench_display_name)
408+
(None, Some(arg_name)) => {
409+
bench_name.push_str(format!("[{arg_name}]").as_str());
410+
}
411+
(Some(type_name), Some(arg_name)) => {
412+
bench_name.push_str(format!("[{type_name}, {arg_name}]").as_str());
412413
}
413414
}
414-
};
415-
416-
(type_name, arg_name)
417-
};
418415

419-
let mut bench_name = bench_function_name.to_string();
416+
let file = bench_entry.meta().location.file;
417+
let mut module_path =
418+
bench_entry.meta().module_path_components().skip(1).collect::<Vec<_>>().join("::");
419+
if !module_path.is_empty() {
420+
module_path.push_str("::");
421+
}
422+
let uri = format!("{file}::{module_path}{bench_name}");
423+
(bench_name, uri)
424+
};
420425

421-
match (bench_type_name, bench_arg_name) {
422-
(None, None) => {}
423-
(Some(type_name), None) => {
424-
bench_name.push_str(format!("[{type_name}]").as_str());
425-
}
426-
(None, Some(arg_name)) => {
427-
bench_name.push_str(format!("[{arg_name}]").as_str());
428-
}
429-
(Some(type_name), Some(arg_name)) => {
430-
bench_name.push_str(format!("[{type_name}, {arg_name}]").as_str());
431-
}
432-
}
426+
let iter_per_round = bench_context.samples.sample_size;
427+
let times_ns: Vec<_> =
428+
bench_context.samples.time_samples.iter().map(|s| s.duration.picos / 1_000).collect();
429+
let max_time_ns = bench_context.options.max_time.map(|t| t.as_nanos());
433430

434-
let file = bench_entry.meta().location.file;
435-
let mut module_path =
436-
bench_entry.meta().module_path_components().skip(1).collect::<Vec<_>>().join("::");
437-
if !module_path.is_empty() {
438-
module_path.push_str("::");
431+
::codspeed::walltime::collect_raw_walltime_results(
432+
"divan",
433+
bench_name,
434+
uri,
435+
iter_per_round,
436+
max_time_ns,
437+
times_ns,
438+
);
439439
}
440-
let uri = format!("{file}::{module_path}{bench_name}");
441-
CodspeedBench { bench_name, uri }
442440
}
443441

444442
/// Makes `Divan::skip_regex` input polymorphic.

0 commit comments

Comments
 (0)