| 1 | // Copyright © 2013, 2014, The Go-LXC Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a LGPLv2.1 |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | //go:build linux && cgo |
| 6 | // +build linux,cgo |
| 7 | |
| 8 | package main |
| 9 | |
| 10 | import ( |
| 11 | "flag" |
| 12 | "io/ioutil" |
| 13 | "log" |
| 14 | "runtime" |
| 15 | "strconv" |
| 16 | "sync" |
| 17 | |
| 18 | "github.com/lxc/go-lxc" |
| 19 | ) |
| 20 | |
| 21 | var ( |
| 22 | lxcpath string |
| 23 | iteration int |
| 24 | threads int |
| 25 | template string |
| 26 | quiet bool |
| 27 | startstop bool |
| 28 | createdestroy bool |
| 29 | ) |
| 30 | |
| 31 | func init() { |
| 32 | runtime.GOMAXPROCS(runtime.NumCPU()) |
| 33 | |
| 34 | flag.StringVar(&lxcpath, "lxcpath", lxc.DefaultConfigPath(), "Use specified container path") |
| 35 | flag.StringVar(&template, "template", "busybox", "Template to use") |
| 36 | flag.IntVar(&threads, "threads", 10, "Number of operations to run concurrently") |
| 37 | flag.IntVar(&iteration, "iteration", 1, "Number times to run the test") |
| 38 | flag.BoolVar(&quiet, "quiet", false, "Don't produce any output") |
| 39 | flag.BoolVar(&startstop, "startstop", false, "Flag to execute Start and Stop") |
| 40 | flag.BoolVar(&createdestroy, "createdestroy", false, "Flag to execute Create and Destroy") |
| 41 | flag.Parse() |
| 42 | } |
| 43 | |
| 44 | func main() { |
| 45 | if quiet { |
| 46 | log.SetOutput(ioutil.Discard) |
| 47 | } |
| 48 | log.Printf("Using %d GOMAXPROCS\n", runtime.NumCPU()) |
| 49 | |
| 50 | var wg sync.WaitGroup |
| 51 | |
| 52 | options := lxc.BusyboxTemplateOptions |
| 53 | options.Template = template |
| 54 | |
| 55 | for i := 0; i < iteration; i++ { |
| 56 | log.Printf("-- ITERATION %d --\n", i+1) |
| 57 | for _, mode := range []string{"CREATE", "START", "STOP", "DESTROY"} { |
| 58 | log.Printf("\t-- %s --\n", mode) |
| 59 | for j := 0; j < threads; j++ { |
| 60 | wg.Add(1) |
| 61 | go func(i int, mode string) { |
| 62 | c, err := lxc.NewContainer(strconv.Itoa(i), lxcpath) |
| 63 | if err != nil { |
| 64 | log.Fatalf("ERROR: %s\n", err.Error()) |
| 65 | } |
| 66 | defer c.Release() |
| 67 | |
| 68 | if mode == "CREATE" && startstop == false { |
| 69 | log.Printf("\t\tCreating the container (%d)...\n", i) |
| 70 | if err := c.Create(options); err != nil { |
| 71 | log.Fatalf("\t\t\tERROR: %s\n", err.Error()) |
| 72 | } |
| 73 | } else if mode == "START" && createdestroy == false { |
| 74 | log.Printf("\t\tStarting the container (%d)...\n", i) |
| 75 | if err := c.Start(); err != nil { |
| 76 | log.Fatalf("\t\t\tERROR: %s\n", err.Error()) |
| 77 | } |
| 78 | } else if mode == "STOP" && createdestroy == false { |
| 79 | log.Printf("\t\tStoping the container (%d)...\n", i) |
| 80 | if err := c.Stop(); err != nil { |
| 81 | log.Fatalf("\t\t\tERROR: %s\n", err.Error()) |
| 82 | } |
| 83 | } else if mode == "DESTROY" && startstop == false { |
| 84 | log.Printf("\t\tDestroying the container (%d)...\n", i) |
| 85 | if err := c.Destroy(); err != nil { |
| 86 | log.Fatalf("\t\t\tERROR: %s\n", err.Error()) |
| 87 | } |
| 88 | } |
| 89 | wg.Done() |
| 90 | }(j, mode) |
| 91 | } |
| 92 | wg.Wait() |
| 93 | } |
| 94 | } |
| 95 | } |