Skip to content

Commit 9ad0d41

Browse files
committed
feat(go-runner): keep temporary build directory to speedup execution
1 parent fa82646 commit 9ad0d41

3 files changed

Lines changed: 16 additions & 19 deletions

File tree

go-runner/src/builder/templater.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ struct TemplateData {
2121
module_name: String,
2222
}
2323

24-
pub fn run<P: AsRef<Path>>(
25-
package: &BenchmarkPackage,
26-
profile_dir: P,
27-
) -> anyhow::Result<(TempDir, PathBuf)> {
28-
// 1. Copy the whole git repository to a build directory
29-
let target_dir = TempDir::new()?;
24+
pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow::Result<PathBuf> {
25+
// Create a temporary target directory for building the modified Go project.
26+
// NOTE: We don't want to spend time cleanup any temporary files since the code is only
27+
// run on CI servers which clean up themselves.
28+
let target_dir = TempDir::new()?.keep();
3029
std::fs::create_dir_all(&target_dir).context("Failed to create target directory")?;
3130

31+
// 1. Copy the whole git repository to a build directory
3232
let git_root = if let Ok(git_dir) = utils::get_parent_git_repo_path(&package.module.dir) {
3333
git_dir
3434
} else {
@@ -55,7 +55,7 @@ pub fn run<P: AsRef<Path>>(
5555
relative_package_path,
5656
};
5757
fs::write(
58-
target_dir.path().join("go-runner.metadata"),
58+
target_dir.join("go-runner.metadata"),
5959
serde_json::to_string_pretty(&metadata)?,
6060
)
6161
.context("Failed to write go-runner.metadata file")?;
@@ -76,7 +76,7 @@ pub fn run<P: AsRef<Path>>(
7676
// 2. Patch the imports and package of the test files
7777
// - Renames package declarations (to support main package tests and external tests)
7878
// - Fixes imports to use our compat packages (e.g., testing/quicktest/testify)
79-
let package_path = target_dir.path().join(relative_package_path);
79+
let package_path = target_dir.join(relative_package_path);
8080
let test_file_paths: Vec<PathBuf> = files.iter().map(|f| package_path.join(f)).collect();
8181

8282
// If we have external tests (e.g. "package {pkg}_test") they have to be
@@ -97,18 +97,15 @@ pub fn run<P: AsRef<Path>>(
9797
// Find the module directory by getting the relative path from git root
9898
let module_dir = Path::new(&package.module.dir)
9999
.strip_prefix(&git_root)
100-
.map(|relative_module_path| target_dir.path().join(relative_module_path))
100+
.map(|relative_module_path| target_dir.join(relative_module_path))
101101
.unwrap_or_else(|_| {
102102
// Fall back to target_dir if we can't calculate relative path
103-
target_dir.path().to_path_buf()
103+
target_dir.to_path_buf()
104104
});
105105
patcher::install_codspeed_dependency(&module_dir)?;
106106

107107
// 3. Handle test files differently based on whether they're external or internal tests
108-
let codspeed_dir = target_dir
109-
.path()
110-
.join(relative_package_path)
111-
.join("codspeed");
108+
let codspeed_dir = target_dir.join(relative_package_path).join("codspeed");
112109
fs::create_dir_all(&codspeed_dir).context("Failed to create codspeed directory")?;
113110

114111
if package.is_external_test_package() {
@@ -117,7 +114,7 @@ pub fn run<P: AsRef<Path>>(
117114
// They're now package main and will be built from the subdirectory
118115
debug!("Handling external test package - moving files to codspeed/ subdirectory");
119116
for file in files {
120-
let src_path = target_dir.path().join(relative_package_path).join(file);
117+
let src_path = target_dir.join(relative_package_path).join(file);
121118
// Rename _test.go to _codspeed.go so it's not treated as a test file
122119
let dst_filename = file.replace("_test.go", "_codspeed.go");
123120
let dst_path = codspeed_dir.join(&dst_filename);
@@ -130,7 +127,7 @@ pub fn run<P: AsRef<Path>>(
130127
// For internal test packages: rename _test.go to _codspeed.go in place
131128
debug!("Handling internal test package - renaming files in place");
132129
for file in files {
133-
let old_path = target_dir.path().join(relative_package_path).join(file);
130+
let old_path = target_dir.join(relative_package_path).join(file);
134131
let new_path = old_path.with_file_name(
135132
old_path
136133
.file_name()
@@ -160,5 +157,5 @@ pub fn run<P: AsRef<Path>>(
160157
let runner_path = codspeed_dir.join("runner.go");
161158
fs::write(&runner_path, rendered).context("Failed to write runner.go file")?;
162159

163-
Ok((target_dir, runner_path))
160+
Ok(runner_path)
164161
}

go-runner/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn run_benchmarks<P: AsRef<Path>>(
3535
// 2. Generate codspeed runners, build binaries, and execute them
3636
for package in &packages {
3737
info!("Generating custom runner for package: {}", package.name);
38-
let (_target_dir, runner_path) = builder::templater::run(package, &profile_dir)?;
38+
let runner_path = builder::templater::run(package, &profile_dir)?;
3939

4040
info!("Building binary for package: {}", package.name);
4141

go-runner/tests/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::Path;
44
/// Helper function to run a single package with arguments
55
pub fn run_package_with_args(package: &BenchmarkPackage, args: &[&str]) -> anyhow::Result<String> {
66
let profile_dir = tempfile::TempDir::new()?;
7-
let (_dir, runner_path) = builder::templater::run(package, profile_dir.as_ref())?;
7+
let runner_path = builder::templater::run(package, profile_dir.as_ref())?;
88
let binary_path = builder::build_binary(&runner_path)?;
99
runner::run_with_stdout(&binary_path, args)
1010
}

0 commit comments

Comments
 (0)