fat-contexts-article-companion/bench_test.go

55 lines
970 B
Go
Raw Normal View History

2024-03-17 11:33:29 +01:00
package main_test
import (
"context"
"fmt"
"testing"
2024-03-18 10:10:31 +01:00
"math/rand/v2"
2024-03-17 11:33:29 +01:00
)
const key = "key"
func BenchmarkContext(b *testing.B) {
benchmarks := []struct {
times uint64
}{
2024-03-18 10:10:31 +01:00
{10},
{100},
2024-03-17 11:33:29 +01:00
{1_000},
{10_000},
{100_000},
}
for _, bm := range benchmarks {
ctx := context.WithValue(context.Background(), key, "some value")
b.Run(fmt.Sprintf("shadow %v", bm.times), func(b *testing.B) {
for i := 0; i < b.N; i++ {
shadow(ctx, bm.times)
}
})
b.Run(fmt.Sprintf("fat %v", bm.times), func(b *testing.B) {
for i := 0; i < b.N; i++ {
fat(ctx, bm.times)
}
})
}
}
func shadow(ctx context.Context, times uint64) {
for range times {
ctx := contextWithRandom(ctx)
_ = ctx.Value(key)
}
}
func fat(ctx context.Context, times uint64) {
for range times {
ctx = contextWithRandom(ctx)
_ = ctx.Value(key)
}
}
func contextWithRandom(ctx context.Context) context.Context {
return context.WithValue(ctx, "other_key", rand.Int64())
}