-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathtimingwheel_benchmark_test.go
74 lines (64 loc) · 1.44 KB
/
timingwheel_benchmark_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
package timingwheel_test
import (
"testing"
"time"
"github.com/RussellLuo/timingwheel"
)
func genD(i int) time.Duration {
return time.Duration(i%10000) * time.Millisecond
}
func BenchmarkTimingWheel_StartStop(b *testing.B) {
tw := timingwheel.NewTimingWheel(time.Millisecond, 20)
tw.Start()
defer tw.Stop()
cases := []struct {
name string
N int // the data size (i.e. number of existing timers)
}{
{"N-1m", 1000000},
{"N-5m", 5000000},
{"N-10m", 10000000},
}
for _, c := range cases {
b.Run(c.name, func(b *testing.B) {
base := make([]*timingwheel.Timer, c.N)
for i := 0; i < len(base); i++ {
base[i] = tw.AfterFunc(genD(i), func() {})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
tw.AfterFunc(time.Second, func() {}).Stop()
}
b.StopTimer()
for i := 0; i < len(base); i++ {
base[i].Stop()
}
})
}
}
func BenchmarkStandardTimer_StartStop(b *testing.B) {
cases := []struct {
name string
N int // the data size (i.e. number of existing timers)
}{
{"N-1m", 1000000},
{"N-5m", 5000000},
{"N-10m", 10000000},
}
for _, c := range cases {
b.Run(c.name, func(b *testing.B) {
base := make([]*time.Timer, c.N)
for i := 0; i < len(base); i++ {
base[i] = time.AfterFunc(genD(i), func() {})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
time.AfterFunc(time.Second, func() {}).Stop()
}
b.StopTimer()
for i := 0; i < len(base); i++ {
base[i].Stop()
}
})
}
}