/
run.go
345 lines (314 loc) · 8.96 KB
/
run.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
345
package main
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/mitchellh/cli"
"github.com/pkg/errors"
"github.com/schattian/bencher/internal/bencher"
"go.etcd.io/bbolt"
)
type runCmd struct {
docker *client.Client
}
func (cmd *runCmd) Run(args []string) int {
args, version := popNameFlag(args)
if version == "" {
version = namesgenerator.GetRandomName(0)
fmt.Printf("version name not given, using `%s`. To give a version name use the `-name` flag\n", version)
}
args, image := popImageFlag(args)
if image == "" {
image = minDockerImage
}
ctx := context.Background()
err := errors.Wrap(cmd.prepareRuntime(ctx, version, args, image), "prepareRuntime")
if err != nil {
fmt.Printf("err %v", err)
return 1
}
err = errors.Wrap(runServerCmd(ctx, cmd.docker, []string{"sched", version}, os.Getenv("BENCHER_DEBUG") != ""), "runServerCmd")
if err != nil {
fmt.Printf("err %v", err)
return 1
}
return 0
}
func popFlagBoolean(args []string, flagName string) ([]string, bool) {
singleFlagName := fmt.Sprintf("-%s", flagName)
doubleFlagName := fmt.Sprintf("-%s", singleFlagName)
for i, arg := range args {
if arg == singleFlagName || arg == doubleFlagName {
if len(args) <= i+1 {
return args[:i], true
}
return append(args[:i], args[i+1:]...), true
}
}
return args, false
}
func popFlagWithVal(args []string, flagName string) ([]string, string) {
pivot := -2
singleFlagName := fmt.Sprintf("-%s", flagName)
doubleFlagName := fmt.Sprintf("-%s", singleFlagName)
for i, arg := range args {
if arg == singleFlagName || arg == doubleFlagName {
pivot = i
}
if i == pivot+1 {
return append(args[:i-1], args[i+1:]...), arg
}
if strings.HasPrefix(arg, fmt.Sprintf("%s=", doubleFlagName)) {
val := arg[len(flagName)+3:] // --=
if len(args) <= i+1 {
return args[:i], val
}
return append(args[:i], args[i+1:]...), val
}
if strings.HasPrefix(arg, fmt.Sprintf("%s=", singleFlagName)) {
val := arg[len(flagName)+2:] // -=
if len(args) <= i+1 {
return args[:i], val
}
return append(args[:i], args[i+1:]...), val
}
}
return args, ""
}
func popNameFlag(args []string) ([]string, string) {
return popFlagWithVal(args, "name")
}
func popImageFlag(args []string) ([]string, string) {
return popFlagWithVal(args, "image")
}
func pruneContainers(ctx context.Context, docker *client.Client) error {
args := filters.NewArgs(filters.Arg("label", fmt.Sprintf("%s=server", bencher.ContainersLabel)))
_, err := docker.ContainersPrune(ctx, args)
if err != nil {
return err
}
return nil
}
func (cmd *runCmd) prepareRuntime(ctx context.Context, version string, forward []string, image string) error {
if _, err := os.Stat(bencher.HostServerRootPath); os.IsNotExist(err) {
fmt.Println("preparing bencher runtime, this could take a while as it's your first time...")
}
err := os.MkdirAll(bencher.HostServerRootPath, os.ModePerm)
if err != nil {
return errors.Wrap(err, "MkdirAll")
}
wd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "Getwd")
}
root, err := getModPath()
if err != nil {
return errors.Wrap(err, "getModPath")
}
// adding filepath.Base(root) could be useful as a prefix
versionPath := filepath.Join(bencher.HostVersionsPath, version)
err = os.MkdirAll(versionPath, os.ModePerm)
if err != nil {
return err
}
execCmd := exec.Command("rsync", "-a", root+"/", versionPath, "--exclude", ".git")
err = execCmd.Run()
if err != nil {
return errors.Wrap(err, "rsync")
}
execCmd = exec.Command("go", "mod", "vendor")
execCmd.Dir = versionPath
err = execCmd.Run()
if err != nil {
return errors.Wrap(err, "go mod vendor")
}
// todo: add go version by module on this path
r, err := cmd.docker.ImagePull(ctx, image, types.ImagePullOptions{})
if err != nil {
return err
}
defer r.Close()
_, err = io.Copy(io.Discard, r)
if err != nil {
return errors.Wrap(err, "io.Copy")
}
if len(forward) == 0 || (len(forward) == 1 && forward[0] == ".") { // . is alias of nothing since we run it in wd
// fmt.Printf("command not given, using the default one (`go test -bench=. -benchmem`). To give a command just use args\n")
forward = defaultCmd
}
err = createContainer(ctx, cmd.docker, version, versionPath, forward, wd[len(root):], image)
if err != nil {
return errors.Wrap(err, "createContainer")
}
db, err := initDB() // just used to ensure db fs is reachable by the client
defer db.Close()
if err != nil {
return errors.Wrap(err, "initDB")
}
return nil
}
func initDB() (*bbolt.DB, error) {
return bbolt.Open(bencher.HostDBFilename, 0600, bbolt.DefaultOptions)
}
func runServerCmd(ctx context.Context, docker *client.Client, cmd []string, debug bool) error {
containerName := fmt.Sprintf("%s_%s", bencher.ContainersLabel, namesgenerator.GetRandomName(0))
r, err := docker.ContainerCreate(
ctx,
&container.Config{
Image: bencher.ServerImage,
Env: []string{"CGO_ENABLED=0"},
Labels: map[string]string{bencher.ContainersLabel: "server"},
WorkingDir: bencher.ServerRootPath,
Volumes: map[string]struct{}{
defaultUnixSocket: {},
bencher.HostServerRootPath: {},
},
Cmd: cmd,
AttachStdout: debug,
AttachStderr: debug,
},
&container.HostConfig{
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: defaultUnixSocket,
Target: defaultUnixSocket,
},
{
Type: mount.TypeBind,
Source: bencher.HostServerRootPath,
Target: bencher.ServerRootPath,
},
},
},
nil,
nil,
containerName,
)
if isNoSuchImage(err) { // todo: easier invalidation
r, err := docker.ImagePull(ctx, bencher.ServerImage, types.ImagePullOptions{})
if err != nil {
return err
}
defer r.Close()
_, err = io.Copy(io.Discard, r)
if err != nil {
return errors.Wrap(err, "io.Copy")
}
return runServerCmd(ctx, docker, cmd, debug)
}
if isContainerExists(err) {
return runServerCmd(ctx, docker, cmd, debug)
}
if err != nil {
return errors.Wrap(err, "create")
}
err = docker.ContainerStart(ctx, r.ID, types.ContainerStartOptions{})
if err != nil {
return errors.Wrap(err, "start")
}
if debug {
wait, errCh := docker.ContainerWait(ctx, r.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
return errors.Wrap(err, "wait")
case <-wait:
}
}
return nil
}
func isNoSuchImage(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "No such image")
}
func isContainerExists(err error) bool {
if err == nil {
return false
}
return strings.HasPrefix(err.Error(), "Conflict. The container name")
}
// This code is heavily inspired from golang/go/src/go/build/build.go
func getModPath() (string, error) {
parent, err := os.Getwd()
if err != nil {
return "", errors.New("not using modules")
}
for {
if f, err := os.Open(filepath.Join(parent, "go.mod")); err == nil {
buf := make([]byte, 100)
_, err := f.Read(buf)
f.Close()
if err == nil || err == io.EOF {
return parent, nil
}
}
d := filepath.Dir(parent)
if len(d) >= len(parent) {
return "", errors.New("not using modules")
}
parent = d
}
}
func createContainer(ctx context.Context, docker *client.Client, version, versionPath string, cmd []string, wd string, image string) error {
_, err := docker.ContainerCreate(
ctx,
&container.Config{
Image: image,
Env: []string{"CGO_ENABLED=0"}, // TODO
Labels: map[string]string{bencher.ContainersLabel: "runner"},
WorkingDir: bencher.RunnerRootPath + wd,
Entrypoint: strslice.StrSlice{""},
Volumes: map[string]struct{}{
versionPath: {},
},
Cmd: cmd,
},
&container.HostConfig{
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: versionPath,
Target: bencher.RunnerRootPath,
},
},
},
nil,
nil,
version,
)
if err != nil {
return err
}
return nil
}
func prepareRun() (cli.Command, error) {
docker, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, err
}
pruneContainers(context.Background(), docker)
return &runCmd{docker: docker}, nil
}
func (cmd *runCmd) Synopsis() string {
return `schedule a benchmark to be ran`
}
func (cmd *runCmd) Help() string {
return `Usage: bencher run [--name] [--image] [go test command]
Schedule a benchmark to be run, given by the [go test command], and being "go test-bench=. -benchmem" the default value
You can pass whichever flag you want to the [go test command] (e.g: bencher run go test -bench=^Regex -benchtime=5m -benchmem -v -run=^$)
Consider that the command uses the current working directory and that can also be ran over subdirectories
If [--image] given, you can run under the specified docker image (default: golang alpine, targeted by digest)`
}