-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (55 loc) · 1.45 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
package main
import (
"fmt"
"os"
"runtime"
"strconv"
"time"
)
func readArguments(args []string) (int64, int64) {
percentageLoad, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
fmt.Println("Invalid percentage load")
percentageLoad = 0
}
secondsToRun, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
fmt.Println("Invalid seconds to run")
secondsToRun = 0
}
return percentageLoad, secondsToRun
}
func calcuatePercentage(percentageLoad int64) float64 {
return float64((float64(percentageLoad) / 100.0))
}
func calcuateCPUCount(percentage float64, CPU int) float64 {
return float64(CPU) * percentage
}
func outputLoadAndCount(cpuCount, percentage float64, percentageLoad int64) {
fmt.Println(fmt.Sprintf("%v * ( %v / %v ) = %v", runtime.NumCPU(), percentageLoad, 100, cpuCount))
fmt.Println(fmt.Sprintf("%v * %v = %v", runtime.NumCPU(), percentage, cpuCount))
}
func troll(cpuCount float64, secondsToRun int64) {
done := make(chan int)
for i := 0; i < int(cpuCount); i++ {
go func() {
for {
select {
case <-done:
return
default:
}
}
}()
}
time.Sleep(time.Second * time.Duration(secondsToRun))
close(done)
}
func main() {
args := os.Args[1:]
var percentageLoad, secondsToRun = readArguments(args)
var percentage = calcuatePercentage(percentageLoad)
var cpuCount = calcuateCPUCount(percentage, runtime.NumCPU())
outputLoadAndCount(cpuCount, percentage, percentageLoad)
troll(cpuCount, secondsToRun)
}