strings: add benchmarks for Replace

This commit is contained in:
Julien Cretel 2025-03-14 13:24:33 +01:00
parent f41fdd962d
commit 85a0db545d
No known key found for this signature in database
GPG Key ID: 9BC102DCCA7031A9
1 changed files with 16 additions and 0 deletions

View File

@ -1473,6 +1473,10 @@ var ReplaceTests = []struct {
func TestReplace(t *testing.T) {
for _, tt := range ReplaceTests {
allocs := testing.AllocsPerRun(10, func() { Replace(tt.in, tt.old, tt.new, tt.n) })
if allocs > 1 {
t.Errorf("Replace(%q, %q, %q, %d) allocates %.2f objects", tt.in, tt.old, tt.new, tt.n, allocs)
}
if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
}
@ -1531,6 +1535,18 @@ func FuzzReplace(f *testing.F) {
})
}
func BenchmarkReplace(b *testing.B) {
for _, tt := range ReplaceTests {
desc := fmt.Sprintf("%q %q %q %d", tt.in, tt.old, tt.new, tt.n)
b.Run(desc, func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
Replace(tt.in, tt.old, tt.new, tt.n)
}
})
}
}
var TitleTests = []struct {
in, out string
}{