From 69164dea8f2210a5c06b9c42e84fd0b625ca3701 Mon Sep 17 00:00:00 2001 From: wuyangfan <1102042793@qq.com> Date: Sat, 30 May 2026 13:06:00 +0800 Subject: [PATCH] test: fix BenchmarkNova clone setup Clone nova once over HTTPS, use b.Cleanup for teardown, and measure only WalkDir in the benchmark loop. Fixes #16 --- find_replace_test.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/find_replace_test.go b/find_replace_test.go index 9a54564..5617372 100644 --- a/find_replace_test.go +++ b/find_replace_test.go @@ -660,27 +660,30 @@ func withWorkingDir(t *testing.T, dir string) { t.Cleanup(func() { _ = os.Chdir(prev) }) } -func CloneRepoToTestDir(b *testing.B, repoUrl string) *File { +func cloneRepoToBenchDir(b *testing.B, repoURL string) *File { b.Helper() - d := newTestDir(b, "", "*") - defer os.Remove(d.Path) + d := newTestDir(b, "", "bench") + b.Cleanup(func() { _ = os.RemoveAll(d.Path) }) - cmd := exec.Command("git", "clone", "--depth=1", "--single-branch", repoUrl, ".") + cmd := exec.Command("git", "clone", "--depth=1", "--single-branch", repoURL, ".") cmd.Dir = d.Path out, err := cmd.CombinedOutput() if err != nil { - b.Errorf("failed to clone repo: %s", out) + b.Fatalf("failed to clone repo: %s", out) } return d } func BenchmarkNova(b *testing.B) { + const repoURL = "https://github.com/openstack/nova.git" + + b.StopTimer() + d := cloneRepoToBenchDir(b, repoURL) + fr := findReplace{find: RandomString(2), replace: RandomString(2)} + b.ResetTimer() + for n := 0; n < b.N; n++ { - b.StopTimer() - d := CloneRepoToTestDir(b, "git@github.com:openstack/nova.git") - fr := findReplace{find: RandomString(2), replace: RandomString(2)} - b.StartTimer() fr.WalkDir(d) } }