-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress_bench_test.go
106 lines (82 loc) · 1.77 KB
/
progress_bench_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
101
102
103
104
105
106
package dspc
import (
"fmt"
"io"
"sync"
"sync/atomic"
"testing"
)
func BenchmarkSingleThreaded(b *testing.B) {
keys := []string{
"key-1", "key-2", "key-3", "key-4", "key-5", "key-6",
"key-7", "key-8", "key-9", "key-10", "key-11", "key-12",
}
b.Run("Map", func(b *testing.B) {
m := make(map[string]int64)
b.ReportAllocs()
for i := range b.N {
key := keys[i%len(keys)]
m[key]++
}
})
b.Run("Progress", func(b *testing.B) {
var progress Progress
b.ReportAllocs()
for i := range b.N {
key := keys[i%len(keys)]
progress.Inc(key, 1)
}
})
}
func BenchmarkMultiThreaded(b *testing.B) {
keys := []string{
"key-1", "key-2", "key-3", "key-4", "key-5", "key-6",
"key-7", "key-8", "key-9", "key-10", "key-11", "key-12",
}
b.Run("Map", func(b *testing.B) {
m := make(map[string]int64)
var mu sync.Mutex
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for i := 0; pb.Next(); i++ {
key := keys[i%len(keys)]
mu.Lock()
m[key]++
mu.Unlock()
}
})
})
b.Run("Progress", func(b *testing.B) {
var progress Progress
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for i := 0; pb.Next(); i++ {
key := keys[i%len(keys)]
progress.Inc(key, 1)
}
})
})
// each goroutine uses its own key
b.Run("Progress w disjoint keys", func(b *testing.B) {
var progress Progress
b.ReportAllocs()
i := int64(-1)
b.RunParallel(func(pb *testing.PB) {
key := keys[int(atomic.AddInt64(&i, 1))%len(keys)]
for pb.Next() {
progress.Inc(key, 1)
}
})
})
}
func BenchmarkPrinting(b *testing.B) {
var progress Progress
for i := range 10 {
key := fmt.Sprintf("key-%d", i)
progress.Inc(key, 1)
}
b.ReportAllocs()
for range b.N {
progress.prettyPrint(io.Discard, "Test progress:", true)
}
}