-
Notifications
You must be signed in to change notification settings - Fork 1
/
pbench_test.go
60 lines (52 loc) · 1.09 KB
/
pbench_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
package pbench
import (
"math/rand"
"testing"
"time"
)
func BenchmarkTest(tb *testing.B) {
b := New(tb)
b.ReportPercentile(0.5)
b.ReportPercentile(0.95)
b.ReportPercentile(0.99)
b.Run("Example", func(b *B) {
rand.Seed(int64(time.Now().Nanosecond()))
b.ResetTimer()
b.RunParallel(func(pb *PB) {
for pb.Next() {
v := rand.Float64()
switch {
case v <= 0.5:
time.Sleep(10 * time.Microsecond)
case v <= 0.95:
time.Sleep(100 * time.Microsecond)
case v <= 0.99:
time.Sleep(1000 * time.Microsecond)
default:
time.Sleep(10000 * time.Microsecond)
}
}
})
})
}
func BenchmarkControl(b *testing.B) {
b.Run("Example", func(b *testing.B) {
rand.Seed(int64(time.Now().Nanosecond()))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
v := rand.Float64()
switch {
case v <= 0.5:
time.Sleep(10 * time.Microsecond)
case v <= 0.95:
time.Sleep(100 * time.Microsecond)
case v <= 0.99:
time.Sleep(1000 * time.Microsecond)
default:
time.Sleep(10000 * time.Microsecond)
}
}
})
})
}