-
Notifications
You must be signed in to change notification settings - Fork 113
/
devops_mem.go
105 lines (92 loc) · 2.78 KB
/
devops_mem.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package devops
import (
. "github.com/influxdata/influxdb-comparisons/bulk_data_gen/common"
"math"
"math/rand"
"time"
)
var (
MemoryByteString = []byte("mem") // heap optimization
// Choices for modeling a host's memory capacity.
MemoryMaxBytesChoices = []int64{8 << 30, 12 << 30, 16 << 30}
// Field keys for 'mem' points.
MemoryFieldKeys = [][]byte{
[]byte("total"),
[]byte("available"),
[]byte("used"),
[]byte("free"),
[]byte("cached"),
[]byte("buffered"),
[]byte("used_percent"),
[]byte("available_percent"),
[]byte("buffered_percent"),
}
)
type MemMeasurement struct {
// this doesn't change:
bytesTotal int64
// these change:
timestamp time.Time
bytesUsedDist, bytesCachedDist, bytesBufferedDist Distribution
}
func NewMemMeasurement(start time.Time) *MemMeasurement {
bytesTotal := MemoryMaxBytesChoices[rand.Intn(len(MemoryMaxBytesChoices))]
bytesUsedDist := &ClampedRandomWalkDistribution{
State: rand.Float64() * float64(bytesTotal),
Min: 0.0,
Max: float64(bytesTotal),
Step: &NormalDistribution{
Mean: 0.0,
StdDev: float64(bytesTotal) / 64,
},
}
bytesCachedDist := &ClampedRandomWalkDistribution{
State: rand.Float64() * float64(bytesTotal),
Min: 0.0,
Max: float64(bytesTotal),
Step: &NormalDistribution{
Mean: 0.0,
StdDev: float64(bytesTotal) / 64,
},
}
bytesBufferedDist := &ClampedRandomWalkDistribution{
State: rand.Float64() * float64(bytesTotal),
Min: 0.0,
Max: float64(bytesTotal),
Step: &NormalDistribution{
Mean: 0.0,
StdDev: float64(bytesTotal) / 64,
},
}
return &MemMeasurement{
timestamp: start,
bytesTotal: bytesTotal,
bytesUsedDist: bytesUsedDist,
bytesCachedDist: bytesCachedDist,
bytesBufferedDist: bytesBufferedDist,
}
}
func (m *MemMeasurement) Tick(d time.Duration) {
m.timestamp = m.timestamp.Add(d)
m.bytesUsedDist.Advance()
m.bytesCachedDist.Advance()
m.bytesBufferedDist.Advance()
}
func (m *MemMeasurement) ToPoint(p *Point) bool {
p.SetMeasurementName(MemoryByteString)
p.SetTimestamp(&m.timestamp)
total := m.bytesTotal
used := m.bytesUsedDist.Get()
cached := m.bytesCachedDist.Get()
buffered := m.bytesBufferedDist.Get()
p.AppendField(MemoryFieldKeys[0], total)
p.AppendField(MemoryFieldKeys[1], int(math.Floor(float64(total)-used)))
p.AppendField(MemoryFieldKeys[2], int(math.Floor(used)))
p.AppendField(MemoryFieldKeys[3], int(math.Floor(cached)))
p.AppendField(MemoryFieldKeys[4], int(math.Floor(buffered)))
p.AppendField(MemoryFieldKeys[5], int(math.Floor(used)))
p.AppendField(MemoryFieldKeys[6], 100.0*(used/float64(total)))
p.AppendField(MemoryFieldKeys[7], 100.0*(float64(total)-used)/float64(total))
p.AppendField(MemoryFieldKeys[8], 100.0*(float64(total)-buffered)/float64(total))
return true
}