forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.go
115 lines (95 loc) · 1.89 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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"time"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/nomad/structs"
)
func main() {
client, err := api.NewClient(api.DefaultConfig())
if err != nil {
fmt.Println(err.Error())
return
}
var total int
if len(os.Args) != 2 {
fmt.Println("need 1 arg")
return
}
if total, err = strconv.Atoi(os.Args[1]); err != nil {
fmt.Println("arg 1 must be number")
return
}
fh, err := ioutil.TempFile("", "bench")
if err != nil {
fmt.Println(err.Error())
return
}
defer os.Remove(fh.Name())
jobContent := fmt.Sprintf(job, total)
if _, err := fh.WriteString(jobContent); err != nil {
fmt.Println(err.Error())
return
}
fh.Close()
isRunning := false
allocClient := client.Allocations()
cmd := exec.Command("nomad", "run", fh.Name())
if err := cmd.Run(); err != nil {
fmt.Println("nomad run failed: " + err.Error())
return
}
start := time.Now()
last := 0
fmt.Printf("benchmarking %d allocations\n", total)
opts := &api.QueryOptions{AllowStale: true}
for {
time.Sleep(100 * time.Millisecond)
allocs, _, err := allocClient.List(opts)
if err != nil {
fmt.Println(err.Error())
// keep going to paper over minor errors
continue
}
now := time.Now()
running := 0
for _, alloc := range allocs {
if alloc.ClientStatus == structs.AllocClientStatusRunning {
if !isRunning {
fmt.Printf("time to first running: %s\n", now.Sub(start))
isRunning = true
}
running++
}
}
if last != running {
fmt.Printf("%d running after %s\n", running, now.Sub(start))
}
last = running
if running == total {
return
}
}
}
const job = `
job "bench" {
datacenters = ["ams2", "ams3", "nyc3", "sfo1"]
group "cache" {
count = %d
task "redis" {
driver = "docker"
config {
image = "redis"
}
resources {
cpu = 100
memory = 100
}
}
}
}
`