-
Notifications
You must be signed in to change notification settings - Fork 5
/
wrk3.go
218 lines (185 loc) · 4.94 KB
/
wrk3.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
package wrk3
import (
"context"
"flag"
"fmt"
"log"
"sync"
"time"
"github.com/HdrHistogram/hdrhistogram-go"
"golang.org/x/time/rate"
)
type RequestFunc func() error
type Benchmark struct {
Concurrency int
Throughput float64
Duration time.Duration
SendRequest RequestFunc
}
type BenchResult struct {
Throughput float64
Counter int
Errors int
Omitted int
Latency *hdrhistogram.Histogram
TotalTime time.Duration
}
// BenchmarkCmd is a main function helper that runs the provided target function using the commandline arguments
func BenchmarkCmd(target RequestFunc) {
var concurrency int
var throughput float64
var duration time.Duration
flag.IntVar(&concurrency, "concurrency", 10, "level of benchmark concurrency")
flag.Float64Var(&throughput, "throughput", 10000, "target benchmark throughput")
flag.DurationVar(&duration, "duration", 20*time.Second, "benchmark time period")
flag.Parse()
fmt.Printf("running benchmark for %v...\n", duration)
b := Benchmark{
Concurrency: concurrency,
Throughput: throughput,
Duration: duration,
SendRequest: target,
}
result := b.Run()
PrintBenchResult(throughput, duration, result)
}
type localResult struct {
errors int
counter int
latency *hdrhistogram.Histogram
}
type executioner struct {
eventsGenerator
benchmark Benchmark
results chan localResult
startTime time.Time
}
type eventsGenerator struct {
lock sync.Mutex
eventsBuf chan time.Time
doneCtx context.Context
cancel context.CancelFunc
// omitted value is valid only after the execution is done
omitted int
}
func (b Benchmark) Run() BenchResult {
execution := b.newExecution()
execution.generateEvents(b.Throughput, 2*b.Concurrency)
for i := 0; i < b.Concurrency; i++ {
go execution.sendRequests()
}
execution.awaitDone()
return execution.summarizeResults()
}
func (b Benchmark) newExecution() *executioner {
return &executioner{
eventsGenerator: newEventsGenerator(b.Duration, int(b.Throughput*10) /*10 sec buffer*/),
benchmark: b,
results: make(chan localResult, b.Concurrency),
startTime: time.Now(),
}
}
func newEventsGenerator(duration time.Duration, bufSize int) eventsGenerator {
doneCtx, cancel := context.WithTimeout(context.Background(), duration)
return eventsGenerator{
lock: sync.Mutex{},
doneCtx: doneCtx,
cancel: cancel,
eventsBuf: make(chan time.Time, bufSize),
}
}
func (e *eventsGenerator) generateEvents(throughput float64, burstSize int) {
go func() {
omitted := 0
rateLimiter := rate.NewLimiter(rate.Limit(throughput), burstSize)
for err := rateLimiter.Wait(e.doneCtx); err == nil; err = rateLimiter.Wait(e.doneCtx) {
select {
case e.eventsBuf <- time.Now():
default:
omitted++
}
}
close(e.eventsBuf)
e.lock.Lock()
e.omitted = omitted
e.lock.Unlock()
}()
}
func (e *eventsGenerator) omittedCount() int {
e.lock.Lock()
defer e.lock.Unlock()
return e.omitted
}
func (e *eventsGenerator) awaitDone() {
<-e.doneCtx.Done()
e.cancel()
}
func (e *executioner) sendRequests() {
res := localResult{
errors: 0,
counter: 0,
latency: createHistogram(),
}
done := false
for !done {
select {
case <-e.doneCtx.Done():
done = true
case t, ok := <-e.eventsBuf:
if ok {
res.counter++
err := e.benchmark.SendRequest()
if err != nil {
res.errors++
}
if err = res.latency.RecordValue(int64(time.Since(t))); err != nil {
log.Println("failed to record latency", err)
}
} else {
done = true
}
}
}
e.results <- res
}
func (e *executioner) summarizeResults() BenchResult {
counter := 0
errors := 0
latency := createHistogram()
for i := 0; i < e.benchmark.Concurrency; i++ {
localRes := <-e.results
counter += localRes.counter
errors += localRes.errors
latency.Merge(localRes.latency)
}
totalTime := time.Since(e.startTime)
return BenchResult{
Throughput: float64(counter) / totalTime.Seconds(),
Counter: counter,
Errors: errors,
Omitted: e.omittedCount(),
Latency: latency,
TotalTime: totalTime,
}
}
func createHistogram() *hdrhistogram.Histogram {
return hdrhistogram.New(0, int64(time.Minute), 3)
}
func PrintBenchResult(throughput float64, duration time.Duration, result BenchResult) {
fmt.Println("benchmark results:")
fmt.Println("total duration: ", result.TotalTime, "(target duration:", duration, ")")
fmt.Println("total requests: ", result.Counter)
fmt.Println("errors: ", result.Errors)
fmt.Println("omitted requests: ", result.Omitted)
fmt.Println("throughput: ", result.Throughput, "(target throughput:", throughput, ")")
fmt.Println("latency distribution:")
printHistogram(result.Latency)
}
func printHistogram(hist *hdrhistogram.Histogram) {
brackets := hist.CumulativeDistribution()
fmt.Println("Quantile | Count | Value ")
fmt.Println("------------+-----------+-------------")
for _, q := range brackets {
fmt.Printf("%-08.3f | %-09d | %v\n", q.Quantile, q.Count, time.Duration(q.ValueAt))
}
}