Skip to content

Commit

Permalink
Improve Stats benchmarks setup
Browse files Browse the repository at this point in the history
```shell
$ go test -run=- -bench=Stats -count=20 | benchstat -col=/implem -
goos: linux
goarch: amd64
pkg: github.com/diegommm/adaptivepool
cpu: 13th Gen Intel(R) Core(TM) i7-13700H
         │   default   │
         │   sec/op    │
Stats-20   6.040n ± 4%

         │  default   │
         │    B/op    │
Stats-20   0.000 ± 0%

         │  default   │
         │ allocs/op  │
Stats-20   0.000 ± 0%
```
  • Loading branch information
diegommm committed Oct 28, 2024
1 parent 24868ca commit 25993e7
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions stats_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,18 @@ import "testing"
func BenchmarkStats(b *testing.B) {
// Consider running this benchmark like this for consistency with previous
// commits
// go test -run=- -bench=Stats/implem -count=20 | benchstat -col=/implem -
// go test -run=- -bench=Stats -count=20 | benchstat -col=/implem -

values := allTestDataInputValues(b)
b.Run("implem=default", benchStats(new(Stats), values))
}

func benchStats(st stats, values []float64) func(b *testing.B) {
lower := func(a, b float64) float64 {
if a < b {
return a
}
return b
}
var witness float64 // prevent the compiler from being too smart
b.Run("implem=default", func(b *testing.B) {
st := new(Stats)
b.ReportAllocs()
b.ResetTimer()

return func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, v := range values {
st.Push(v)
witness = lower(witness, st.N())
witness = lower(witness, st.Mean())
witness = lower(witness, st.StdDev())
}
st.Push(float64(i))
st.N()
st.Mean()
st.StdDev()
}
}
})
}

0 comments on commit 25993e7

Please sign in to comment.