mirror of
https://github.com/peco/peco.git
synced 2026-09-10 07:16:29 -04:00
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% |
30 lines
507 B
Go
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")
|
|
})
|
|
}
|
|
}
|