Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions crates/codspeed/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ macro_rules! abs_file {
#[macro_export]
macro_rules! codspeed_uri {
( $name:ident ) => {
format!("{}::{}", file!(), stringify!($name))
format!(
"{}::{}",
$crate::utils::normalize_file_path(file!()),
stringify!($name)
)
};
( $function:path ) => {
format!("{}::{}", file!(), stringify!($function))
format!(
"{}::{}",
$crate::utils::normalize_file_path(file!()),
stringify!($function)
)
};
}

Expand Down
80 changes: 79 additions & 1 deletion crates/codspeed/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;
use std::io;
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};

fn get_parent_git_repo_path(abs_path: &Path) -> io::Result<PathBuf> {
if abs_path.join(".git").exists() {
Expand Down Expand Up @@ -63,6 +64,48 @@ pub fn caller_file_path() -> String {
}
}

/// Removes the `.` and `..` segments of a `file!()` path, for the benchmark
/// URI. It returns paths without such segments unchanged.
///
/// rustc keeps these segments when a module comes from `#[path = "../x.rs"]`
/// or a target from `[[bench]] path = "../x.rs"`. In a URI, a URL parser
/// resolves them, so the benchmark page and its flame graph break.
///
/// It doesn't touch the file system. A `..` segment removes the segment
/// before it, so leading `..` segments stay.
pub fn normalize_file_path(file: &str) -> Cow<'_, str> {
// Splits the string: `Path::components` hides the `.` segments not at the start
let has_dot_segment = file
.split(std::path::is_separator)
.any(|segment| segment == "." || segment == "..");
if !has_dot_segment {
return Cow::Borrowed(file);
}

let mut components: Vec<Component> = Vec::new();
for component in Path::new(file).components() {
match component {
Component::CurDir => {}
Component::ParentDir => match components.last() {
Some(Component::Normal(_)) => {
components.pop();
}
// The root has no parent
Some(Component::RootDir | Component::Prefix(_)) => {}
_ => components.push(component),
},
_ => components.push(component),
}
}
Cow::Owned(
components
.iter()
.collect::<PathBuf>()
.to_string_lossy()
.into_owned(),
)
}

/// Fixes spaces around `::` created by stringify!($function).
pub fn get_formated_function_path(function_path: impl Into<String>) -> String {
let function_path = function_path.into();
Expand Down Expand Up @@ -142,6 +185,41 @@ mod tests {
assert_eq!(relative_path, symlink.canonicalize().unwrap());
}

#[test]
fn test_normalize_file_path_removes_dot_segments() {
let cases = [
("crates/p/benches/../src/x.rs", "crates/p/src/x.rs"),
("crates/p/./benches/x.rs", "crates/p/benches/x.rs"),
("./benches/x.rs", "benches/x.rs"),
("benches/../../shared/x.rs", "../shared/x.rs"),
("../../x.rs", "../../x.rs"),
];
for (file, expected) in cases {
assert_eq!(
Path::new(normalize_file_path(file).as_ref()),
Path::new(expected),
"{file}"
);
}
}

#[cfg(unix)]
Comment thread
lvaroqui marked this conversation as resolved.
Resolved
#[test]
fn test_normalize_file_path_keeps_absolute_paths_absolute() {
assert_eq!(
normalize_file_path("/home/ci/ws/a/../b.rs"),
"/home/ci/ws/b.rs"
);
assert_eq!(normalize_file_path("/../x.rs"), "/x.rs");
}

#[test]
fn test_normalize_file_path_keeps_paths_without_dot_segments() {
for file in ["benches/bench.rs", "a//b.rs", "a/.../b.rs", "a/..b.rs"] {
assert!(matches!(normalize_file_path(file), Cow::Borrowed(f) if f == file));
}
}

/// COD-2324: empty segments (no file/macro group when `criterion_group!` is bypassed)
/// must not produce URIs like `::::my_group::my_bench`.
#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/divan_compat/divan_fork/src/divan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ mod codspeed {
}
}

let file = bench_entry.meta().location.file;
let file = ::codspeed::utils::normalize_file_path(bench_entry.meta().location.file);
let mut module_path =
bench_entry.meta().module_path_components().skip(1).collect::<Vec<_>>().join("::");
if !module_path.is_empty() {
Expand Down
26 changes: 25 additions & 1 deletion crates/divan_compat/src/compat/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) fn generate(bench_entry: &AnyBenchEntry, closure_bench_display_name:
}
}

let file = bench_entry.meta().location.file;
let file = codspeed::utils::normalize_file_path(bench_entry.meta().location.file);
// In the context of a bench, the top level module will be a repetition of the file name, we
// chose to skip it
let mut module_path = bench_entry
Expand Down Expand Up @@ -161,6 +161,30 @@ mod tests {
assert_eq!(uri, "bar.rs::bench_function");
}

#[test]
fn test_generate_removes_dot_segments_from_the_file() {
let meta = EntryMeta {
display_name: "bench_function",
raw_name: "bench_function",
module_path: "test::inner",
location: EntryLocation {
file: "crates/p/benches/../src/inner.rs",
..Default::default()
},
bench_options: None,
};
let bench_entry = BenchEntry {
meta,
bench: BenchEntryRunner::Plain(|_| {}),
};
let uri = generate(&AnyBenchEntry::Bench(&bench_entry), "root/../");
// The args keep their dot segments: they are not a path
assert_eq!(
uri,
"crates/p/src/inner.rs::inner::bench_function[root/../]"
);
}

#[allow(non_upper_case_globals)]
static mock_group_entry: GroupEntry = GroupEntry {
meta: EntryMeta {
Expand Down
Loading