forked from ailidani/paxi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.go
282 lines (246 loc) · 6.39 KB
/
benchmark.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package paxi
import (
"math"
"math/rand"
"sync"
"time"
"github.com/ailidani/paxi/log"
)
// DB is general interface implemented by client to call client library
type DB interface {
Init() error
Read(key int) (int, error)
Write(key, value int) error
Stop() error
}
// Bconfig holds all benchmark configuration
type Bconfig struct {
T int // total number of running time in seconds
N int // total number of requests
K int // key sapce
W float64 // write ratio
Throttle int // requests per second throttle, unused if 0
Concurrency int // number of simulated clients
Distribution string // distribution
LinearizabilityCheck bool // run linearizability checker at the end of benchmark
// rounds int // repeat in many rounds sequentially
// conflict distribution
Conflicts int // percentage of conflicting keys
Min int // min key
// normal distribution
Mu float64 // mu of normal distribution
Sigma float64 // sigma of normal distribution
Move bool // moving average (mu) of normal distribution
Speed int // moving speed in milliseconds intervals per key
// zipfian distribution
ZipfianS float64 // zipfian s parameter
ZipfianV float64 // zipfian v parameter
// exponential distribution
Lambda float64 // rate parameter
}
// DefaultBConfig returns a default benchmark config
func DefaultBConfig() Bconfig {
return Bconfig{
T: 60,
N: 0,
K: 1000,
W: 0.5,
Throttle: 0,
Concurrency: 1,
Distribution: "uniform",
LinearizabilityCheck: true,
Conflicts: 100,
Min: 0,
Mu: 0,
Sigma: 60,
Move: false,
Speed: 500,
ZipfianS: 2,
ZipfianV: 1,
Lambda: 0.01,
}
}
// Benchmark is benchmarking tool that generates workload and collects operation history and latency
type Benchmark struct {
db DB // read/write operation interface
Bconfig
*History
rate *Limiter
latency []time.Duration // latency per operation
startTime time.Time
zipf *rand.Zipf
counter int
wait sync.WaitGroup // waiting for all generated keys to complete
}
// NewBenchmark returns new Benchmark object given implementation of DB interface
func NewBenchmark(db DB) *Benchmark {
b := new(Benchmark)
b.db = db
b.Bconfig = config.Benchmark
b.History = NewHistory()
if b.Throttle > 0 {
b.rate = NewLimiter(b.Throttle)
}
rand.Seed(time.Now().UTC().UnixNano())
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
b.zipf = rand.NewZipf(r, b.ZipfianS, b.ZipfianV, uint64(b.K))
return b
}
// Load will create all K keys to DB
func (b *Benchmark) Load() {
b.W = 1.0
b.Throttle = 0
b.db.Init()
keys := make(chan int, b.Concurrency)
latencies := make(chan time.Duration, 1000)
defer close(latencies)
go b.collect(latencies)
b.startTime = time.Now()
for i := 0; i < b.Concurrency; i++ {
go b.worker(keys, latencies)
}
for i := b.Min; i < b.Min+b.K; i++ {
b.wait.Add(1)
keys <- i
}
t := time.Now().Sub(b.startTime)
b.db.Stop()
close(keys)
b.wait.Wait()
stat := Statistic(b.latency)
log.Infof("Benchmark took %v\n", t)
log.Infof("Throughput %f\n", float64(len(b.latency))/t.Seconds())
log.Info(stat)
}
// Run starts the main logic of benchmarking
func (b *Benchmark) Run() {
var stop chan bool
if b.Move {
move := func() { b.Mu = float64(int(b.Mu+1) % b.K) }
stop = Schedule(move, time.Duration(b.Speed)*time.Millisecond)
defer close(stop)
}
b.latency = make([]time.Duration, 0)
keys := make(chan int, b.Concurrency)
latencies := make(chan time.Duration, 1000)
defer close(latencies)
go b.collect(latencies)
for i := 0; i < b.Concurrency; i++ {
go b.worker(keys, latencies)
}
b.db.Init()
b.startTime = time.Now()
if b.T > 0 {
timer := time.NewTimer(time.Second * time.Duration(b.T))
loop:
for {
select {
case <-timer.C:
break loop
default:
b.wait.Add(1)
keys <- b.next()
}
}
} else {
for i := 0; i < b.N; i++ {
b.wait.Add(1)
keys <- b.next()
}
b.wait.Wait()
}
t := time.Now().Sub(b.startTime)
b.db.Stop()
close(keys)
stat := Statistic(b.latency)
log.Infof("Concurrency = %d", b.Concurrency)
log.Infof("Write Ratio = %f", b.W)
log.Infof("Number of Keys = %d", b.K)
log.Infof("Benchmark Time = %v\n", t)
log.Infof("Throughput = %f\n", float64(len(b.latency))/t.Seconds())
log.Info(stat)
stat.WriteFile("latency")
b.History.WriteFile("history")
if b.LinearizabilityCheck {
n := b.History.Linearizable()
if n == 0 {
log.Info("The execution is linearizable.")
} else {
log.Info("The execution is NOT linearizable.")
log.Infof("Total anomaly read operations are %d", n)
log.Infof("Anomaly percentage is %f", float64(n)/float64(stat.Size))
}
}
}
// generates key based on distribution
func (b *Benchmark) next() int {
var key int
switch b.Distribution {
case "order":
b.counter = (b.counter + 1) % b.K
key = b.counter + b.Min
case "uniform":
key = rand.Intn(b.K) + b.Min
case "conflict":
if rand.Intn(100) < b.Conflicts {
key = 0
} else {
b.counter = (b.counter + 1) % b.K
key = b.counter + b.Min
}
case "normal":
key = int(rand.NormFloat64()*b.Sigma + b.Mu)
for key < 0 {
key += b.K
}
for key > b.K {
key -= b.K
}
case "zipfan":
key = int(b.zipf.Uint64())
case "exponential":
key = int(rand.ExpFloat64() / b.Lambda)
default:
log.Fatalf("unknown distribution %s", b.Distribution)
}
if b.Throttle > 0 {
b.rate.Wait()
}
return key
}
func (b *Benchmark) worker(keys <-chan int, result chan<- time.Duration) {
var s time.Time
var e time.Time
var v int
var err error
for k := range keys {
op := new(operation)
if rand.Float64() < b.W {
v = rand.Int()
s = time.Now()
err = b.db.Write(k, v)
e = time.Now()
op.input = v
} else {
s = time.Now()
v, err = b.db.Read(k)
e = time.Now()
op.output = v
}
op.start = s.Sub(b.startTime).Nanoseconds()
if err == nil {
op.end = e.Sub(b.startTime).Nanoseconds()
result <- e.Sub(s)
} else {
op.end = math.MaxInt64
log.Error(err)
}
b.History.AddOperation(k, op)
}
}
func (b *Benchmark) collect(latencies <-chan time.Duration) {
for t := range latencies {
b.latency = append(b.latency, t)
b.wait.Done()
}
}