2015-09-26 16:27:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-09-26 16:57:48 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-09-26 16:39:19 +00:00
|
|
|
"os/exec"
|
2015-09-26 16:57:48 +00:00
|
|
|
"strconv"
|
2015-09-26 16:27:40 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2015-09-26 16:57:48 +00:00
|
|
|
total := 0
|
|
|
|
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()
|
|
|
|
|
2015-09-26 16:39:19 +00:00
|
|
|
isRunning := false
|
2015-09-26 16:27:40 +00:00
|
|
|
allocClient := client.Allocations()
|
|
|
|
|
2015-09-26 16:57:48 +00:00
|
|
|
cmd := exec.Command("nomad", "run", fh.Name())
|
2015-09-26 16:39:19 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
fmt.Println("nomad run failed: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
2015-09-26 17:50:21 +00:00
|
|
|
start := time.Now()
|
2015-09-26 16:39:19 +00:00
|
|
|
|
2015-09-26 17:50:21 +00:00
|
|
|
last := 0
|
2015-09-26 16:27:40 +00:00
|
|
|
fmt.Printf("benchmarking %d allocations\n", total)
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
allocs, _, err := allocClient.List(nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
|
|
|
|
// keep going to paper over minor errors
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
|
2015-09-26 16:39:19 +00:00
|
|
|
running := 0
|
2015-09-26 16:27:40 +00:00
|
|
|
for _, alloc := range allocs {
|
|
|
|
if alloc.ClientStatus == structs.AllocClientStatusRunning {
|
2015-09-26 16:39:19 +00:00
|
|
|
if !isRunning {
|
|
|
|
fmt.Printf("time to first running: %s\n", now.Sub(start))
|
|
|
|
isRunning = true
|
2015-09-26 16:27:40 +00:00
|
|
|
}
|
|
|
|
running++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-26 17:50:21 +00:00
|
|
|
if last != running {
|
2015-09-26 16:27:40 +00:00
|
|
|
fmt.Printf("%d running after %s\n", running, now.Sub(start))
|
|
|
|
}
|
2015-09-26 17:50:21 +00:00
|
|
|
last = running
|
2015-09-26 16:27:40 +00:00
|
|
|
|
|
|
|
if running == total {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-26 16:57:48 +00:00
|
|
|
|
|
|
|
const job = `
|
|
|
|
job "bench" {
|
|
|
|
datacenters = ["ams2", "ams3", "nyc3", "sfo1"]
|
|
|
|
|
|
|
|
group "cache" {
|
|
|
|
count = %d
|
|
|
|
|
|
|
|
task "redis" {
|
|
|
|
driver = "docker"
|
|
|
|
|
|
|
|
config {
|
|
|
|
image = "redis"
|
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
2015-09-26 17:50:21 +00:00
|
|
|
cpu = 10
|
|
|
|
memory = 10
|
2015-09-26 16:57:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|