github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/demo/digitalocean/app/bench.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "os/exec" 8 "strconv" 9 "time" 10 11 "github.com/hashicorp/nomad/api" 12 "github.com/hashicorp/nomad/nomad/structs" 13 ) 14 15 func main() { 16 client, err := api.NewClient(api.DefaultConfig()) 17 if err != nil { 18 fmt.Println(err.Error()) 19 return 20 } 21 22 total := 0 23 if len(os.Args) != 2 { 24 fmt.Println("need 1 arg") 25 return 26 } 27 28 if total, err = strconv.Atoi(os.Args[1]); err != nil { 29 fmt.Println("arg 1 must be number") 30 return 31 } 32 33 fh, err := ioutil.TempFile("", "bench") 34 if err != nil { 35 fmt.Println(err.Error()) 36 return 37 } 38 defer os.Remove(fh.Name()) 39 40 jobContent := fmt.Sprintf(job, total) 41 if _, err := fh.WriteString(jobContent); err != nil { 42 fmt.Println(err.Error()) 43 return 44 } 45 fh.Close() 46 47 isRunning := false 48 allocClient := client.Allocations() 49 50 cmd := exec.Command("nomad", "run", fh.Name()) 51 if err := cmd.Run(); err != nil { 52 fmt.Println("nomad run failed: " + err.Error()) 53 return 54 } 55 start := time.Now() 56 57 last := 0 58 fmt.Printf("benchmarking %d allocations\n", total) 59 opts := &api.QueryOptions{AllowStale: true} 60 for { 61 time.Sleep(100 * time.Millisecond) 62 63 allocs, _, err := allocClient.List(opts) 64 if err != nil { 65 fmt.Println(err.Error()) 66 67 // keep going to paper over minor errors 68 continue 69 } 70 now := time.Now() 71 72 running := 0 73 for _, alloc := range allocs { 74 if alloc.ClientStatus == structs.AllocClientStatusRunning { 75 if !isRunning { 76 fmt.Printf("time to first running: %s\n", now.Sub(start)) 77 isRunning = true 78 } 79 running++ 80 } 81 } 82 83 if last != running { 84 fmt.Printf("%d running after %s\n", running, now.Sub(start)) 85 } 86 last = running 87 88 if running == total { 89 return 90 } 91 } 92 } 93 94 const job = ` 95 job "bench" { 96 datacenters = ["ams2", "ams3", "nyc3", "sfo1"] 97 98 group "cache" { 99 count = %d 100 101 task "redis" { 102 driver = "docker" 103 104 config { 105 image = "redis" 106 } 107 108 resources { 109 cpu = 100 110 memory = 100 111 } 112 } 113 } 114 } 115 `