This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_test.go
100 lines (79 loc) · 1.97 KB
/
benchmarks_test.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package wpool_test
import (
"fmt"
"sync"
"testing"
"time"
"github.com/egnd/go-wpool/v2"
"github.com/egnd/go-wpool/v2/interfaces"
"github.com/rs/zerolog"
)
type SomeTask struct {
wg *sync.WaitGroup
delay time.Duration
id string
}
func (t *SomeTask) GetID() string { return t.id }
func (t *SomeTask) Do() {
defer t.wg.Done()
if t.delay > 0 {
time.Sleep(t.delay)
}
}
func benchPipelinePool(workerCnt int, delay time.Duration, b *testing.B) {
pipeline := make(chan interfaces.Worker)
logger := wpool.NewZerologAdapter(zerolog.Nop())
pool := wpool.NewPipelinePool(pipeline, logger)
defer pool.Close()
for i := 0; i < workerCnt; i++ {
pool.AddWorker(wpool.NewPipelineWorker(pipeline))
}
var wg sync.WaitGroup
for i := 0; i < b.N; i++ {
wg.Add(1)
if err := pool.AddTask(
&SomeTask{&wg, delay, "task" + fmt.Sprint(i)},
); err != nil {
b.Error(err)
break
}
}
wg.Wait()
}
func benchStickyPool(workerCnt int, workerSize int, delay time.Duration, b *testing.B) {
logger := wpool.NewZerologAdapter(zerolog.Nop())
pool := wpool.NewStickyPool(logger)
defer pool.Close()
for i := 0; i < workerCnt; i++ {
pool.AddWorker(wpool.NewWorker(workerSize))
}
var wg sync.WaitGroup
for i := 0; i < b.N; i++ {
wg.Add(1)
if err := pool.AddTask(
&SomeTask{&wg, delay, "task" + fmt.Sprint(i)},
); err != nil {
b.Error(err)
break
}
}
wg.Wait()
}
func Benchmark_PPool_Threads1(b *testing.B) {
benchPipelinePool(1, 10*time.Millisecond, b)
}
func Benchmark_PPool_Threads10(b *testing.B) {
benchPipelinePool(10, 10*time.Millisecond, b)
}
func Benchmark_SPool_Threads1_WSize1(b *testing.B) {
benchStickyPool(1, 1, 10*time.Millisecond, b)
}
func Benchmark_SPool_Threads1_WSize10(b *testing.B) {
benchStickyPool(1, 10, 10*time.Millisecond, b)
}
func Benchmark_SPool_Threads10_WSize1(b *testing.B) {
benchStickyPool(10, 1, 10*time.Millisecond, b)
}
func Benchmark_SPool_Threads10_WSize100(b *testing.B) {
benchStickyPool(10, 10, 10*time.Millisecond, b)
}