Skip to content

Commit b6b38ba

Browse files
committed
feat: add more test projects; add custom binary dir to run tests in parallel
1 parent 1232fbc commit b6b38ba

File tree

9 files changed

+84
-50
lines changed

9 files changed

+84
-50
lines changed

.gitmodules

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
[submodule "go-runner/testdata/projects/caddy"]
22
path = go-runner/testdata/projects/caddy
33
url = https://github.com/caddyserver/caddy.git
4+
[submodule "go-runner/testdata/projects/fzf"]
5+
path = go-runner/testdata/projects/fzf
6+
url = https://github.com/junegunn/fzf.git
7+
[submodule "go-runner/testdata/projects/opentelemetry-go"]
8+
path = go-runner/testdata/projects/opentelemetry-go
9+
url = https://github.com/open-telemetry/opentelemetry-go.git
10+
[submodule "go-runner/testdata/projects/golang-benchmarks"]
11+
path = go-runner/testdata/projects/golang-benchmarks
12+
url = https://github.com/SimonWaldherr/golang-benchmarks.git

go-runner/src/integration_tests.rs

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,67 @@
11
use crate::prelude::*;
22
use std::path::{Path, PathBuf};
3+
use std::sync::Mutex;
34
use tempfile::TempDir;
45

5-
fn setup_test_project(project_name: &str) -> (TempDir, PathBuf) {
6-
let project_path = Path::new("testdata/projects").join(project_name);
7-
let temp_dir = TempDir::new().unwrap();
8-
let test_dir = temp_dir.path();
6+
fn setup_test_project(project_name: &str) -> anyhow::Result<TempDir> {
7+
let project_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
8+
.join("testdata/projects")
9+
.join(project_name);
10+
println!("Project path: {}", project_path.display());
911

10-
crate::utils::copy_dir_all(&project_path, test_dir.join(project_name)).unwrap();
11-
let project_test_path = test_dir.join(project_name);
12+
let temp_dir = TempDir::new()?;
13+
crate::utils::copy_dir_all(&project_path, &temp_dir)?;
1214

13-
std::env::set_current_dir(&project_test_path).unwrap();
15+
Ok(temp_dir)
16+
}
17+
18+
fn assert_benchmarks_created(binary_dir: &Path, n: usize) {
19+
assert!(binary_dir.exists());
1420

15-
(temp_dir, project_test_path)
21+
let entries: Vec<_> = std::fs::read_dir(binary_dir)
22+
.unwrap()
23+
.collect::<Result<Vec<_>, _>>()
24+
.unwrap();
25+
assert_eq!(entries.len(), n);
1626
}
1727

18-
/// Integration tests with real Go projects to ensure end-to-end functionality
19-
#[cfg(test)]
20-
mod tests {
21-
use super::*;
22-
23-
fn assert_benchmarks_created(n: usize) {
24-
let codspeed_dir = std::env::current_dir()
25-
.unwrap()
26-
.join(".codspeed")
27-
.join("walltime");
28-
assert!(codspeed_dir.exists());
29-
30-
let entries: Vec<_> = std::fs::read_dir(&codspeed_dir)
31-
.unwrap()
32-
.collect::<Result<Vec<_>, _>>()
33-
.unwrap();
34-
assert_eq!(entries.len(), n);
35-
}
28+
fn run_benchmark_for_project(project_name: &str) {
29+
let temp_dir = setup_test_project(project_name).unwrap();
3630

37-
#[test]
38-
fn test_caddy_benchmarks() {
39-
let (_temp_dir, caddy_test_path) = setup_test_project("caddy");
31+
let binary_dir = temp_dir.path().join(".codspeed").join("walltime");
32+
let binaries = crate::build_benchmarks(temp_dir.path(), &binary_dir).unwrap();
33+
assert!(!binaries.is_empty(), "No benchmark binaries were created");
34+
assert_benchmarks_created(&binary_dir, binaries.len());
4035

41-
let binaries = crate::build_benchmarks(&caddy_test_path).unwrap();
42-
assert!(!binaries.is_empty(), "No benchmark binaries were created");
43-
assert_benchmarks_created(binaries.len());
36+
// Mutex to prevent concurrent tests from interfering with CODSPEED_PROFILE_FOLDER env var
37+
static ENV_MUTEX: Mutex<()> = Mutex::new(());
38+
let _guard = ENV_MUTEX.lock().unwrap();
4439

45-
// FIXME: Find a better solution for this
46-
unsafe {
47-
std::env::set_var("CODSPEED_PROFILE_FOLDER", caddy_test_path.join("profile"));
48-
}
49-
crate::run_benchmarks(".").unwrap();
40+
unsafe { std::env::set_var("CODSPEED_PROFILE_FOLDER", temp_dir.path().join("profile")) };
41+
if let Err(error) = crate::run_benchmarks(".", &binary_dir) {
42+
panic!("Benchmarks couldn't run: {error}");
5043
}
44+
45+
// TODO: Assert that we have a results.json?
46+
}
47+
48+
#[ignore = "doesn't work atm"]
49+
#[test]
50+
fn test_caddy_benchmarks() {
51+
run_benchmark_for_project("caddy");
52+
}
53+
54+
#[test]
55+
fn test_fzf_benchmarks() {
56+
run_benchmark_for_project("fzf");
57+
}
58+
59+
#[test]
60+
fn test_opentelemetry_go_benchmarks() {
61+
run_benchmark_for_project("opentelemetry-go");
62+
}
63+
64+
#[test]
65+
fn test_golang_benchmarks() {
66+
run_benchmark_for_project("golang-benchmarks");
5167
}

go-runner/src/lib.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) mod utils;
1313
mod integration_tests;
1414

1515
/// Builds the specified go project and writes the benchmarks to the .codspeed folder.
16-
pub fn build_benchmarks(project_dir: &Path) -> anyhow::Result<Vec<PathBuf>> {
16+
pub fn build_benchmarks(project_dir: &Path, binary_dir: &Path) -> anyhow::Result<Vec<PathBuf>> {
1717
// 1. Benchmark and package discovery
1818
let benchmark_packages = builder::discovery::run(project_dir)?;
1919
info!("Discovered {} packages", benchmark_packages.len());
@@ -37,18 +37,16 @@ pub fn build_benchmarks(project_dir: &Path) -> anyhow::Result<Vec<PathBuf>> {
3737
binaries.push(builder::runner::build(&runner_path)?);
3838
}
3939

40-
// 4. Copy them to the .codspeed folder
41-
let codspeed_dir = std::env::current_dir()?.join(".codspeed").join("walltime");
42-
43-
debug!("Creating codspeed directory: {}", codspeed_dir.display());
44-
std::fs::create_dir_all(&codspeed_dir)?;
40+
// 4. Copy them to the binary folder
41+
debug!("Creating binary directory: {}", binary_dir.display());
42+
std::fs::create_dir_all(binary_dir)?;
4543

4644
let mut copied_binaries = Vec::new();
4745
for binary_path in &binaries {
4846
// Create a unique filename to avoid accidentally overwriting existing benchmarks.
4947
let filename = binary_path.file_name().unwrap().to_string_lossy();
5048
let unique_filename = format!("{}_{:08x}", filename, rand::random::<u32>());
51-
let target_path = codspeed_dir.join(unique_filename);
49+
let target_path = binary_dir.join(unique_filename);
5250

5351
debug!(
5452
"Copying {} to {}",
@@ -62,10 +60,9 @@ pub fn build_benchmarks(project_dir: &Path) -> anyhow::Result<Vec<PathBuf>> {
6260
Ok(copied_binaries)
6361
}
6462

65-
pub fn run_benchmarks(bench: &str) -> anyhow::Result<()> {
63+
pub fn run_benchmarks(bench: &str, binary_dir: &Path) -> anyhow::Result<()> {
6664
// 1. Discover the benchmarks
67-
let codspeed_dir = std::env::current_dir()?.join(".codspeed").join("walltime");
68-
let benchmark_packages = std::fs::read_dir(&codspeed_dir)?
65+
let benchmark_packages = std::fs::read_dir(binary_dir)?
6966
.filter_map(Result::ok)
7067
.filter(|entry| entry.path().is_file())
7168
.map(|entry| entry.path())
@@ -77,7 +74,8 @@ pub fn run_benchmarks(bench: &str) -> anyhow::Result<()> {
7774

7875
let cmd = std::process::Command::new(bench_path)
7976
.arg(format!("-test.bench={bench}"))
80-
.output()?;
77+
.output()
78+
.context("Failed to execute benchmark command")?;
8179

8280
// Check if the command was successful
8381
if !cmd.status.success() {

go-runner/src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ fn main() -> anyhow::Result<()> {
3737

3838
let cli = Cli::parse();
3939

40+
let binary_dir = std::env::current_dir()
41+
.unwrap()
42+
.join(".codspeed")
43+
.join("walltime");
4044
match &cli.command {
4145
Commands::Build { path } => {
42-
go_runner::build_benchmarks(path)?;
46+
go_runner::build_benchmarks(path, &binary_dir)?;
4347
}
4448
Commands::Run { bench } => {
45-
go_runner::run_benchmarks(bench)?;
49+
go_runner::run_benchmarks(bench, &binary_dir)?;
4650
}
4751
}
4852

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ignore .git directories in test projects
2+
*/.git
3+
*/.git/*

go-runner/testdata/projects/caddy

Submodule caddy added at fb22a26

go-runner/testdata/projects/fzf

Submodule fzf added at 04c4269
Submodule opentelemetry-go added at 69e8108

0 commit comments

Comments
 (0)