2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-09-26 16:27:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-09-26 16:57:48 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2017-09-26 22:26:33 +00:00
|
|
|
var total int
|
2015-09-26 16:57:48 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-08 15:39:03 +00:00
|
|
|
fh, err := os.CreateTemp("", "bench")
|
2015-09-26 16:57:48 +00:00
|
|
|
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)
|
2015-09-27 21:12:23 +00:00
|
|
|
opts := &api.QueryOptions{AllowStale: true}
|
2015-09-26 19:38:31 +00:00
|
|
|
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++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 18:17:24 +00:00
|
|
|
cpu = 100
|
|
|
|
memory = 100
|
2015-09-26 16:57:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|