forked from ethereum/hive
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbenchmarker.go
276 lines (246 loc) · 8.96 KB
/
benchmarker.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
package main
import (
"errors"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/fsouza/go-dockerclient"
"gopkg.in/inconshreveable/log15.v2"
)
// benchmarkResult represents the results of a benchmark run, containing
// various metadata.
type benchmarkResult struct {
Start time.Time `json:"start"` // Time instance when the benchmark ended
End time.Time `json:"end"` // Time instance when the benchmark ended
Success bool `json:"success"` // Whether the entire benchmark succeeded
Error error `json:"error,omitempty"` // Potential hive failure during benchmark
Iterations int `json:"iterations,omitempty"` // Number of benchmark iterations made
NsPerOp int64 `json:"ns/op,omitempty"` // Nanoseconds spend per single iteration
}
// benchmarkClients runs a batch of benchmark tests matched by benchmarkerPattern
// against all clients matching clientPattern.
func benchmarkClients(daemon *docker.Client, clientPattern, benchmarkerPattern string, overrides []string) (map[string]map[string]*benchmarkResult, error) {
// Build all the clients matching the benchmark pattern
log15.Info("building clients for benchmark", "pattern", clientPattern)
clients, err := buildClients(daemon, clientPattern)
if err != nil {
return nil, err
}
// Build all the benchmarkers known to the harness
log15.Info("building benchmarkers for measurements", "pattern", benchmarkerPattern)
benchmarkers, err := buildBenchmarkers(daemon, benchmarkerPattern)
if err != nil {
return nil, err
}
// Iterate over all client and benchmarker combos and cross-execute them
results := make(map[string]map[string]*benchmarkResult)
for client, clientImage := range clients {
results[client] = make(map[string]*benchmarkResult)
for benchmarker, benchmarkerImage := range benchmarkers {
// Create the logger and log folder
logger := log15.New("client", client, "benchmarker", benchmarker)
logdir := filepath.Join(hiveLogsFolder, "benchmarks", fmt.Sprintf("%s[%s]", strings.Replace(benchmarker, "/", ":", -1), client))
os.RemoveAll(logdir)
// Wrap the benchmark code into the Go's testing framework
var result *benchmarkResult
report := testing.Benchmark(func(b *testing.B) {
if result = benchmark(daemon, clientImage, benchmarkerImage, overrides, logger, logdir, b); !result.Success {
b.Fatalf("benchmark failed")
}
})
result.Iterations = report.N
result.NsPerOp = report.NsPerOp()
results[client][benchmarker] = result
}
}
return results, nil
}
func benchmark(daemon *docker.Client, client, benchmarker string, overrides []string, logger log15.Logger, logdir string, b *testing.B) *benchmarkResult {
logger.Info("running client benchmark", "iterations", b.N)
result := &benchmarkResult{
Start: time.Now(),
}
defer func() { result.End = time.Now() }()
// Create the client container and make sure it's cleaned up afterwards
logger.Debug("creating client container")
cc, err := createClientContainer(daemon, client, benchmarker, nil, overrides, nil)
if err != nil {
logger.Error("failed to create client", "error", err)
result.Error = err
return result
}
clogger := logger.New("id", cc.ID[:8])
clogger.Debug("created client container")
defer func() {
clogger.Debug("deleting client container")
daemon.RemoveContainer(docker.RemoveContainerOptions{ID: cc.ID, Force: true})
}()
// Start the client container and retrieve its IP address for the benchmarker
clogger.Debug("running client container")
cwaiter, err := runContainer(daemon, cc.ID, clogger, filepath.Join(logdir, "client.log"), false)
if err != nil {
clogger.Error("failed to run client", "error", err)
result.Error = err
return result
}
defer cwaiter.Close()
lcc, err := daemon.InspectContainer(cc.ID)
if err != nil {
clogger.Error("failed to retrieve client IP", "error", err)
result.Error = err
return result
}
cip := lcc.NetworkSettings.IPAddress
// Wait for the HTTP/RPC socket to open or the container to fail
start := time.Now()
for {
// If the container died, bail out
c, err := daemon.InspectContainer(cc.ID)
if err != nil {
clogger.Error("failed to inspect client", "error", err)
result.Error = err
return result
}
if !c.State.Running {
clogger.Error("client container terminated")
result.Error = errors.New("terminated unexpectedly")
return result
}
// Container seems to be alive, check whether the RPC is accepting connections
if conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", c.NetworkSettings.IPAddress, 8545)); err == nil {
clogger.Debug("client container online", "time", time.Since(start))
conn.Close()
break
}
time.Sleep(100 * time.Millisecond)
}
// Start the benchmark API server to provide access to the benchmark oracle
bench, err := startBenchmarkerAPI(logger, b)
if err != nil {
logger.Error("failed to start benchmarker API", "error", err)
result.Error = err
return result
}
defer bench.Close()
// Create the benchmarker container and make sure it's cleaned up afterwards
logger.Debug("creating benchmarker container")
vc, err := daemon.CreateContainer(docker.CreateContainerOptions{
Config: &docker.Config{
Image: benchmarker,
Env: []string{
"HIVE_CLIENT_IP=" + cip,
"HIVE_BENCHMARKER=http://" + bench.listener.Addr().String(),
"HIVE_BENCHMARKER_ITERS=" + strconv.Itoa(b.N),
},
},
})
if err != nil {
logger.Error("failed to create benchmarker", "error", err)
result.Error = err
return result
}
blogger := logger.New("id", vc.ID[:8])
blogger.Debug("created benchmarker container")
defer func() {
blogger.Debug("deleting benchmarker container")
daemon.RemoveContainer(docker.RemoveContainerOptions{ID: vc.ID, Force: true})
}()
// Start the tester container and wait until it finishes
blogger.Debug("running benchmarker container")
b.ResetTimer()
bwaiter, err := runContainer(daemon, vc.ID, blogger, filepath.Join(logdir, "benchmarker.log"), false)
if err != nil {
blogger.Error("failed to run benchmarker", "error", err)
result.Error = err
return result
}
bwaiter.Wait()
b.StopTimer()
// Retrieve the exist status to report pass of fail
v, err := daemon.InspectContainer(vc.ID)
if err != nil {
blogger.Error("failed to inspect benchmarker", "error", err)
result.Error = err
return result
}
result.Success = v.State.ExitCode == 0
return result
}
// startBenchmarkerAPI starts an HTTP webserver listening for benchmarker commands
// on the docker bridge and executing them until it is torn down.
func startBenchmarkerAPI(logger log15.Logger, b *testing.B) (*benchmarkerAPIHandler, error) {
// Find the IP address of the host container
logger.Debug("looking up docker bridge IP")
bridge, err := lookupBridgeIP(logger)
if err != nil {
logger.Error("failed to lookup bridge IP", "error", err)
return nil, err
}
logger.Debug("docker bridge IP found", "ip", bridge)
// Start a tiny API webserver for benchmarkers to coordinate with
logger.Debug("opening TCP socket for benchmarker")
addr, _ := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:0", bridge))
listener, err := net.ListenTCP("tcp4", addr)
if err != nil {
logger.Error("failed to listen on bridge adapter", "error", err)
return nil, err
}
logger.Debug("listening for benchmarker commands", "ip", bridge, "port", listener.Addr().(*net.TCPAddr).Port)
// Serve connections until the listener is terminated
logger.Debug("starting benchmarker API server")
api := &benchmarkerAPIHandler{
listener: listener,
oracle: b,
logger: logger,
}
go http.Serve(listener, api)
return api, nil
}
// benchmarkerAPIHandler is the HTTP request handler directing the docker engine
// with the commands from the benchmarker runner.
type benchmarkerAPIHandler struct {
listener *net.TCPListener
oracle *testing.B
logger log15.Logger
autoID uint32
}
// ServeHTTP handles all the benchmarker API requests and executes them.
func (h *benchmarkerAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger := h.logger.New("req-id", atomic.AddUint32(&h.autoID, 1))
logger.Debug("new benchmarker request", "from", r.RemoteAddr, "method", r.Method, "endpoint", r.URL.Path)
switch r.Method {
case "GET":
switch r.URL.Path {
case "/iters":
// Return the number of iterations required for this run
fmt.Fprintf(w, "%d", h.oracle.N)
default:
http.Error(w, "not found", http.StatusNotFound)
}
case "POST":
switch r.URL.Path {
case "/reset":
// Restart the benchmark measurements, discarding all counters and timers
h.oracle.ResetTimer()
case "/stop":
// Terminates the measurements but allows cleanups to still run
h.oracle.StopTimer()
default:
http.Error(w, "not found", http.StatusNotFound)
}
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
// Close terminates all running containers and tears down the API server.
func (h *benchmarkerAPIHandler) Close() {
h.logger.Debug("terminating benchmarker server")
h.listener.Close()
}