Skip to content

Commit b27a634

Browse files
committed
fixup: use tmp dir
1 parent f8cd229 commit b27a634

4 files changed

Lines changed: 30 additions & 50 deletions

File tree

go-runner/Cargo.lock

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go-runner/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
anyhow = "1.0.98"
8-
clap = { version = "4.5.41", features = ["derive"] }
8+
clap = { version = "4.5", features = ["derive"] }
99
env_logger = "0.11.8"
1010
glob = "0.3.2"
1111
gosyn = "0.2.9"
@@ -18,8 +18,8 @@ serde = { version = "1.0.219", features = ["derive"] }
1818
serde_json = "1.0.141"
1919
statrs = { version = "0.18.0", default-features = false }
2020
thiserror = "2.0.12"
21+
tempfile = "3.14.0"
2122

2223
[dev-dependencies]
2324
insta = { version = "1.43.1", features = ["json", "redactions"] }
24-
rstest = "0.25.0"
25-
tempfile = "3.14.0"
25+
rstest = "0.26"

go-runner/src/builder/templater.rs

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::path::{Path, PathBuf};
33

44
use handlebars::Handlebars;
55
use serde::{Deserialize, Serialize};
6+
use tempfile::TempDir;
67

78
use crate::builder::{BenchmarkData, GoPackage};
89
use crate::utils;
@@ -52,26 +53,9 @@ struct TemplateData {
5253
module_name: String,
5354
}
5455

55-
pub fn run(package: &GoPackage) -> anyhow::Result<(PathBuf, PathBuf)> {
56-
// TODO:
57-
// [x] 1. Symlink the files to /tmp -> either just the test files or
58-
// [x] 2. Rename the _test.go files to _codspeed.go
59-
// [x] 3. Generate the codspeed/runner.go file using the template
60-
56+
pub fn run(package: &GoPackage) -> anyhow::Result<(TempDir, PathBuf)> {
6157
// 1. Copy the whole module to a build directory
62-
//
63-
// FIXME: Use tmp folder
64-
let target_dir = PathBuf::from(format!(
65-
"/tmp/go-build/build-{}",
66-
package.name() // Path::new(&package.dir)
67-
// .file_name()
68-
// .unwrap()
69-
// .to_string_lossy()
70-
));
71-
// FIXME: Create once and reuse for all the packages
72-
if target_dir.exists() {
73-
fs::remove_dir_all(&target_dir).context("Failed to remove existing target directory")?;
74-
}
58+
let target_dir = TempDir::new()?;
7559
std::fs::create_dir_all(&target_dir).context("Failed to create target directory")?;
7660
utils::copy_dir_all(&package.module.dir, &target_dir)?;
7761

@@ -90,13 +74,12 @@ pub fn run(package: &GoPackage) -> anyhow::Result<(PathBuf, PathBuf)> {
9074
info!("Relative package path: {}", relative_package_path.display());
9175

9276
// 2. Find benchmark files and patch only their imports
93-
let benchmark_files = find_benchmark_files(&target_dir, relative_package_path, files)?;
77+
let benchmark_files = find_benchmark_files(target_dir.path(), relative_package_path, files)?;
9478
patcher::patch_imports(&target_dir, benchmark_files)?;
9579

9680
// 3. Rename the _test.go files to _codspeed.go
97-
9881
for file in files {
99-
let old_path = target_dir.join(relative_package_path).join(file);
82+
let old_path = target_dir.path().join(relative_package_path).join(file);
10083
let new_path = old_path.with_file_name(
10184
old_path
10285
.file_name()
@@ -118,24 +101,25 @@ pub fn run(package: &GoPackage) -> anyhow::Result<(PathBuf, PathBuf)> {
118101
let template_content = include_str!("template.go");
119102
handlebars.register_template_string("main", template_content)?;
120103

121-
// TODO:
122-
// - Find the benchmarks + their names
123-
// - Find their imports and qualified names and everything
124-
//
125-
126104
// import <alias> <mod_path>
127105
// { "<name>", <qualified_path> },
128106
let data = TemplateData {
129107
benchmarks: package.benchmarks()?,
130-
module_name: "asdf".into(), // TODO: Do we need this?
108+
module_name: "codspeed_runner".into(),
131109
};
132110
let rendered = handlebars.render("main", &data)?;
133111

134112
let runner_path = target_dir
113+
.path()
135114
.join(relative_package_path)
136115
.join("codspeed/runner.go");
137-
fs::create_dir_all(target_dir.join(relative_package_path).join("codspeed"))
138-
.context("Failed to create codspeed directory")?;
116+
fs::create_dir_all(
117+
target_dir
118+
.path()
119+
.join(relative_package_path)
120+
.join("codspeed"),
121+
)
122+
.context("Failed to create codspeed directory")?;
139123
fs::write(&runner_path, rendered).context("Failed to write runner.go file")?;
140124

141125
Ok((target_dir, runner_path))

go-runner/src/lib.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,23 @@ pub fn build_benchmarks(project_dir: &Path, binary_dir: &Path) -> anyhow::Result
2626
.sum();
2727
info!("Total benchmarks discovered: {total_benchmarks}");
2828

29+
debug!("Creating binary directory: {}", binary_dir.display());
30+
std::fs::create_dir_all(binary_dir)?;
31+
2932
// 2. Generate codspeed runners
3033
let mut binaries = Vec::new();
3134
for (package_name, package) in &benchmark_packages {
3235
info!("Processing package: {package_name}");
33-
let (_, runner_path) = builder::templater::run(package)?;
36+
let (_target_dir, runner_path) = builder::templater::run(package)?;
3437

3538
// 3. Run the benchmarks
3639
info!("Building benchmarks for: {}", runner_path.display());
37-
binaries.push(builder::runner::build(&runner_path)?);
38-
}
39-
40-
// 4. Copy them to the binary folder
41-
debug!("Creating binary directory: {}", binary_dir.display());
42-
std::fs::create_dir_all(binary_dir)?;
40+
let binary_path = builder::runner::build(&runner_path)?;
4341

44-
let mut copied_binaries = Vec::new();
45-
for binary_path in &binaries {
42+
// 4. Copy the binaries to the binary directory
4643
// Create a unique filename to avoid accidentally overwriting existing benchmarks.
4744
let filename = binary_path.file_name().unwrap().to_string_lossy();
48-
let unique_filename = format!("{}_{:08x}", filename, rand::random::<u32>());
45+
let unique_filename: String = format!("{}_{:08x}", filename, rand::random::<u32>());
4946
let target_path = binary_dir.join(unique_filename);
5047

5148
debug!(
@@ -54,10 +51,10 @@ pub fn build_benchmarks(project_dir: &Path, binary_dir: &Path) -> anyhow::Result
5451
target_path.display()
5552
);
5653
std::fs::copy(binary_path, &target_path)?;
57-
copied_binaries.push(target_path);
54+
binaries.push(target_path);
5855
}
5956

60-
Ok(copied_binaries)
57+
Ok(binaries)
6158
}
6259

6360
pub fn run_benchmarks(bench: &str, binary_dir: &Path) -> anyhow::Result<()> {

0 commit comments

Comments
 (0)