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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

118 lines
1.9 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2015-09-26 16:27:40 +00:00
package main
import (
"fmt"
"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
}
2017-09-26 22:26:33 +00:00
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 := os.CreateTemp("", "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)
2015-09-27 21:12:23 +00:00
opts := &api.QueryOptions{AllowStale: true}
for {
2015-09-26 16:27:40 +00:00
time.Sleep(100 * time.Millisecond)
2015-09-27 21:12:23 +00:00
allocs, _, err := allocClient.List(opts)
2015-09-26 16:27:40 +00:00
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 = 100
memory = 100
}
}
}
}
`