File: concurrent_stress.go

package info (click to toggle)
golang-gopkg-lxc-go-lxc.v2 0.0%2Bgit20220627.ad3d9f7-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 428 kB
  • sloc: ansic: 572; makefile: 77
file content (94 lines) | stat: -rw-r--r-- 2,649 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright © 2013, 2014, The Go-LXC Authors. All rights reserved.
// Use of this source code is governed by a LGPLv2.1
// license that can be found in the LICENSE file.

// +build linux,cgo

package main

import (
	"flag"
	"io/ioutil"
	"log"
	"runtime"
	"strconv"
	"sync"

	"github.com/lxc/go-lxc"
)

var (
	lxcpath       string
	iteration     int
	threads       int
	template      string
	quiet         bool
	startstop     bool
	createdestroy bool
)

func init() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	flag.StringVar(&lxcpath, "lxcpath", lxc.DefaultConfigPath(), "Use specified container path")
	flag.StringVar(&template, "template", "busybox", "Template to use")
	flag.IntVar(&threads, "threads", 10, "Number of operations to run concurrently")
	flag.IntVar(&iteration, "iteration", 1, "Number times to run the test")
	flag.BoolVar(&quiet, "quiet", false, "Don't produce any output")
	flag.BoolVar(&startstop, "startstop", false, "Flag to execute Start and Stop")
	flag.BoolVar(&createdestroy, "createdestroy", false, "Flag to execute Create and Destroy")
	flag.Parse()
}

func main() {
	if quiet {
		log.SetOutput(ioutil.Discard)
	}
	log.Printf("Using %d GOMAXPROCS\n", runtime.NumCPU())

	var wg sync.WaitGroup

	options := lxc.BusyboxTemplateOptions
	options.Template = template

	for i := 0; i < iteration; i++ {
		log.Printf("-- ITERATION %d --\n", i+1)
		for _, mode := range []string{"CREATE", "START", "STOP", "DESTROY"} {
			log.Printf("\t-- %s --\n", mode)
			for j := 0; j < threads; j++ {
				wg.Add(1)
				go func(i int, mode string) {
					c, err := lxc.NewContainer(strconv.Itoa(i), lxcpath)
					if err != nil {
						log.Fatalf("ERROR: %s\n", err.Error())
					}
					defer c.Release()

					if mode == "CREATE" && startstop == false {
						log.Printf("\t\tCreating the container (%d)...\n", i)
						if err := c.Create(options); err != nil {
							log.Fatalf("\t\t\tERROR: %s\n", err.Error())
						}
					} else if mode == "START" && createdestroy == false {
						log.Printf("\t\tStarting the container (%d)...\n", i)
						if err := c.Start(); err != nil {
							log.Fatalf("\t\t\tERROR: %s\n", err.Error())
						}
					} else if mode == "STOP" && createdestroy == false {
						log.Printf("\t\tStoping the container (%d)...\n", i)
						if err := c.Stop(); err != nil {
							log.Fatalf("\t\t\tERROR: %s\n", err.Error())
						}
					} else if mode == "DESTROY" && startstop == false {
						log.Printf("\t\tDestroying the container (%d)...\n", i)
						if err := c.Destroy(); err != nil {
							log.Fatalf("\t\t\tERROR: %s\n", err.Error())
						}
					}
					wg.Done()
				}(j, mode)
			}
			wg.Wait()
		}
	}
}