-
Notifications
You must be signed in to change notification settings - Fork 35
/
bench_test.go
224 lines (179 loc) · 5.18 KB
/
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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package beehive
import (
"flag"
"io/ioutil"
"log"
"runtime"
"strconv"
"testing"
"time"
)
var (
benchmarkEndToEndBees = flag.Int("bench.e2e.bees", runtime.NumCPU(),
"number of bees in end-to-end benchmarks")
)
func benchmarkEndToEnd(b *testing.B, name string, hives int, emittingHive int,
handler Handler, app ...AppOption) {
// Warm up.
b.StopTimer()
log.SetOutput(ioutil.Discard)
bees := *benchmarkEndToEndBees
kch := make(chan benchKill)
var hs []Hive
for i := 0; i < hives; i++ {
var h Hive
if i == 0 {
h = newHiveForTest()
} else {
h = newHiveForTest(PeerAddrs(hs[0].(*hive).config.Addr))
}
a := h.NewApp("handler", app...)
a.Handle(BenchMsg(0), handler)
a.Handle(benchKill{}, benchKillHandler{ch: kch})
go h.Start()
waitTilStareted(h)
hs = append(hs, h)
}
mainHive := hs[0]
for i := 1; i <= bees; i++ {
mainHive.Emit(BenchMsg(i))
mainHive.Emit(benchKill{BenchMsg: BenchMsg(i)})
<-kch
}
b.StartTimer()
emitting := hs[emittingHive]
for i := 1; i <= bees; i++ {
for j := 0; j < b.N/bees; j++ {
emitting.Emit(BenchMsg(i))
}
emitting.Emit(benchKill{BenchMsg: BenchMsg(i)})
}
for i := 0; i < bees; i++ {
<-kch
}
b.StopTimer()
// Grace period for raft.
time.Sleep(time.Duration(len(hs)) * time.Second)
}
func BenchmarkEndToEndTransientNoOp(b *testing.B) {
benchmarkEndToEnd(b, "tx-noop", 1, 0, benchNoOpHandler{},
NonTransactional())
}
func BenchmarkEndToEndTransientBytes(b *testing.B) {
benchmarkEndToEnd(b, "tx-bytes", 1, 0, benchBytesHandler{},
NonTransactional())
}
func BenchmarkEndToEndTransientGob(b *testing.B) {
benchmarkEndToEnd(b, "tx-gob", 1, 0, benchGobHandler{}, NonTransactional())
}
func BenchmarkEndToEndTransactionalNoOp(b *testing.B) {
benchmarkEndToEnd(b, "tx-noop", 1, 0, benchNoOpHandler{}, Transactional())
}
func BenchmarkEndToEndTransactionalBytes(b *testing.B) {
benchmarkEndToEnd(b, "tx-bytes", 1, 0, benchBytesHandler{},
Transactional())
}
func BenchmarkEndToEndTransactionalGob(b *testing.B) {
benchmarkEndToEnd(b, "tx-gob", 1, 0, benchGobHandler{}, Transactional())
}
func BenchmarkEndToEndPersistentNoOp(b *testing.B) {
benchmarkEndToEnd(b, "p-noop", 1, 0, benchNoOpHandler{}, Persistent(1))
}
func BenchmarkEndToEndPersistentBytes(b *testing.B) {
benchmarkEndToEnd(b, "p-bytes", 1, 0, benchBytesHandler{}, Persistent(1))
}
func BenchmarkEndToEndPersistentGob(b *testing.B) {
benchmarkEndToEnd(b, "p-gob", 1, 0, benchGobHandler{}, Persistent(1))
}
func BenchmarkEndToEndReplicatedNoOp(b *testing.B) {
benchmarkEndToEnd(b, "r-noop", 3, 0, benchNoOpHandler{}, Persistent(3))
}
func BenchmarkEndToEndReplicatedBytes(b *testing.B) {
benchmarkEndToEnd(b, "r-bytes", 3, 0, benchBytesHandler{}, Persistent(3))
}
func BenchmarkEndToEndReplicatedGob(b *testing.B) {
benchmarkEndToEnd(b, "r-gob", 3, 0, benchGobHandler{}, Persistent(3))
}
func BenchmarkEndToEndRemoteTransactionalNoOp(b *testing.B) {
benchmarkEndToEnd(b, "rt-noop", 3, 2, benchNoOpHandler{}, Transactional())
}
func BenchmarkEndToEndRemoteTransactionalBytes(b *testing.B) {
benchmarkEndToEnd(b, "rt-bytes", 3, 2, benchBytesHandler{},
Transactional())
}
func BenchmarkEndToEndRemoteTransactionalGob(b *testing.B) {
benchmarkEndToEnd(b, "rt-gob", 3, 2, benchGobHandler{}, Transactional())
}
func BenchmarkEndToEndRemotePersistentNoOp(b *testing.B) {
benchmarkEndToEnd(b, "rp-noop", 3, 2, benchNoOpHandler{}, Persistent(3))
}
func BenchmarkEndToEndRemotePersistentBytes(b *testing.B) {
benchmarkEndToEnd(b, "rp-bytes", 3, 2, benchBytesHandler{}, Persistent(3))
}
func BenchmarkEndToEndRemotePersistentGob(b *testing.B) {
benchmarkEndToEnd(b, "rp-gob", 3, 2, benchGobHandler{}, Persistent(3))
}
type BenchMsg int
func (m BenchMsg) key() string {
return strconv.Itoa(int(m))
}
func keyForTestBenchMsg(m Msg) string {
return m.Data().(BenchMsg).key()
}
const (
benchDict = "dict"
benchShards = 16
)
type benchNoOpHandler struct{}
func (h benchNoOpHandler) Rcv(msg Msg, ctx RcvContext) error {
return nil
}
func (h benchNoOpHandler) Map(msg Msg, ctx MapContext) MappedCells {
return benchMap(msg, ctx)
}
type benchBytesHandler struct{}
func (h benchBytesHandler) Rcv(msg Msg, ctx RcvContext) error {
dict := ctx.Dict(benchDict)
k := keyForTestBenchMsg(msg)
v, err := dict.Get(k)
cnt := uint32(0)
if err == nil {
cnt = v.(uint32)
}
cnt++
return dict.Put(k, cnt)
}
func (h benchBytesHandler) Map(msg Msg, ctx MapContext) MappedCells {
return benchMap(msg, ctx)
}
type benchGobHandler struct{}
func (h benchGobHandler) Rcv(msg Msg, ctx RcvContext) error {
dict := ctx.Dict(benchDict)
k := keyForTestBenchMsg(msg)
cnt := uint32(0)
v, err := dict.Get(k)
if err == nil {
cnt = v.(uint32)
}
cnt++
return dict.Put(k, cnt)
}
func (h benchGobHandler) Map(msg Msg, ctx MapContext) MappedCells {
return benchMap(msg, ctx)
}
func benchMap(msg Msg, ctx MapContext) MappedCells {
return MappedCells{{benchDict, keyForTestBenchMsg(msg)}}
}
type benchKill struct {
BenchMsg
}
type benchKillHandler struct {
ch chan benchKill
}
func (h benchKillHandler) Rcv(msg Msg, ctx RcvContext) error {
h.ch <- benchKill{}
return nil
}
func (h benchKillHandler) Map(msg Msg, ctx MapContext) MappedCells {
return MappedCells{{benchDict, msg.Data().(benchKill).key()}}
}