-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.go
182 lines (153 loc) · 5.25 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
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"errors"
"os"
"os/signal"
"runtime"
"sync"
"time"
"github.com/yarpc/yab/limiter"
"github.com/yarpc/yab/statsd"
"github.com/yarpc/yab/transport"
)
var (
errNegativeDuration = errors.New("duration cannot be negative")
errNegativeMaxReqs = errors.New("max requests cannot be negative")
)
// setGoMaxProcs sets runtime.GOMAXPROCS if the option is set
// and returns the number of GOMAXPROCS configured.
func (o BenchmarkOptions) setGoMaxProcs() int {
if o.NumCPUs > 0 {
runtime.GOMAXPROCS(o.NumCPUs)
}
return runtime.GOMAXPROCS(-1)
}
func (o BenchmarkOptions) getNumConnections(goMaxProcs int) int {
if o.Connections > 0 {
return o.Connections
}
// If the user doesn't specify a number of connections, choose a sane default.
return goMaxProcs * 2
}
func (o BenchmarkOptions) validate() error {
if o.MaxDuration < 0 {
return errNegativeDuration
}
if o.MaxRequests < 0 {
return errNegativeMaxReqs
}
return nil
}
func (o BenchmarkOptions) enabled() bool {
// By default, benchmarks are disabled. At least MaxDuration or MaxRequests
// should not be 0 for the benchmark to start.
// We guard for negative values in the options validate() method, called
// after entering the benchmark case.
return o.MaxDuration != 0 || o.MaxRequests != 0
}
func runWorker(t transport.Transport, m benchmarkMethod, s *benchmarkState, run *limiter.Run) {
for cur := run; cur.More(); {
latency, err := m.call(t)
if err != nil {
s.recordError(err)
continue
}
s.recordLatency(latency)
}
}
func runBenchmark(out output, allOpts Options, m benchmarkMethod) {
opts := allOpts.BOpts
if err := opts.validate(); err != nil {
out.Fatalf("Invalid benchmarking options: %v", err)
}
if !opts.enabled() {
return
}
if opts.RPS > 0 && opts.MaxDuration > 0 {
// The RPS * duration in seconds may cap opts.MaxRequests.
rpsMax := int(float64(opts.RPS) * opts.MaxDuration.Seconds())
if rpsMax < opts.MaxRequests || opts.MaxRequests == 0 {
opts.MaxRequests = rpsMax
}
}
goMaxProcs := opts.setGoMaxProcs()
numConns := opts.getNumConnections(goMaxProcs)
out.Printf("Benchmark parameters:\n")
out.Printf(" CPUs: %v\n", goMaxProcs)
out.Printf(" Connections: %v\n", numConns)
out.Printf(" Concurrency: %v\n", opts.Concurrency)
out.Printf(" Max requests: %v\n", opts.MaxRequests)
out.Printf(" Max duration: %v\n", opts.MaxDuration)
out.Printf(" Max RPS: %v\n", opts.RPS)
// Warm up number of connections.
connections, err := m.WarmTransports(numConns, allOpts.TOpts, opts.WarmupRequests)
if err != nil {
out.Fatalf("Failed to warmup connections for benchmark: %v", err)
}
statter, err := statsd.NewClient(opts.StatsdHostPort, allOpts.TOpts.ServiceName, allOpts.ROpts.Procedure)
if err != nil {
out.Fatalf("Failed to create statsd client for benchmark: %v", err)
}
var wg sync.WaitGroup
states := make([]*benchmarkState, len(connections)*opts.Concurrency)
for i := range states {
states[i] = newBenchmarkState(statter)
}
run := limiter.New(opts.MaxRequests, opts.RPS, opts.MaxDuration)
stopOnInterrupt(out, run)
start := time.Now()
for i, c := range connections {
for j := 0; j < opts.Concurrency; j++ {
state := states[i*opts.Concurrency+j]
wg.Add(1)
go func(c transport.Transport) {
defer wg.Done()
runWorker(c, m, state, run)
}(c)
}
}
// TODO: Support streaming updates.
// Wait for all the worker goroutines to end.
wg.Wait()
total := time.Since(start)
// Merge all the states into 0
overall := states[0]
for _, s := range states[1:] {
overall.merge(s)
}
overall.printErrors(out)
overall.printLatencies(out)
out.Printf("Elapsed time: %v\n", (total / time.Millisecond * time.Millisecond))
out.Printf("Total requests: %v\n", overall.totalRequests)
out.Printf("RPS: %.2f\n", float64(overall.totalRequests)/total.Seconds())
}
// stopOnInterrupt sets up a signal that will trigger the run to stop.
func stopOnInterrupt(out output, r *limiter.Run) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
// Preceding newline since Ctrl-C will be printed inline.
out.Printf("\n!!Benchmark interrupted!!\n")
r.Stop()
}()
}