Skip to content

Commit 5e50425

Browse files
committed
fix(go-runner): use git-relative file paths
1 parent 9cbdece commit 5e50425

5 files changed

Lines changed: 166 additions & 2 deletions

File tree

go-runner/src/integration_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn assert_results_snapshots(profile_dir: &Path, project_name: &str) {
6060
// Currently not producing results:
6161
#[case::fuego("fuego")]
6262
#[case::cli_runtime("cli-runtime")]
63+
#[case::example("example")]
6364
fn test_build_and_run(#[case] project_name: &str) {
6465
let project_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
6566
.join("testdata/projects")

go-runner/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ pub fn run_benchmarks(project_dir: &Path, cli: &crate::cli::Cli) -> anyhow::Resu
2727
let mut bench_name_to_path = HashMap::new();
2828
for package in &packages {
2929
for benchmark in &package.benchmarks {
30-
bench_name_to_path.insert(benchmark.name.clone(), benchmark.file_path.clone());
30+
// Create absolute path and immediately convert to git-relative path
31+
let abs_path = package.module.dir.join(&benchmark.file_path);
32+
let git_relative_path = crate::utils::get_git_relative_path(&abs_path);
33+
bench_name_to_path.insert(benchmark.name.clone(), git_relative_path);
3134
}
3235
}
3336

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
source: go-runner/src/integration_tests.rs
3+
expression: content
4+
---
5+
{
6+
"creator": {
7+
"name": "codspeed-go",
8+
"version": "0.1.0",
9+
"pid": "[pid]"
10+
},
11+
"instrument": {
12+
"type": "walltime"
13+
},
14+
"benchmarks": [
15+
{
16+
"name": "BenchmarkFibonacci10::fibonacci(10)::fibonacci(10)",
17+
"uri": "example/fib_test.go::BenchmarkFibonacci10::fibonacci(10)::fibonacci(10)",
18+
"config": {
19+
"warmup_time_ns": null,
20+
"min_round_time_ns": null,
21+
"max_time_ns": null,
22+
"max_rounds": null
23+
},
24+
"stats": "[stats]"
25+
},
26+
{
27+
"name": "BenchmarkFibonacci20_Loop",
28+
"uri": "example/fib_test.go::BenchmarkFibonacci20_Loop",
29+
"config": {
30+
"warmup_time_ns": null,
31+
"min_round_time_ns": null,
32+
"max_time_ns": null,
33+
"max_rounds": null
34+
},
35+
"stats": "[stats]"
36+
},
37+
{
38+
"name": "BenchmarkFibonacci20_bN",
39+
"uri": "example/fib_test.go::BenchmarkFibonacci20_bN",
40+
"config": {
41+
"warmup_time_ns": null,
42+
"min_round_time_ns": null,
43+
"max_time_ns": null,
44+
"max_rounds": null
45+
},
46+
"stats": "[stats]"
47+
},
48+
{
49+
"name": "BenchmarkSleep100ns",
50+
"uri": "example/sleep_test.go::BenchmarkSleep100ns",
51+
"config": {
52+
"warmup_time_ns": null,
53+
"min_round_time_ns": null,
54+
"max_time_ns": null,
55+
"max_rounds": null
56+
},
57+
"stats": "[stats]"
58+
},
59+
{
60+
"name": "BenchmarkSleep100us",
61+
"uri": "example/sleep_test.go::BenchmarkSleep100us",
62+
"config": {
63+
"warmup_time_ns": null,
64+
"min_round_time_ns": null,
65+
"max_time_ns": null,
66+
"max_rounds": null
67+
},
68+
"stats": "[stats]"
69+
},
70+
{
71+
"name": "BenchmarkSleep10ms",
72+
"uri": "example/sleep_test.go::BenchmarkSleep10ms",
73+
"config": {
74+
"warmup_time_ns": null,
75+
"min_round_time_ns": null,
76+
"max_time_ns": null,
77+
"max_rounds": null
78+
},
79+
"stats": "[stats]"
80+
},
81+
{
82+
"name": "BenchmarkSleep10us",
83+
"uri": "example/sleep_test.go::BenchmarkSleep10us",
84+
"config": {
85+
"warmup_time_ns": null,
86+
"min_round_time_ns": null,
87+
"max_time_ns": null,
88+
"max_rounds": null
89+
},
90+
"stats": "[stats]"
91+
},
92+
{
93+
"name": "BenchmarkSleep1ms",
94+
"uri": "example/sleep_test.go::BenchmarkSleep1ms",
95+
"config": {
96+
"warmup_time_ns": null,
97+
"min_round_time_ns": null,
98+
"max_time_ns": null,
99+
"max_rounds": null
100+
},
101+
"stats": "[stats]"
102+
},
103+
{
104+
"name": "BenchmarkSleep1us",
105+
"uri": "example/sleep_test.go::BenchmarkSleep1us",
106+
"config": {
107+
"warmup_time_ns": null,
108+
"min_round_time_ns": null,
109+
"max_time_ns": null,
110+
"max_rounds": null
111+
},
112+
"stats": "[stats]"
113+
},
114+
{
115+
"name": "BenchmarkSleep50ms",
116+
"uri": "example/sleep_test.go::BenchmarkSleep50ms",
117+
"config": {
118+
"warmup_time_ns": null,
119+
"min_round_time_ns": null,
120+
"max_time_ns": null,
121+
"max_rounds": null
122+
},
123+
"stats": "[stats]"
124+
}
125+
]
126+
}

go-runner/src/utils.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::Path;
1+
use std::path::{Path, PathBuf};
22
use std::{fs, io};
33

44
pub fn copy_dir_recursively(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
@@ -18,3 +18,36 @@ pub fn copy_dir_recursively(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io:
1818
}
1919
Ok(())
2020
}
21+
22+
// WARN: Git-related helper functions have been taken from codspeed-rust. Keep in sync!
23+
24+
fn get_parent_git_repo_path(abs_path: &Path) -> io::Result<PathBuf> {
25+
if abs_path.join(".git").exists() {
26+
Ok(abs_path.to_path_buf())
27+
} else {
28+
get_parent_git_repo_path(
29+
abs_path
30+
.parent()
31+
.ok_or(io::Error::from(io::ErrorKind::NotFound))?,
32+
)
33+
}
34+
}
35+
36+
pub fn get_git_relative_path<P>(abs_path: P) -> PathBuf
37+
where
38+
P: AsRef<Path>,
39+
{
40+
if let Ok(canonicalized_abs_path) = abs_path.as_ref().canonicalize() {
41+
// `repo_path` is still canonicalized as it is a subpath of `canonicalized_abs_path`
42+
if let Ok(repo_path) = get_parent_git_repo_path(&canonicalized_abs_path) {
43+
canonicalized_abs_path
44+
.strip_prefix(repo_path)
45+
.expect("Repository path is malformed.")
46+
.to_path_buf()
47+
} else {
48+
canonicalized_abs_path
49+
}
50+
} else {
51+
abs_path.as_ref().to_path_buf()
52+
}
53+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../example

0 commit comments

Comments
 (0)