Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor: isolate benchmark commands and consolidate shared utilities into wt-perf (#1878)
Maximilian Roosafa0a478c97f
diff --git a/benches/completion.rs b/benches/completion.rsindex efb1f8eba..fd2fc403e 100644--- a/benches/completion.rs+++ b/benches/completion.rs@@ -1,21 +1,19 @@use criterion::{Criterion, criterion_group, criterion_main};use std::path::Path;use std::process::Command;-use wt_perf::{RepoConfig, create_repo};+use wt_perf::{RepoConfig, create_repo, isolate_cmd};fn run_completion(binary: &Path, repo_path: &Path, words: &[&str]) {let index = words.len().saturating_sub(1);- Command::new(binary)- .env("COMPLETE", "bash")+ let mut cmd = Command::new(binary);+ cmd.arg("--").args(words).current_dir(repo_path);+ isolate_cmd(&mut cmd, None);+ cmd.env("COMPLETE", "bash").env("_CLAP_COMPLETE_INDEX", index.to_string()).env("_CLAP_COMPLETE_COMP_TYPE", "9").env("_CLAP_COMPLETE_SPACE", "true")- .env("_CLAP_IFS", "\n")- .arg("--")- .args(words)- .current_dir(repo_path)- .output()- .unwrap();+ .env("_CLAP_IFS", "\n");+ cmd.output().unwrap();}fn bench_completion_switch(c: &mut Criterion) {diff --git a/benches/list.rs b/benches/list.rsindex 0095d1563..ad66d5e7b 100644--- a/benches/list.rs+++ b/benches/list.rs@@ -26,7 +26,7 @@ use std::path::{Path, PathBuf};use std::process::Command;use wt_perf::{RepoConfig, add_history_spread_branches, add_worktrees, clone_rust_repo, create_repo,- invalidate_caches_auto, run_git, setup_fake_remote,+ invalidate_caches_auto, isolate_cmd, run_git, setup_fake_remote,};/// Benchmark configuration wrapping RepoConfig with cache state.@@ -63,10 +63,6 @@ impl BenchConfig {}}-fn release_binary() -> &'static Path {- Path::new(env!("CARGO_BIN_EXE_wt"))-}-/// Run a benchmark with the given config.fn run_benchmark(b: &mut criterion::Bencher,@@ -79,6 +75,7 @@ fn run_benchmark(let cmd_factory = || {let mut cmd = Command::new(binary);cmd.args(args).current_dir(repo_path);+ isolate_cmd(&mut cmd, None);if let Some((key, value)) = env {cmd.env(key, value);}@@ -102,7 +99,7 @@ fn run_benchmark(fn bench_skeleton(c: &mut Criterion) {let mut group = c.benchmark_group("skeleton");- let binary = release_binary();+ let binary = Path::new(env!("CARGO_BIN_EXE_wt"));for worktrees in [1, 4, 8] {for cold in [false, true] {@@ -133,7 +130,7 @@ fn bench_skeleton(c: &mut Criterion) {fn bench_full(c: &mut Criterion) {let mut group = c.benchmark_group("full");- let binary = release_binary();+ let binary = Path::new(env!("CARGO_BIN_EXE_wt"));for worktrees in [1, 4, 8] {for cold in [false, true] {@@ -157,7 +154,7 @@ fn bench_full(c: &mut Criterion) {fn bench_worktree_scaling(c: &mut Criterion) {let mut group = c.benchmark_group("worktree_scaling");- let binary = release_binary();+ let binary = Path::new(env!("CARGO_BIN_EXE_wt"));for worktrees in [1, 4, 8] {for cold in [false, true] {@@ -181,7 +178,7 @@ fn bench_worktree_scaling(c: &mut Criterion) {fn bench_real_repo(c: &mut Criterion) {let mut group = c.benchmark_group("real_repo");- let binary = release_binary();+ let binary = Path::new(env!("CARGO_BIN_EXE_wt"));for worktrees in [1, 4, 8] {for cold in [false, true] {@@ -197,25 +194,24 @@ fn bench_real_repo(c: &mut Criterion) {add_worktrees(&config, &workspace_main);run_git(&workspace_main, &["status"]);+ let make_cmd = || {+ let mut cmd = Command::new(binary);+ cmd.arg("list").current_dir(&workspace_main);+ isolate_cmd(&mut cmd, None);+ cmd+ };+if cold {b.iter_batched(|| invalidate_caches_auto(&workspace_main),|_| {- Command::new(binary)- .arg("list")- .current_dir(&workspace_main)- .output()- .unwrap();+ make_cmd().output().unwrap();},criterion::BatchSize::SmallInput,);} else {b.iter(|| {- Command::new(binary)- .arg("list")- .current_dir(&workspace_main)- .output()- .unwrap();+ make_cmd().output().unwrap();});}},@@ -228,7 +224,7 @@ fn bench_real_repo(c: &mut Criterion) {fn bench_many_branches(c: &mut Criterion) {let mut group = c.benchmark_group("many_branches");- let binary = release_binary();+ let binary = Path::new(env!("CARGO_BIN_EXE_wt"));for cold in [false, true] {let config = BenchConfig::branches(100, 2, cold);@@ -256,7 +252,7 @@ fn bench_divergent_branches(c: &mut Criterion) {group.measurement_time(std::time::Duration::from_secs(30));group.sample_size(10);- let binary = release_binary();+ let binary = Path::new(env!("CARGO_BIN_EXE_wt"));for cold in [false, true] {let config = BenchConfig::many_divergent_branches(cold);@@ -307,7 +303,7 @@ fn bench_real_repo_many_branches(c: &mut Criterion) {group.measurement_time(std::time::Duration::from_secs(60));group.sample_size(10);- let binary = release_binary();+ let binary = Path::new(env!("CARGO_BIN_EXE_wt"));// Setup function - each bench_function creates its own fresh workspace// Uses setup_rust_workspace_with_branches plus a worktree for worktrees_only test@@ -336,11 +332,11 @@ 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)- .args(["list", "--branches"])- .current_dir(&workspace_main)- .output()- .unwrap();+ let mut cmd = Command::new(binary);+ cmd.args(["list", "--branches"])+ .current_dir(&workspace_main);+ isolate_cmd(&mut cmd, None);+ cmd.output().unwrap();});});@@ -348,12 +344,12 @@ 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)- .args(["list", "--branches"])- .env("WORKTRUNK_TEST_SKIP_EXPENSIVE_THRESHOLD", "1")- .current_dir(&workspace_main)- .output()- .unwrap();+ let mut cmd = Command::new(binary);+ cmd.args(["list", "--branches"])+ .current_dir(&workspace_main);+ isolate_cmd(&mut cmd, None);+ cmd.env("WORKTRUNK_TEST_SKIP_EXPENSIVE_THRESHOLD", "1");+ cmd.output().unwrap();});});@@ -361,11 +357,10 @@ 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)- .arg("list") // no --branches- .current_dir(&workspace_main)- .output()- .unwrap();+ let mut cmd = Command::new(binary);+ cmd.arg("list").current_dir(&workspace_main); // no --branches+ isolate_cmd(&mut cmd, None);+ cmd.output().unwrap();});});diff --git a/benches/remove.rs b/benches/remove.rsindex 114ddc4e1..98c2fac21 100644--- a/benches/remove.rs+++ b/benches/remove.rs@@ -15,32 +15,7 @@use criterion::{Criterion, criterion_group, criterion_main};use std::path::{Path, PathBuf};use std::process::Command;-use worktrunk::shell_exec::Cmd;-use wt_perf::{RepoConfig, run_git, setup_fake_remote};--fn release_binary() -> &'static Path {- Path::new(env!("CARGO_BIN_EXE_wt"))-}--/// Isolate a command from host environment (mirrors test configure_cli_command).-fn isolate_cmd(cmd: &mut Command, user_config_path: &Path) {- for (key, _) in std::env::vars() {- if key.starts_with("GIT_") || key.starts_with("WORKTRUNK_") {- cmd.env_remove(&key);- }- }- cmd.env_remove("NO_COLOR");- cmd.env_remove("SHELL");- cmd.env("WORKTRUNK_CONFIG_PATH", user_config_path);- cmd.env(- "WORKTRUNK_SYSTEM_CONFIG_PATH",- "/nonexistent/bench/system-config.toml",- );- cmd.env(- "WORKTRUNK_APPROVALS_PATH",- "/nonexistent/bench/approvals.toml",- );-}+use wt_perf::{RepoConfig, isolate_cmd, run_git, run_git_ok, setup_fake_remote};/// Create a benchmark repo at a specific path with optional hooks.fn create_bench_repo(base_path: &Path, with_hooks: bool) -> PathBuf {@@ -85,20 +60,10 @@ fn recreate_worktree(repo_path: &Path) {}// Prune stale worktree metadata (best-effort)- let _ = Cmd::new("git")- .args(["worktree", "prune"])- .current_dir(repo_path)- .env("GIT_CONFIG_GLOBAL", "/dev/null")- .env("GIT_CONFIG_SYSTEM", "/dev/null")- .run();+ let _ = run_git_ok(repo_path, &["worktree", "prune"]);// Delete branch if it exists (may already be deleted by removal)- let _ = Cmd::new("git")- .args(["branch", "-D", "feature-wt-1"])- .current_dir(repo_path)- .env("GIT_CONFIG_GLOBAL", "/dev/null")- .env("GIT_CONFIG_SYSTEM", "/dev/null")- .run();+ let _ = run_git_ok(repo_path, &["branch", "-D", "feature-wt-1"]);// Recreate branch + worktreerun_git(@@ -116,7 +81,7 @@ fn recreate_worktree(repo_path: &Path) {fn bench_remove_e2e(c: &mut Criterion) {let mut group = c.benchmark_group("remove_e2e");- let binary = release_binary();+ let binary = Path::new(env!("CARGO_BIN_EXE_wt"));// Persistent temp dirs (kept alive for the benchmark group)let temp_no_hooks = tempfile::tempdir().unwrap();@@ -149,7 +114,7 @@ fn bench_remove_e2e(c: &mut Criterion) {let mut cmd = Command::new(binary);cmd.args(["remove", "--yes", "--no-verify", "--force", "feature-wt-1"]);cmd.current_dir(&repo_no_hooks);- isolate_cmd(&mut cmd, &user_config_no_hooks);+ isolate_cmd(&mut cmd, Some(&user_config_no_hooks));cmd.env("WORKTRUNK_FIRST_OUTPUT", "1");let output = cmd.output().unwrap();assert!(@@ -169,7 +134,7 @@ fn bench_remove_e2e(c: &mut Criterion) {let mut cmd = Command::new(binary);cmd.args(["remove", "--yes", "--no-verify", "--force"]);cmd.current_dir(&wt_path);- isolate_cmd(&mut cmd, &user_config_no_hooks);+ isolate_cmd(&mut cmd, Some(&user_config_no_hooks));let output = cmd.output().unwrap();assert!(output.status.success(),@@ -190,7 +155,7 @@ fn bench_remove_e2e(c: &mut Criterion) {let mut cmd = Command::new(binary);cmd.args(["remove", "--yes", "--force"]);cmd.current_dir(&wt_path);- isolate_cmd(&mut cmd, &user_config_with_hooks);+ isolate_cmd(&mut cmd, Some(&user_config_with_hooks));let output = cmd.output().unwrap();assert!(output.status.success(),diff --git a/benches/time_to_first_output.rs b/benches/time_to_first_output.rsindex 02b098414..0abdba439 100644--- a/benches/time_to_first_output.rs+++ b/benches/time_to_first_output.rs@@ -16,40 +16,35 @@use criterion::{Criterion, criterion_group, criterion_main};use std::path::Path;use std::process::Command;-use wt_perf::{RepoConfig, create_repo, setup_fake_remote};--fn release_binary() -> &'static Path {- Path::new(env!("CARGO_BIN_EXE_wt"))-}--/// Run a command and assert it succeeded.-fn run_bench_cmd(cmd: &mut Command) {- let output = cmd.output().unwrap();- assert!(- output.status.success(),- "Benchmark command failed:\nstderr: {}",- String::from_utf8_lossy(&output.stderr)- );-}+use wt_perf::{RepoConfig, create_repo, isolate_cmd, setup_fake_remote};fn bench_first_output(c: &mut Criterion) {let mut group = c.benchmark_group("first_output");- let binary = release_binary();- let env = ("WORKTRUNK_FIRST_OUTPUT", "1");+ let binary = Path::new(env!("CARGO_BIN_EXE_wt"));let config = RepoConfig::typical(4);let temp = create_repo(&config);let repo_path = temp.path().join("repo");setup_fake_remote(&repo_path);+ let make_cmd = |args: &[&str]| {+ let mut cmd = Command::new(binary);+ cmd.args(args).current_dir(&repo_path);+ isolate_cmd(&mut cmd, None);+ cmd.env("WORKTRUNK_FIRST_OUTPUT", "1");+ cmd+ };+// remove: exits after validation, before approval/outputgroup.bench_function("remove", |b| {b.iter(|| {- run_bench_cmd(- Command::new(binary)- .args(["remove", "--yes", "--no-verify", "--force", "feature-wt-1"])- .current_dir(&repo_path)- .env(env.0, env.1),+ let output = make_cmd(&["remove", "--yes", "--no-verify", "--force", "feature-wt-1"])+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "Benchmark command failed:\nstderr: {}",+ String::from_utf8_lossy(&output.stderr));});});@@ -57,11 +52,13 @@ fn bench_first_output(c: &mut Criterion) {// switch: exits after execute_switch, before mismatch computation and outputgroup.bench_function("switch", |b| {b.iter(|| {- run_bench_cmd(- Command::new(binary)- .args(["switch", "--yes", "--no-verify", "feature-wt-1"])- .current_dir(&repo_path)- .env(env.0, env.1),+ let output = make_cmd(&["switch", "--yes", "--no-verify", "feature-wt-1"])+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "Benchmark command failed:\nstderr: {}",+ String::from_utf8_lossy(&output.stderr));});});@@ -69,11 +66,11 @@ fn bench_first_output(c: &mut Criterion) {// list: exits after skeleton data collection, before rendergroup.bench_function("list", |b| {b.iter(|| {- run_bench_cmd(- Command::new(binary)- .arg("list")- .current_dir(&repo_path)- .env(env.0, env.1),+ let output = make_cmd(&["list"]).output().unwrap();+ assert!(+ output.status.success(),+ "Benchmark command failed:\nstderr: {}",+ String::from_utf8_lossy(&output.stderr));});});diff --git a/tests/helpers/wt-perf/src/lib.rs b/tests/helpers/wt-perf/src/lib.rsindex fca5fd2c0..d99f8c543 100644--- a/tests/helpers/wt-perf/src/lib.rs+++ b/tests/helpers/wt-perf/src/lib.rs@@ -4,6 +4,7 @@//! - Benchmark repository setup (used by `benches/list.rs`, `benches/time_to_first_output.rs`)//! - Cache invalidation for cold benchmark runs//! - Trace analysis utilities+//! - Shared benchmark helpers (`isolate_cmd`, `run_git_ok`)//!//! # Library Usage//!@@ -114,7 +115,7 @@ impl RepoConfig {}}-/// Run a git command in the given directory.+/// Run a git command in the given directory. Panics on failure.pub fn run_git(path: &Path, args: &[&str]) {let output = Cmd::new("git").args(args.iter().copied())@@ -133,6 +134,44 @@ pub fn run_git(path: &Path, args: &[&str]) {);}+/// Run a git command, returning whether it succeeded. Does not panic.+pub fn run_git_ok(path: &Path, args: &[&str]) -> bool {+ Cmd::new("git")+ .args(args.iter().copied())+ .current_dir(path)+ .env("GIT_CONFIG_GLOBAL", "/dev/null")+ .env("GIT_CONFIG_SYSTEM", "/dev/null")+ .run()+ .is_ok_and(|o| o.status.success())+}++/// Strip host environment from a benchmark command for isolation.+///+/// Removes `GIT_*`, `WORKTRUNK_*`, `SHELL`, and `NO_COLOR` env vars, then sets+/// config/system/approvals paths. Pass a `user_config_path` to use a real config+/// file (e.g., with hooks); `None` points at a nonexistent path (defaults only).+pub fn isolate_cmd(cmd: &mut std::process::Command, user_config_path: Option<&Path>) {+ for (key, _) in std::env::vars() {+ if key.starts_with("GIT_") || key.starts_with("WORKTRUNK_") {+ cmd.env_remove(&key);+ }+ }+ cmd.env_remove("NO_COLOR");+ cmd.env(+ "WORKTRUNK_CONFIG_PATH",+ user_config_path.unwrap_or(Path::new("/nonexistent/bench/config.toml")),+ );+ cmd.env(+ "WORKTRUNK_SYSTEM_CONFIG_PATH",+ "/nonexistent/bench/system-config.toml",+ );+ cmd.env(+ "WORKTRUNK_APPROVALS_PATH",+ "/nonexistent/bench/approvals.toml",+ );+ cmd.env_remove("SHELL");+}+/// Create a test repository from config.////// Returns a `TempDir` containing the repo. The main worktree is at `temp.path().join("repo")`.