This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bench.go
344 lines (308 loc) · 8.67 KB
/
bench.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"bytes"
"context"
"crypto/rand"
"encoding/base32"
"encoding/json"
"fmt"
"io"
"log"
"os"
"path"
"path/filepath"
"strings"
"text/template"
"time"
exec "github.com/getsentry/sentry-sdk-benchmark/internal/std/execabs"
)
var dockerComposeTemplate = template.Must(template.ParseFiles(filepath.Join("template", "docker-compose.yml.tmpl")))
type BenchmarkConfig struct {
ID BenchmarkID
StartTime time.Time
Platform string // a valid path like platform/python/django
PlatformConfig PlatformConfig // from platform/*/*/config.json
Runs []RunConfig
}
type PlatformConfig struct {
Target struct {
Path string
}
RPS uint16
Duration string
MaxWait string // optional, use for platforms that are notably slow to boot
}
func (cfg PlatformConfig) Validate() error {
if cfg.Target.Path == "" {
return fmt.Errorf(`platform config missing "target.path"`)
}
if cfg.RPS == 0 {
return fmt.Errorf(`platform config missing "rps"`)
}
d, err := time.ParseDuration(cfg.Duration)
if err != nil {
return fmt.Errorf(`platform config invalid "duration": %q: %s`, cfg.Duration, err)
}
if d <= 0 {
return fmt.Errorf(`platform config nonpositive "duration": %q`, cfg.Duration)
}
if cfg.MaxWait != "" {
if _, err := time.ParseDuration(cfg.MaxWait); err != nil {
return fmt.Errorf(`platform config invalid "maxwait": %q: %s`, cfg.MaxWait, err)
}
}
return nil
}
// BenchmarkConfigFromPath returns the necessary configuration to run a
// benchmark targeting the app or apps at the given path.
//
// Path must be either a directory with a configuration file and one or more app
// directories, or path must be an app directory whose parent contains a
// configuration file.
//
// BenchmarkConfigFromPath panics if it cannot create a valid configuration from
// the given path.
//
// Path is always cleaned with filepath.Clean, such that equivalent spellings of
// the same path will return equivalent configuration.
func BenchmarkConfigFromPath(path string) BenchmarkConfig {
path = filepath.Clean(path)
if fi, err := os.Stat(path); err != nil || !fi.IsDir() {
panic(fmt.Errorf("could not read directory: %q", path))
}
pcpath := MustFindPlatformConfig(path)
cfg := BenchmarkConfig{
ID: NewBenchmarkID(),
StartTime: time.Now().UTC(),
Platform: filepath.Dir(pcpath),
PlatformConfig: MustReadPlatformConfig(pcpath),
}
var apps []string
if cfg.Platform == path {
// all apps of the platform
apps = subDirs(path)
} else {
// single app
apps = []string{path}
}
if len(apps) == 0 {
panic(fmt.Errorf("no app to benchmark in %q", path))
}
for _, app := range apps {
name := filepath.Base(app)
cfg.Runs = append(cfg.Runs, RunConfig{
Name: name,
NeedsRelay: name != "baseline",
})
}
return cfg
}
// MustFindPlatformConfig returns the path to the platform configuration for the
// given path. Path itself must contain a configuration file or path's parent
// directory must contain a configuration file. MustFindPlatformConfig panics if
// a configuration file cannot be found.
func MustFindPlatformConfig(path string) string {
candidates := []string{
filepath.Join(path, "config.json"),
filepath.Join(filepath.Dir(path), "config.json"),
}
for _, p := range candidates {
if fi, err := os.Stat(p); err == nil && !fi.IsDir() {
return p
}
}
panic(fmt.Errorf("no config file found in: %q", candidates))
}
// MustReadPlatformConfig reads and validates a PlatformConfig from path. It
// panics if the configuration cannot be read or is invalid.
func MustReadPlatformConfig(path string) PlatformConfig {
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
var pc PlatformConfig
err = json.NewDecoder(f).Decode(&pc)
if err != nil {
panic(err)
}
if err := pc.Validate(); err != nil {
panic(err)
}
return pc
}
// subDirs returns all subdirectories of path.
func subDirs(path string) []string {
var s []string
entries, err := os.ReadDir(path)
if err != nil {
panic(err)
}
for _, e := range entries {
if !e.IsDir() {
continue
}
s = append(s, e.Name())
}
return s
}
type RunConfig struct {
Name string
NeedsRelay bool
}
type DockerComposeData struct {
ID BenchmarkID
RunName string
PlatformConfig PlatformConfig
App App
ResultPath string
NeedsRelay bool
Language string
Framework string
SanityCheckMode bool
}
type App struct {
ContextPath string
Dockerfile string
HostPort int
ContainerPort int
}
type BenchmarkID [4]byte
func NewBenchmarkID() BenchmarkID {
var id BenchmarkID
_, err := rand.Read(id[:])
if err != nil {
panic(err)
}
return id
}
var base32Encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
func (r BenchmarkID) String() string {
return base32Encoding.EncodeToString(r[:])
}
func Benchmark(ctx context.Context, cfg BenchmarkConfig) {
oldprefix := log.Prefix()
defer log.SetPrefix(oldprefix)
log.SetPrefix(fmt.Sprintf("%s[%s] ", oldprefix, cfg.ID))
var results []*RunResult
for _, runCfg := range cfg.Runs {
results = append(results, run(ctx, cfg, runCfg))
}
report(results)
}
type RunResult struct {
Name string
ComposeFile []byte
Path string
}
func run(ctx context.Context, benchmarkCfg BenchmarkConfig, runCfg RunConfig) *RunResult {
oldprefix := log.Prefix()
defer log.SetPrefix(oldprefix)
log.SetPrefix(fmt.Sprintf("%s[%s] ", oldprefix, path.Join(append(strings.Split(benchmarkCfg.Platform, string(os.PathSeparator))[1:], runCfg.Name)...)))
log.Print("START")
defer log.Print("END")
language := filepath.Base(filepath.Dir(benchmarkCfg.Platform))
framework := filepath.Base(benchmarkCfg.Platform)
projectName := fmt.Sprintf("%s-%s-%s-%s", language, framework, runCfg.Name, benchmarkCfg.ID)
contextPath := path.Join(benchmarkCfg.Platform, runCfg.Name)
resultPath := path.Join(append(
strings.Split(benchmarkCfg.Platform, string(os.PathSeparator))[1:],
fmt.Sprintf("%s-%s", benchmarkCfg.StartTime.Format("20060102-150405"), benchmarkCfg.ID),
runCfg.Name,
)...)
dockerfile := findDockerfile(contextPath)
var b bytes.Buffer
err := dockerComposeTemplate.Execute(&b, DockerComposeData{
ID: benchmarkCfg.ID,
RunName: runCfg.Name,
PlatformConfig: benchmarkCfg.PlatformConfig,
App: App{
ContextPath: contextPath,
Dockerfile: dockerfile,
},
ResultPath: resultPath,
NeedsRelay: runCfg.NeedsRelay,
Language: language,
Framework: framework,
SanityCheckMode: sanityCheckMode,
})
if err != nil {
panic(err)
}
result := &RunResult{
Name: runCfg.Name,
ComposeFile: b.Bytes(),
Path: filepath.Join("result", filepath.Join(strings.Split(resultPath, "/")...)),
}
if err := os.MkdirAll(result.Path, 0777); err != nil {
panic(err)
}
if err := os.WriteFile(filepath.Join(result.Path, "docker-compose.yml"), result.ComposeFile, 0666); err != nil {
panic(err)
}
defer composeDown(projectName)
composeBuild(ctx, projectName, result.ComposeFile)
composeUp(ctx, projectName, result.ComposeFile, filepath.Join(result.Path, "docker-compose-up.log"))
return result
}
func findDockerfile(path string) string {
s, err := os.ReadDir(path)
if err != nil {
panic(err)
}
for _, entry := range s {
if !entry.IsDir() && strings.Contains(strings.ToLower(entry.Name()), "dockerfile") {
return entry.Name()
}
}
panic(fmt.Errorf("no Dockerfile in %s", path))
}
func composeBuild(ctx context.Context, projectName string, composeFile []byte) {
log.Print("Running 'docker compose build'...")
cmd := exec.CommandContext(
ctx,
"docker", "compose",
"--project-name", projectName,
"--file", "-",
"build",
)
cmd.Stdin = bytes.NewReader(composeFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
panic(err)
}
}
func composeUp(ctx context.Context, projectName string, composeFile []byte, outpath string) {
log.Printf("Running 'docker compose up', streaming logs to %q...", outpath)
out, err := os.Create(outpath)
if err != nil {
panic(err)
}
defer out.Close()
cmd := exec.CommandContext(
ctx,
"docker", "compose",
"--project-name", projectName,
"--file", "-",
"up", "--exit-code-from", "loadgen",
)
cmd.Stdin = bytes.NewReader(composeFile)
cmd.Stdout = io.MultiWriter(out, os.Stdout)
cmd.Stderr = io.MultiWriter(out, os.Stderr)
if err := cmd.Run(); err != nil {
panic(err)
}
}
func composeDown(projectName string) {
log.Print("Running 'docker compose down'...")
cmd := exec.Command(
"docker", "compose",
"--project-name", projectName,
"down", "--remove-orphans", "--rmi", "local")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
panic(err)
}
}