-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
86 lines (68 loc) · 1.55 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
78
79
80
81
82
83
84
85
86
package main
// #include <time.h>
import "C"
import (
. "log"
"os"
"runtime"
"sync"
"time"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
logger := New(os.Stderr, "eip-great-ptretender ", Llongfile|Ldate|Ltime)
app.Flags = []cli.Flag{
cli.Float64Flag{
Name: "targetCPU, t",
Value: 30.0,
Usage: "The load to be generated.",
},
cli.Float64Flag{
Name: "samplingRate, s",
Value: 0,
Usage: "A rate, in second, to sample CPU usage to determine percentage(used ticks / total ticks since start). If 0, starts are not reset.",
},
cli.BoolFlag{
Name: "debug, d",
Usage: "If Set, outputs metrics about system load.",
},
}
app.Action = func(c *cli.Context) error {
var wg sync.WaitGroup
defer func() {
if errP := recover(); errP != nil {
logger.Printf("Couldn't recover from panic - reason: %e", errP)
}
}()
wg.Add(1)
go func(wg *sync.WaitGroup) {
targetCPU := c.Float64("targetCPU")
samplingRate := c.Float64("samplingRate")
debug := c.Bool("debug")
//done := make(chan int)
//for CPU
for i := 0; i < runtime.NumCPU(); i++ {
go func() {
for {
cpuLoad := CpuUsagePercent(samplingRate, debug)
if debug {
logger.Printf(" Load : %f, Target : %f", cpuLoad, targetCPU)
}
if cpuLoad >= targetCPU {
waitTime := time.Second / time.Duration(WaitFactor)
time.Sleep(waitTime)
}
}
defer wg.Done()
}()
}
}(&wg)
wg.Wait()
return nil
}
errRun := app.Run(os.Args)
if errRun != nil {
logger.Panic(errRun)
}
}