-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
58 lines (52 loc) · 1.6 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
package main
import (
"flag"
"io"
"io/ioutil"
"os"
"time"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api/resource"
)
var (
argMemTotal = flag.String("mem-total", "0", "total memory to be consumed. Memory will be consumed via multiple allocations.")
argMemStepSize = flag.String("mem-alloc-size", "4Ki", "amount of memory to be consumed in each allocation")
argMemSleepDuration = flag.Duration("mem-alloc-sleep", time.Millisecond, "duration to sleep between allocations")
argCpus = flag.Int("cpus", 0, "total number of CPUs to utilize")
buffer [][]byte
)
func main() {
flag.Parse()
total := resource.MustParse(*argMemTotal)
stepSize := resource.MustParse(*argMemStepSize)
glog.Infof("Allocating %q memory, in %q chunks, with a %v sleep between allocations", total.String(), stepSize.String(), *argMemSleepDuration)
burnCPU()
allocateMemory(total, stepSize)
glog.Infof("Allocated %q memory", total.String())
select {}
}
func burnCPU() {
src, err := os.Open("/dev/zero")
if err != nil {
glog.Fatalf("failed to open /dev/zero")
}
for i := 0; i < *argCpus; i++ {
glog.Infof("Spawning a thread to consume CPU")
go func() {
_, err := io.Copy(ioutil.Discard, src)
if err != nil {
glog.Fatalf("failed to copy from /dev/zero to /dev/null: %v", err)
}
}()
}
}
func allocateMemory(total, stepSize resource.Quantity) {
for i := int64(1); i*stepSize.Value() <= total.Value(); i++ {
newBuffer := make([]byte, stepSize.Value())
for i := range newBuffer {
newBuffer[i] = 0
}
buffer = append(buffer, newBuffer)
time.Sleep(*argMemSleepDuration)
}
}