Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions find_replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Recreate the checkout for each benchmark iteration

Because the checkout is cloned only once outside the b.N loop, the first WalkDir mutates that tree by rewriting contents and renaming paths; when b.N > 1, later iterations walk an already-transformed repository and no longer measure the same find/replace workload. This amortizes a single real replacement run across many mostly no-op walks, so the benchmark result is misleading even after the timer is restarted.

Useful? React with 👍 / 👎.

fr := findReplace{find: RandomString(2), replace: RandomString(2)}
b.ResetTimer()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restart the benchmark timer before walking

After b.StopTimer(), b.ResetTimer() only clears counters; the Go testing.B.ResetTimer docs state it does not affect whether the timer is running. As a result, every fr.WalkDir(d) call runs while the benchmark timer is still stopped, so go test -bench reports invalid near-zero timings. Call b.StartTimer() before the loop, or avoid stopping the timer and just reset it after setup.

Useful? React with 👍 / 👎.


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)
}
}