Files
worker/pool_bench_test.go

45 lines
714 B
Go

package worker
import (
"runtime"
"sync/atomic"
"testing"
"time"
)
func BenchmarkSubmitNoop(b *testing.B) {
benchmarkSubmit(b, func() {})
}
func BenchmarkSubmit10usTask(b *testing.B) {
benchmarkSubmit(b, func() {
deadline := time.Now().Add(10 * time.Microsecond)
for time.Now().Before(deadline) {
}
})
}
func benchmarkSubmit(b *testing.B, task func()) {
p, err := New(6, 6)
if err != nil {
b.Fatal(err)
}
var completed atomic.Int64
wrapped := func() {
task()
completed.Add(1)
}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if err := p.Submit(wrapped); err != nil {
b.Fatal(err)
}
}
for completed.Load() != int64(b.N) {
runtime.Gosched()
}
b.StopTimer()
p.Close()
}