-
Notifications
You must be signed in to change notification settings - Fork 32
/
memory.go
47 lines (38 loc) · 1.19 KB
/
memory.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
package main
import (
"math/rand"
"time"
"github.com/prometheus/client_golang/prometheus"
)
var memoryUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "memory_usage_bytes",
Help: "The memory usage by type.",
},
[]string{"type"},
)
func init() {
prometheus.MustRegister(memoryUsage)
}
func runMemorySim(total uint64, usedBase uint64, cachedBase uint64, buffersBase uint64, maxDeviation float64) {
var used, cached, buffers = usedBase, cachedBase, buffersBase
randomStep := func(current, base uint64) uint64 {
current += uint64((rand.Float64() - 0.5) * 60 * 1024 * 1024)
maxDev := uint64(float64(base) * maxDeviation)
if current < base-maxDev || current > base+maxDev {
current = base
}
return current
}
for {
used = randomStep(used, usedBase)
cached = randomStep(cached, cachedBase)
buffers = randomStep(buffers, buffersBase)
memoryUsage.WithLabelValues("used").Set(float64(used))
memoryUsage.WithLabelValues("cached").Set(float64(cached))
memoryUsage.WithLabelValues("buffers").Set(float64(buffers))
memoryUsage.WithLabelValues("free").Set(float64(total - used - cached - buffers))
time.Sleep(100 * time.Millisecond)
}
}