forked from giantswarm/kube-stresscheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (63 loc) · 1.98 KB
/
main.go
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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"runtime"
)
// #include <unistd.h>
import "C"
const (
listenSocket = ":6666"
stressBinary = "stress"
stressTimeout = 60
stressIterations = 5
stressDefaultMemoryMB = 256
stressMemoryHangSec = 2
)
// Common variables.
var (
description = "Simple stress checker for Kubernetes nodes."
gitCommit = "n/a"
name = "kube-stresscheck"
source = "https://github.com/giantswarm/kube-stresscheck"
)
func main() {
// Print version.
if (len(os.Args) > 1) && (os.Args[1] == "version") {
fmt.Printf("Description: %s\n", description)
fmt.Printf("Git Commit: %s\n", gitCommit)
fmt.Printf("Go Version: %s\n", runtime.Version())
fmt.Printf("Name: %s\n", name)
fmt.Printf("OS / Arch: %s / %s\n", runtime.GOOS, runtime.GOARCH)
fmt.Printf("Source: %s\n", source)
return
}
// Print usage.
if (len(os.Args) > 1) && (os.Args[1] == "--help") {
return
}
// CPU forks to start. By default allocate 2 times cores.
var stressCPUForks = 2 * runtime.NumCPU()
// The easiest way w/o tons of code and external modules to get system memory.
var totalSystemMemoryMB = C.sysconf(C._SC_PHYS_PAGES) * C.sysconf(C._SC_PAGE_SIZE) / 1024 / 1024
// Memory forks to start. By default aim to allocate total memory size.
var stressMemoryForks = totalSystemMemoryMB / stressDefaultMemoryMB
args := []string{
"--cpu", fmt.Sprintf("%v", stressCPUForks),
"--vm", fmt.Sprintf("%v", stressMemoryForks),
"--vm-hang", fmt.Sprintf("%v", stressMemoryHangSec),
"--timeout", fmt.Sprintf("%v", stressTimeout),
}
// Invoke stress command multiple times.
for i := 0; i < stressIterations; i++ {
log.Printf("Executing %s with %v", stressBinary, args)
err := exec.Command(stressBinary, args...).Run()
if err != nil {
log.Printf("Command stress exited with: %s", err)
}
}
// We can not say was it success or failure, we just gave a stress to the node.
log.Printf("Stress testing executed.")
}