Skip to content

Commit

Permalink
fix(perf): Fix benchmark
Browse files Browse the repository at this point in the history
The reader was being read only once in the first iteration, and
afterwards it was always empty, thus `parseCodeowners` was parsing an
empty file.

These were the metrics before the change:

    $ go test -benchmem -bench=.
    goos: darwin
    goarch: arm64
    pkg: github.com/hairyhenderson/go-codeowners
    BenchmarkParseCodeowners-10      2677503               465.3 ns/op          4096 B/op          1 allocs/op
    PASS
    ok      github.com/hairyhenderson/go-codeowners 2.255s

After the change it's not showing more realistic data:

    $ go test -benchmem -bench=.
    goos: darwin
    goarch: arm64
    pkg: github.com/hairyhenderson/go-codeowners
    BenchmarkParseCodeowners-10        31054             38910 ns/op           61424 B/op        641 allocs/op
    PASS
    ok      github.com/hairyhenderson/go-codeowners 3.529s
  • Loading branch information
inkel authored and hairyhenderson committed Sep 27, 2024
1 parent d659b73 commit 43a0da6
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion codeowners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ func TestParseCodeownersSections(t *testing.T) {
}

func BenchmarkParseCodeowners(b *testing.B) {
r := bytes.NewBufferString(sample)
var c []Codeowner

for n := 0; n < b.N; n++ {
b.StopTimer()
r := bytes.NewBufferString(sample)
b.StartTimer()
c, _ = parseCodeowners(r)
}

Expand Down

0 comments on commit 43a0da6

Please sign in to comment.