forked from juliusv/prometheus_demo_service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
49 lines (40 loc) · 1.12 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
48
49
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, usedBase, cachedBase, buffersBase, maxDeviation float64) {
var used, cached, buffers = usedBase, cachedBase, buffersBase
randomStep := func(current, base float64) float64 {
current += (rand.Float64() - 0.5) * 60 * 1024 * 1024
if current < base-base*maxDeviation {
current = base
}
if current > base+base*maxDeviation {
current = base
}
return current
}
for {
used = randomStep(used, usedBase)
cached = randomStep(cached, cachedBase)
buffers = randomStep(buffers, buffersBase)
memoryUsage.WithLabelValues("used").Set(used)
memoryUsage.WithLabelValues("cached").Set(cached)
memoryUsage.WithLabelValues("buffers").Set(buffers)
memoryUsage.WithLabelValues("free").Set(total - used - cached - buffers)
time.Sleep(100 * time.Millisecond)
}
}