forked from robaho/go-concurrency-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps_test.go
155 lines (142 loc) · 3.33 KB
/
maps_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package go_concurrency_test
import (
"fmt"
"github.com/robaho/go-concurrency-test"
"sync"
"sync/atomic"
"testing"
"time"
)
const NGOS = 2 // number of concurrent go routines for read/load tests
const Mask = (1024 * 1024) - 1
var um = go_concurrency.NewUnsharedCache()
var lm = go_concurrency.NewLockCache()
var sm = go_concurrency.NewSyncCache()
var cm = go_concurrency.NewChannelCache()
var sc = go_concurrency.NewShardCache()
var ssc = go_concurrency.NewSharedShardCache()
var im = go_concurrency.NewIntMap(256000) // so there are 4x collisions
var im2 = go_concurrency.NewIntMap(1000000) // so there are no collisions
var sim = go_concurrency.NewSharedIntMap(1000000) // so there are no collisions
var Sink atomic.Value
func rand(r int) int {
/* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
r ^= r << 13
r ^= r >> 17
r ^= r << 5
return r & 0x7fffffff
}
func TestNewSharedIntMap(t *testing.T) {
for i := 0; i < 2000000; i++ {
sim.Put(i, i)
}
for i := 0; i < 2000000; i++ {
if sim.Get(i) != i {
t.Fatal("wrong value")
}
}
}
func BenchmarkRand(m *testing.B) {
r := time.Now().Nanosecond()
for i := 0; i < m.N; i++ {
r = rand(r)
}
Sink.Store(r)
}
func testget(impl go_concurrency.Cache, b *testing.B) {
r := time.Now().Nanosecond()
var sum int
for i := 0; i < b.N; i++ {
r = rand(r)
sum += impl.Get(r & Mask)
}
Sink.Store(sum)
}
func testput(impl go_concurrency.Cache, b *testing.B) {
r := time.Now().Nanosecond()
for i := 0; i < b.N; i++ {
r = rand(r)
impl.Put(r&Mask, r)
}
}
func testputget(impl go_concurrency.Cache, b *testing.B) {
r := time.Now().Nanosecond()
var sum int
for i := 0; i < b.N; i++ {
r = rand(r)
impl.Put(r&Mask, r)
r = rand(r)
sum += impl.Get(r & Mask)
}
Sink.Store(sum)
}
func BenchmarkMain(m *testing.B) {
fmt.Println("populating maps...")
for i := 0; i <= Mask; i++ {
um.Put(i, i)
lm.Put(i, i)
sm.Put(i, i)
cm.Put(i, i)
sc.Put(i, i)
ssc.Put(i, i)
im.Put(i, i)
im2.Put(i, i)
sim.Put(i, i)
}
sm.Get(100)
m.ResetTimer()
impls := []go_concurrency.Cache{um, lm, sm, cm, sc, ssc, im, im2, sim}
names := []string{"unshared", "lock", "sync", "channel", "shard", "shareshard", "intmap", "intmap2", "sharedint"}
multi := []bool{false, true, true, true, false, true, true, true, true}
//impls := []go_concurrency.Cache{lm,sm}
//names := []string{"lock","sync"}
//multi := []bool{true,true}
for i := 0; i < len(impls); i++ {
impl := impls[i]
m.Run(names[i]+".get", func(b *testing.B) {
testget(impl, b)
})
m.Run(names[i]+".put", func(b *testing.B) {
testput(impl, b)
})
m.Run(names[i]+".putget", func(b *testing.B) {
testputget(impl, b)
})
m.Run(names[i]+".multiget", func(b *testing.B) {
wg := sync.WaitGroup{}
for g := 0; g < NGOS; g++ {
wg.Add(1)
go func() {
testget(impl, b)
wg.Done()
}()
}
wg.Wait()
})
if !multi[i] { // some impl do not support concurrent write
continue
}
m.Run(names[i]+".multiput", func(b *testing.B) {
wg := sync.WaitGroup{}
for g := 0; g < NGOS; g++ {
wg.Add(1)
go func() {
testput(impl, b)
wg.Done()
}()
}
wg.Wait()
})
m.Run(names[i]+".multiputget", func(b *testing.B) {
wg := sync.WaitGroup{}
for g := 0; g < NGOS; g++ {
wg.Add(1)
go func() {
testputget(impl, b)
wg.Done()
}()
}
wg.Wait()
})
}
}