-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
157 lines (138 loc) · 3.89 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
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
package main
import (
"crypto/rand"
"fmt"
"math/big"
"os"
"strings"
"sync"
"time"
"github.com/codegangsta/cli"
docker "github.com/fsouza/go-dockerclient"
"github.com/montanaflynn/stats"
)
const MILLIS_IN_SECOND = 1000
func worker(name string, id, requests int, image string, labels map[string]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()
containerName := fmt.Sprintf("%s-%d", name, i+(requests*id))
container, err := client.CreateContainer(docker.CreateContainerOptions{
Name: containerName,
Config: &docker.Config{
Image: image,
Cmd: args,
Labels: labels,
},
HostConfig: &docker.HostConfig{
PublishAllPorts: true,
},
})
if err != nil {
panic(err)
}
err = client.StartContainer(container.ID, nil)
if err != nil {
panic(err)
}
completeCh <- time.Since(start)
}
}
func session(name string, requests, concurrency int, images []string, labels map[string]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(i int) {
worker(name, i, n, image, labels, args, completeCh)
wg.Done()
}(i)
}
wg.Wait()
}
func bench(requests, concurrency int, images []string, labels map[string]string, args []string) {
id, _ := rand.Int(rand.Reader, big.NewInt(0xffffff))
name := fmt.Sprintf("swarm-bench-%X", id)
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(name, requests, concurrency, images, labels, 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.",
},
cli.StringSliceFlag{
Name: "label, l",
Value: &cli.StringSlice{},
Usage: "Label(s) to apply to containers.",
},
}
app.Action = func(c *cli.Context) {
if !c.IsSet("image") && !c.IsSet("i") {
cli.ShowAppHelp(c)
os.Exit(1)
}
var labels map[string]string
if c.IsSet("l") || c.IsSet("label") {
labels = map[string]string{}
for _, l := range c.StringSlice("label") {
parts := strings.SplitN(l, "=", 2)
if len(parts) != 2 {
panic("Malformed label " + l)
}
labels[parts[0]] = parts[1]
}
}
bench(c.Int("requests"), c.Int("concurrency"), c.StringSlice("image"), labels, c.Args())
}
app.Run(os.Args)
}