1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-28 03:57:09 +02:00

Stabilize benchmark result of BenchmarkValueEqual (#5717)

`BenchmarkValueEqual` does not run the target code with `b.N`, as
https://pkg.go.dev/testing#hdr-Benchmarks states. Thus, it cannot
produce a stable benchmark result. And, it would fail the benchmark:
https://github.com/open-telemetry/opentelemetry-go/actions/runs/10412186663.
This PR fixes this issue.

Here is the result of `benchstat`

```
goos: darwin
goarch: arm64
pkg: go.opentelemetry.io/otel/log
                       │       old.txt        │                      new.txt                       │
                       │        sec/op        │       sec/op        vs base                        │
ValueEqual/2_with_1-10   0.0000002000n ± 100%   3.1450000000n ± 1%  +1572499900.00% (p=0.000 n=10)

                       │  old.txt   │            new.txt             │
                       │    B/op    │    B/op     vs base            │
ValueEqual/2_with_1-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

                       │  old.txt   │            new.txt             │
                       │ allocs/op  │ allocs/op   vs base            │
ValueEqual/2_with_1-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal
```
This commit is contained in:
Sam Xie 2024-08-16 09:33:36 -07:00 committed by GitHub
parent d61bbf18f5
commit 5b2ce02911
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -225,7 +225,9 @@ func BenchmarkValueEqual(b *testing.B) {
for _, v2 := range vals {
b.Run(v1.String()+" with "+v2.String(), func(b *testing.B) {
b.ReportAllocs()
_ = v1.Equal(v2)
for i := 0; i < b.N; i++ {
_ = v1.Equal(v2)
}
})
}
}