Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

refactor(bench): simplify benchmark setup (#608)

Maximilian Roosd6435980fa86
diff --git a/benches/completion.rs b/benches/completion.rs
index 752635274..efb1f8eba 100644
--- a/benches/completion.rs
+++ b/benches/completion.rs
@@ -1,72 +1,11 @@
use criterion::{Criterion, criterion_group, criterion_main};
use std::path::Path;
use std::process::Command;
-use tempfile::TempDir;
+use wt_perf::{RepoConfig, create_repo};
-/// Create a test git repository with specified number of branches and worktrees
-fn setup_test_repo(num_branches: usize, num_worktrees: usize) -> TempDir {
- let temp_dir = tempfile::tempdir().unwrap();
- let repo_path = temp_dir.path();
-
- // Initialize git repo
- Command::new("git")
- .args(["init"])
- .current_dir(repo_path)
- .output()
- .unwrap();
-
- // Configure git
- Command::new("git")
- .args(["config", "user.name", "Test"])
- .current_dir(repo_path)
- .output()
- .unwrap();
-
- Command::new("git")
- .args(["config", "user.email", "[email protected]"])
- .current_dir(repo_path)
- .output()
- .unwrap();
-
- // Create initial commit
- Command::new("git")
- .args(["commit", "--allow-empty", "-m", "initial"])
- .current_dir(repo_path)
- .output()
- .unwrap();
-
- // Create branches
- for i in 0..num_branches {
- Command::new("git")
- .args(["branch", &format!("branch-{}", i)])
- .current_dir(repo_path)
- .output()
- .unwrap();
- }
-
- // Create worktrees on separate branches
- for i in 0..num_worktrees {
- let worktree_path = repo_path.join(format!("wt-{}", i));
- Command::new("git")
- .args([
- "worktree",
- "add",
- "-b",
- &format!("wt-branch-{}", i),
- worktree_path.to_str().unwrap(),
- ])
- .current_dir(repo_path)
- .output()
- .unwrap();
- }
-
- temp_dir
-}
-
-fn run_completion(repo_path: &Path, words: &[&str]) {
+fn run_completion(binary: &Path, repo_path: &Path, words: &[&str]) {
let index = words.len().saturating_sub(1);
- Command::new("cargo")
- .args(["run", "--"])
+ Command::new(binary)
.env("COMPLETE", "bash")
.env("_CLAP_COMPLETE_INDEX", index.to_string())
.env("_CLAP_COMPLETE_COMP_TYPE", "9")
@@ -81,56 +20,42 @@ fn run_completion(repo_path: &Path, words: &[&str]) {
fn bench_completion_switch(c: &mut Criterion) {
let mut group = c.benchmark_group("completion_switch");
-
- // Benchmark with 10 branches
- group.bench_function("10_branches", |b| {
- let temp = setup_test_repo(10, 0);
- b.iter(|| run_completion(temp.path(), &["wt", "switch", ""]));
- });
-
- // Benchmark with 50 branches
- group.bench_function("50_branches", |b| {
- let temp = setup_test_repo(50, 0);
- b.iter(|| run_completion(temp.path(), &["wt", "switch", ""]));
- });
-
- // Benchmark with 100 branches
- group.bench_function("100_branches", |b| {
- let temp = setup_test_repo(100, 0);
- b.iter(|| run_completion(temp.path(), &["wt", "switch", ""]));
- });
-
- group.finish();
-}
-
-fn bench_completion_switch_with_worktrees(c: &mut Criterion) {
- let mut group = c.benchmark_group("completion_switch_filtered");
-
- // Benchmark with 50 branches, 10 with worktrees (tests filtering performance)
- group.bench_function("50_branches_10_worktrees", |b| {
- let temp = setup_test_repo(50, 10);
- b.iter(|| run_completion(temp.path(), &["wt", "switch", ""]));
+ let binary = Path::new(env!("CARGO_BIN_EXE_wt"));
+
+ // Without worktrees: all branches are candidates
+ group.bench_function("branches_only", |b| {
+ let config = RepoConfig {
+ commits_on_main: 1,
+ files: 1,
+ branches: 50,
+ commits_per_branch: 0,
+ worktrees: 0,
+ worktree_commits_ahead: 0,
+ worktree_uncommitted_files: 0,
+ };
+ let temp = create_repo(&config);
+ let repo = temp.path().join("repo");
+ b.iter(|| run_completion(binary, &repo, &["wt", "switch", ""]));
});
- group.finish();
-}
-
-fn bench_completion_push(c: &mut Criterion) {
- let mut group = c.benchmark_group("completion_push");
-
- // Benchmark push completion (shows all branches, no filtering)
- group.bench_function("100_branches", |b| {
- let temp = setup_test_repo(100, 0);
- b.iter(|| run_completion(temp.path(), &["wt", "push", ""]));
+ // With worktrees: filters out branches that already have worktrees
+ group.bench_function("with_worktrees", |b| {
+ let config = RepoConfig {
+ commits_on_main: 1,
+ files: 1,
+ branches: 50,
+ commits_per_branch: 0,
+ worktrees: 10,
+ worktree_commits_ahead: 0,
+ worktree_uncommitted_files: 0,
+ };
+ let temp = create_repo(&config);
+ let repo = temp.path().join("repo");
+ b.iter(|| run_completion(binary, &repo, &["wt", "switch", ""]));
});
group.finish();
}
-criterion_group!(
- benches,
- bench_completion_switch,
- bench_completion_switch_with_worktrees,
- bench_completion_push
-);
+criterion_group!(benches, bench_completion_switch);
criterion_main!(benches);
diff --git a/benches/list.rs b/benches/list.rs
index 5877551c7..8c3993060 100644
--- a/benches/list.rs
+++ b/benches/list.rs
@@ -76,17 +76,8 @@ fn run_git(path: &Path, args: &[&str]) {
);
}
-fn get_release_binary() -> PathBuf {
- let build_output = Command::new("cargo")
- .args(["build", "--release"])
- .output()
- .unwrap();
- assert!(
- build_output.status.success(),
- "Failed to build release binary: {}",
- String::from_utf8_lossy(&build_output.stderr)
- );
- std::env::current_dir().unwrap().join("target/release/wt")
+fn get_release_binary() -> &'static Path {
+ Path::new(env!("CARGO_BIN_EXE_wt"))
}
/// Run a benchmark with the given config.
@@ -139,7 +130,7 @@ fn bench_skeleton(c: &mut Criterion) {
|b, config| {
run_benchmark(
b,
- &binary,
+ binary,
&repo_path,
config,
&["list"],
@@ -168,7 +159,7 @@ fn bench_complete(c: &mut Criterion) {
BenchmarkId::new(config.label(), worktrees),
&config,
|b, config| {
- run_benchmark(b, &binary, &repo_path, config, &["list"], None);
+ run_benchmark(b, binary, &repo_path, config, &["list"], None);
},
);
}
@@ -192,7 +183,7 @@ fn bench_worktree_scaling(c: &mut Criterion) {
BenchmarkId::new(config.label(), worktrees),
&config,
|b, config| {
- run_benchmark(b, &binary, &repo_path, config, &["list"], None);
+ run_benchmark(b, binary, &repo_path, config, &["list"], None);
},
);
}
@@ -281,7 +272,7 @@ fn bench_real_repo(c: &mut Criterion) {
b.iter_batched(
|| invalidate_caches(&workspace_main, worktrees),
|_| {
- Command::new(&binary)
+ Command::new(binary)
.arg("list")
.current_dir(&workspace_main)
.output()
@@ -292,7 +283,7 @@ fn bench_real_repo(c: &mut Criterion) {
} else {
run_git(&workspace_main, &["status"]);
b.iter(|| {
- Command::new(&binary)
+ Command::new(binary)
.arg("list")
.current_dir(&workspace_main)
.output()
@@ -320,7 +311,7 @@ fn bench_many_branches(c: &mut Criterion) {
group.bench_function(config.label(), |b| {
run_benchmark(
b,
- &binary,
+ binary,
&repo_path,
&config,
&["list", "--branches", "--progressive"],
@@ -348,7 +339,7 @@ fn bench_divergent_branches(c: &mut Criterion) {
group.bench_function(config.label(), |b| {
run_benchmark(
b,
- &binary,
+ binary,
&repo_path,
&config,
&["list", "--branches", "--progressive"],
@@ -451,7 +442,7 @@ fn bench_real_repo_many_branches(c: &mut Criterion) {
group.bench_function("warm", |b| {
let (_temp, workspace_main) = setup_workspace();
b.iter(|| {
- Command::new(&binary)
+ Command::new(binary)
.args(["list", "--branches"])
.current_dir(&workspace_main)
.output()
@@ -463,7 +454,7 @@ fn bench_real_repo_many_branches(c: &mut Criterion) {
group.bench_function("warm_optimized", |b| {
let (_temp, workspace_main) = setup_workspace();
b.iter(|| {
- Command::new(&binary)
+ Command::new(binary)
.args(["list", "--branches"])
.env("WORKTRUNK_TEST_SKIP_EXPENSIVE_THRESHOLD", "1")
.current_dir(&workspace_main)
@@ -476,7 +467,7 @@ fn bench_real_repo_many_branches(c: &mut Criterion) {
group.bench_function("warm_worktrees_only", |b| {
let (_temp, workspace_main) = setup_workspace();
b.iter(|| {
- Command::new(&binary)
+ Command::new(binary)
.arg("list") // no --branches
.current_dir(&workspace_main)
.output()
@@ -506,7 +497,7 @@ fn bench_timeout_effect(c: &mut Criterion) {
// Without timeout (baseline)
group.bench_function("no_timeout", |b| {
b.iter(|| {
- Command::new(&binary)
+ Command::new(binary)
.args(["list", "--branches"])
.current_dir(&workspace_main)
.output()
@@ -517,7 +508,7 @@ fn bench_timeout_effect(c: &mut Criterion) {
// With 500ms timeout (GH #461 fix)
group.bench_function("timeout_500ms", |b| {
b.iter(|| {
- Command::new(&binary)
+ Command::new(binary)
.args(["list", "--branches"])
.env("WORKTRUNK_COMMAND_TIMEOUT_MS", "500")
.current_dir(&workspace_main)