open-nomad/demo/digitalocean/app/bench.go

115 lines
1.9 KiB
Go
Raw Normal View History

2015-09-26 16:27:40 +00:00
package main
import (
"fmt"
"io/ioutil"
"os"
2015-09-26 16:39:19 +00:00
"os/exec"
"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
}
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()
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
}
start := time.Now()
2015-09-26 16:39:19 +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++
}
}
if last != running {
2015-09-26 16:27:40 +00:00
fmt.Printf("%d running after %s\n", running, now.Sub(start))
}
last = running
2015-09-26 16:27:40 +00:00
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 = 10
memory = 10
}
}
}
}
`