-
Notifications
You must be signed in to change notification settings - Fork 0
/
conp.go
104 lines (87 loc) · 1.56 KB
/
conp.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
package main
import (
"fmt"
"sync"
"testing"
"github.com/xba/stress"
)
const (
// number of goroutines to use
N = 100
// zipf seed
SEED = 245250
)
func Once(f func()) func(*testing.B) {
return func(b *testing.B) {
for n := 0; n < b.N; n++ {
f()
}
}
}
func Many(f func()) func(*testing.B) {
return func(b *testing.B) {
wg := sync.WaitGroup{}
for i := 0; i < N; i++ {
wg.Add(1)
go func() {
for n := 0; n < b.N; n++ {
f()
}
wg.Done()
}()
}
wg.Wait()
}
}
func Zipf() *stress.Trace {
return stress.NewSimulator(stress.GenerateZipf(
1.1,
1,
0xffffffffffffffff,
SEED))
}
func Iden(k interface{}, err error) int {
if err != nil {
panic(err)
}
return int(k.(uint64))
}
func main() {
// zipf
{
zipf := Zipf()
fmt.Println("zipf_create", testing.Benchmark(Once(func() {
Zipf()
})))
fmt.Println("zipf_next", testing.Benchmark(Once(func() {
zipf.Next()
})))
}
// default map
{
data := make(map[int]interface{})
zipf := Zipf()
fmt.Println("map_default_get", testing.Benchmark(Once(func() {
_ = data[Iden(zipf.Next())]
})))
fmt.Println("map_default_put", testing.Benchmark(Once(func() {
data[Iden(zipf.Next())] = nil
})))
}
// mutex map
{
data := make(map[int]interface{})
mute := sync.Mutex{}
zipf := Zipf()
fmt.Println("map_mutex_get", testing.Benchmark(Many(func() {
mute.Lock()
_ = data[Iden(zipf.Next())]
mute.Unlock()
})))
fmt.Println("map_mutex_put", testing.Benchmark(Many(func() {
mute.Lock()
data[Iden(zipf.Next())] = nil
mute.Unlock()
})))
}
}