diff options
Diffstat (limited to 'workerpool/workerpool_bench_test.go')
| -rw-r--r-- | workerpool/workerpool_bench_test.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/workerpool/workerpool_bench_test.go b/workerpool/workerpool_bench_test.go new file mode 100644 index 0000000..1fbc672 --- /dev/null +++ b/workerpool/workerpool_bench_test.go | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | package workerpool | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | "fmt" | ||
| 6 | "testing" | ||
| 7 | ) | ||
| 8 | |||
| 9 | type benchTask struct { | ||
| 10 | id string | ||
| 11 | } | ||
| 12 | |||
| 13 | func (t *benchTask) Execute(_ context.Context) error { return nil } | ||
| 14 | func (t *benchTask) GetID() string { return t.id } | ||
| 15 | |||
| 16 | func BenchmarkWorkerPool(b *testing.B) { | ||
| 17 | b.ReportAllocs() | ||
| 18 | |||
| 19 | pool, _ := NewWorkerPool[*benchTask](b.Context(), | ||
| 20 | WithNumWorkers(4), | ||
| 21 | WithTaskQueueSize(256), | ||
| 22 | ) | ||
| 23 | pool.Start() | ||
| 24 | |||
| 25 | task := &benchTask{id: "bench"} | ||
| 26 | |||
| 27 | for b.Loop() { | ||
| 28 | pool.Push(task) | ||
| 29 | } | ||
| 30 | |||
| 31 | pool.StopAndWait() | ||
| 32 | } | ||
| 33 | |||
| 34 | func BenchmarkWorkerPool_Push(b *testing.B) { | ||
| 35 | b.ReportAllocs() | ||
| 36 | |||
| 37 | pool, _ := NewWorkerPool[*benchTask](b.Context(), | ||
| 38 | WithNumWorkers(8), | ||
| 39 | WithTaskQueueSize(1024), | ||
| 40 | ) | ||
| 41 | pool.Start() | ||
| 42 | defer pool.StopAndWait() | ||
| 43 | |||
| 44 | b.RunParallel(func(pb *testing.PB) { | ||
| 45 | task := &benchTask{id: fmt.Sprintf("bench-%d", b.N)} | ||
| 46 | for pb.Next() { | ||
| 47 | pool.Push(task) | ||
| 48 | } | ||
| 49 | }) | ||
| 50 | } | ||
