forked from aluzzardi/swarm-bench
/
main.go
128 lines (110 loc) · 3.02 KB
/
main.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
package main
import (
"fmt"
"os"
"sync"
"time"
"github.com/codegangsta/cli"
docker "github.com/fsouza/go-dockerclient"
"github.com/montanaflynn/stats"
)
const MILLIS_IN_SECOND = 1000
func worker(requests int, image string, args []string, completeCh chan time.Duration) {
client, err := docker.NewClientFromEnv()
if err != nil {
panic(err)
}
for i := 0; i < requests; i++ {
start := time.Now()
container, err := client.CreateContainer(docker.CreateContainerOptions{
Config: &docker.Config{
Image: image,
Cmd: args,
}})
if err != nil {
panic(err)
}
err = client.StartContainer(container.ID, nil)
if err != nil {
panic(err)
}
completeCh <- time.Since(start)
}
}
func session(requests, concurrency int, images []string, args []string, completeCh chan time.Duration) {
var wg sync.WaitGroup
var size = len(images)
n := requests / concurrency
for i := 0; i < concurrency; i++ {
wg.Add(1)
image := images[i%size]
go func() {
worker(n, image, args, completeCh)
wg.Done()
}()
}
wg.Wait()
}
func bench(requests, concurrency int, images []string, args []string) {
start := time.Now()
timings := make([]float64, requests)
// Create a buffered channel so our display goroutine can't slow down the workers.
completeCh := make(chan time.Duration, requests)
doneCh := make(chan struct{})
current := 0
go func() {
for timing := range completeCh {
timings = append(timings, timing.Seconds())
current++
percent := float64(current) / float64(requests) * 100
fmt.Printf("[%3.f%%] %d/%d containers started\n", percent, current, requests)
}
doneCh <- struct{}{}
}()
session(requests, concurrency, images, args, completeCh)
close(completeCh)
<-doneCh
total := time.Since(start)
mean, _ := stats.Mean(timings)
p90th, _ := stats.Percentile(timings, 90)
p99th, _ := stats.Percentile(timings, 99)
meanMillis := mean * MILLIS_IN_SECOND
p90thMillis := p90th * MILLIS_IN_SECOND
p99thMillis := p99th * MILLIS_IN_SECOND
fmt.Printf("\n")
fmt.Printf("Time taken for tests: %.3fs\n", total.Seconds())
fmt.Printf("Time per container: %.3fms [mean] | %.3fms [90th] | %.3fms [99th]\n", meanMillis, p90thMillis, p99thMillis)
}
func main() {
app := cli.NewApp()
app.Name = "swarm-bench"
app.Usage = "Swarm Benchmarking Tool"
app.Version = "0.1.0"
app.Author = ""
app.Email = ""
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "concurrency, c",
Value: 1,
Usage: "Number of multiple requests to perform at a time. Default is one request at a time.",
},
cli.IntFlag{
Name: "requests, n",
Value: 1,
Usage: "Number of containers to start for the benchmarking session. The default is to just start a single container.",
},
cli.StringSliceFlag{
Name: "image, i",
Value: &cli.StringSlice{},
Usage: "Image(s) to use for benchmarking.",
},
}
app.Action = func(c *cli.Context) {
if !c.IsSet("image") && !c.IsSet("i") {
cli.ShowAppHelp(c)
os.Exit(1)
}
bench(c.Int("requests"), c.Int("concurrency"), c.StringSlice("image"), c.Args())
}
app.Run(os.Args)
}