peco.peco/hub/bench_test.go
Daisuke Maki e2bc390c68 fix: merge two context keys into one in Hub.Batch
Reduce context.WithValue calls from 2 to 1 per Batch invocation.

| Metric     | Before | After | Delta |
|------------|--------|-------|-------|
| allocs/op  | 3      | 2     | -1    |
| B/op       | 128    | 80    | -48   |
| ns/op      | ~372   | ~352  | -5%   |
2026-02-21 22:20:25 +09:00

30 lines
507 B
Go

package hub_test
import (
"context"
"testing"
"github.com/peco/peco/hub"
)
// BenchmarkHubBatch measures the allocation cost of Hub.Batch context setup.
func BenchmarkHubBatch(b *testing.B) {
h := hub.New(5)
ctx := context.Background()
// Drain query channel and call Done() so batch sends unblock
go func() {
for p := range h.QueryCh() {
p.Done()
}
}()
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
h.Batch(ctx, func(bctx context.Context) {
h.SendQuery(bctx, "test")
})
}
}