-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcounter_benchmark_test.go
More file actions
57 lines (42 loc) · 902 Bytes
/
counter_benchmark_test.go
File metadata and controls
57 lines (42 loc) · 902 Bytes
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
package counter
import (
"sync"
"testing"
)
// basicCounter is a basic implementation of a counter.
// It's used to compare the performance to our version.
type basicCounter struct {
mutex sync.Mutex
count uint64
}
func (c *basicCounter) Increment() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.count++
}
func (c *basicCounter) Count() uint64 {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.count
}
func BenchmarkBasicCounterImplementation(b *testing.B) {
counter := basicCounter{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
counter.Increment()
}
}
func BenchmarkIncrement(b *testing.B) {
counter := NewCounter().Start()
b.ResetTimer()
for i := 0; i < b.N; i++ {
counter.Increment()
}
}
func BenchmarkIncrementWithAdvancedStats(b *testing.B) {
counter := NewCounter().WithAdvancedStats().Start()
b.ResetTimer()
for i := 0; i < b.N; i++ {
counter.Increment()
}
}