testing: Revise usage of b.N and b.Loop() in benchmarks

This commit is contained in:
Bjørn Erik Pedersen
2025-11-06 16:22:02 +01:00
parent 91eac9e573
commit 4c7a78f5ca
20 changed files with 128 additions and 736 deletions
+6 -5
View File
@@ -152,17 +152,18 @@ func TestIndirectInterface(t *testing.T) {
}
func BenchmarkIsContextType(b *testing.B) {
const size = 1000
type k string
b.Run("value", func(b *testing.B) {
ctx := context.Background()
ctxs := make([]reflect.Type, b.N)
for i := 0; b.Loop(); i++ {
ctxs := make([]reflect.Type, size)
for i := range size {
ctxs[i] = reflect.TypeOf(context.WithValue(ctx, k("key"), i))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if !IsContextType(ctxs[i]) {
for i := 0; b.Loop(); i++ {
idx := i % size
if !IsContextType(ctxs[idx]) {
b.Fatal("not context")
}
}