Last active
March 31, 2019 20:51
-
-
Save maurorappa/b868902fc7aa13d312fc265b7107591d to your computer and use it in GitHub Desktop.
Memory test containter, it ramps up memory usage as you want
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
==================================== | |
Build it as standalone binary : | |
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o memhog memhog.g | |
==================================== | |
Build its container : | |
FROM scratch | |
ADD memhog / | |
ENV GOGC=off | |
LABEL Memhog 1.0 | |
ENTRYPOINT ["/memhog”"] | |
// based on https://github.com/litrin/gostress/blob/master/gostress.go | |
*/ | |
package main | |
import ( | |
"runtime" | |
"flag" | |
"os" | |
"time" | |
) | |
var ( | |
cpuNumber = flag.Int("cpu", 0, "Enable CPU stress and specify how many cores ") | |
interval = flag.Int("time", 1, "Interval (seconds)") | |
steps = flag.Int("steps", 1, "Increment steps") | |
memory = flag.Int("memory", 1, "Memory increment per step (Mb)") | |
verbose = flag.Bool( "v", false, "Verbose On") | |
memstats runtime.MemStats | |
test_memory = make(map[int][]byte) | |
) | |
func main() { | |
flag.Parse() | |
// MARATHON_APP_RESOURCE_CPUS contains the value of the app definition cpus value. | |
// MARATHON_APP_RESOURCE_MEM contains the value of the app definition mem value, expressed in megabytes. | |
fmt.Println("Server:", os.Getenv("SERVERNAME")) | |
available_cores:= runtime.NumCPU() | |
if *cpuNumber > available_cores { | |
println("WARNING: cpu number cannot be bigger than cores number, using", available_cores,"threads") | |
*cpuNumber = available_cores | |
} | |
interval := time.Duration(*interval) * time.Second | |
if *cpuNumber > 0 { | |
println("CPU stress enabled") | |
for i := 0; i < *cpuNumber; i ++ { | |
go threads() | |
} | |
} | |
for i := 1; i <= *steps; i++ { | |
println("This process will now consume", *memory, "Mb more (total ", i * *memory,"Mb)") | |
time.Sleep(interval) | |
test_memory[i] = memoryFill(*memory, i) | |
//if you want to see the effect of the GC | |
//delete(test_memory,i-1) | |
} | |
println("DONE") | |
if *verbose { | |
//check here for details https://golang.org/pkg/runtime/#MemStats | |
runtime.ReadMemStats(&memstats) | |
println("Total Kbytes allocated for Heap:", memstats.TotalAlloc / 1024) | |
println("Total Kbytes obtained from OS:", memstats.Sys / 1024) | |
println("Total GC:", memstats.NumGC) | |
} | |
} | |
func threads() { | |
for i := 0; true; i ++ { | |
cpuHog(38) | |
} | |
} | |
func memoryFill(size int, value int) (buffer []byte) { | |
size = size * 1024 * 1024 | |
buffer = make([]byte, size) | |
for i := 0; i < size; i++ { | |
buffer[i] = byte(value) | |
} | |
return buffer | |
} | |
func cpuHog(i int) (result uint64) { | |
if i < 2 { | |
return uint64(1) | |
} | |
if i < 5 { | |
return cpuHog(i - 1) + cpuHog(i - 2) | |
} | |
return cpuHog(i - 1) + cpuHog(i - 2) - cpuHog(i - 5) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment