aboutsummaryrefslogtreecommitdiff
path: root/workerpool/workerpool_bench_test.go
blob: 1fbc6728b62c6edec8c9ed9b0eeb5486f613280a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package workerpool

import (
	"context"
	"fmt"
	"testing"
)

type benchTask struct {
	id string
}

func (t *benchTask) Execute(_ context.Context) error { return nil }
func (t *benchTask) GetID() string                   { return t.id }

func BenchmarkWorkerPool(b *testing.B) {
	b.ReportAllocs()

	pool, _ := NewWorkerPool[*benchTask](b.Context(),
		WithNumWorkers(4),
		WithTaskQueueSize(256),
	)
	pool.Start()

	task := &benchTask{id: "bench"}

	for b.Loop() {
		pool.Push(task)
	}

	pool.StopAndWait()
}

func BenchmarkWorkerPool_Push(b *testing.B) {
	b.ReportAllocs()

	pool, _ := NewWorkerPool[*benchTask](b.Context(),
		WithNumWorkers(8),
		WithTaskQueueSize(1024),
	)
	pool.Start()
	defer pool.StopAndWait()

	b.RunParallel(func(pb *testing.PB) {
		task := &benchTask{id: fmt.Sprintf("bench-%d", b.N)}
		for pb.Next() {
			pool.Push(task)
		}
	})
}