Skip to content

Commit cd1154f

Browse files
committed
commit performance on test improvements
1 parent 52638c9 commit cd1154f

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

tests/performance.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::io::Write;
88
use std::path::PathBuf;
99
use std::process::Command;
1010
use std::sync::OnceLock;
11-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
11+
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
1212

1313
mod repos;
1414
use git_ai::observability::wrapper_performance_targets::BenchmarkResult;
@@ -625,49 +625,56 @@ impl Sampler {
625625
self.sample_with_setup(
626626
test_repo,
627627
|repo| {
628-
// Default setup: Reset to clean state before each run (not timed)
628+
// Optimized setup: Since each test commits its changes, the working tree
629+
// is clean after each run. We just need to get back to the default branch.
629630

630-
// 1. Clean any untracked files and directories
631-
repo.git_og(&["clean", "-fd"])
632-
.expect("Clean should succeed");
631+
let setup_start = Instant::now();
633632

634-
// 2. Reset --hard to clean any changes
635-
repo.git_og(&["reset", "--hard"])
636-
.expect("Reset --hard should succeed");
637-
638-
// 3. Get the default branch from the remote
639-
// Try to get the symbolic ref for origin/HEAD to find the default branch
633+
// 1. Get the default branch (fast - just reading refs)
640634
let default_branch = repo
641-
.git(&["symbolic-ref", "refs/remotes/origin/HEAD"])
635+
.git_og(&["symbolic-ref", "refs/remotes/origin/HEAD"])
642636
.ok()
643637
.and_then(|output| {
644-
// Extract branch name from "refs/remotes/origin/main"
645638
output
646639
.trim()
647640
.strip_prefix("refs/remotes/origin/")
648641
.map(|b| b.to_string())
649642
})
650643
.unwrap_or_else(|| {
651-
// Fallback: try main, then master
652-
if repo.git(&["rev-parse", "--verify", "main"]).is_ok() {
644+
if repo.git_og(&["rev-parse", "--verify", "main"]).is_ok() {
653645
"main".to_string()
654646
} else {
655647
"master".to_string()
656648
}
657649
});
658650

659-
// 4. Checkout the default branch
660-
repo.git(&["checkout", &default_branch])
651+
// 2. Get current branch name to delete it later (if it's a test branch)
652+
let current_branch = repo
653+
.git_og(&["branch", "--show-current"])
654+
.ok()
655+
.map(|s| s.trim().to_string());
656+
657+
// 3. Checkout default branch with force to discard any uncommitted changes
658+
repo.git_og(&["checkout", "-f", &default_branch])
661659
.expect(&format!("Checkout {} should succeed", default_branch));
662660

661+
// 4. Delete the old test branch if it was a test-bench branch
662+
if let Some(branch) = current_branch {
663+
if branch.starts_with("test-bench/") {
664+
let _ = repo.git_og(&["branch", "-D", &branch]);
665+
}
666+
}
667+
663668
// 5. Create a new branch with timestamp for isolation
664669
let timestamp_nanos = SystemTime::now()
665670
.duration_since(UNIX_EPOCH)
666671
.expect("Time went backwards")
667672
.as_nanos();
668673
let branch_name = format!("test-bench/{}", timestamp_nanos);
669-
repo.git(&["checkout", "-b", &branch_name])
674+
repo.git_og(&["checkout", "-b", &branch_name])
670675
.expect("Create branch should succeed");
676+
677+
println!("Time taken to setup: {:?}", setup_start.elapsed());
671678
},
672679
operation,
673680
)

0 commit comments

Comments
 (0)