This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
/
resourcemanager_test.go
251 lines (228 loc) · 6.54 KB
/
resourcemanager_test.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) 2012 VMware, Inc.
package gonit
import (
"github.com/bmizerany/assert"
"os"
"testing"
"time"
)
type FakeSigarGetter struct {
memResident uint64
procUsed uint64
sysMemUsed uint64
}
func (s *FakeSigarGetter) getMemResident(pid int) (uint64, error) {
return (s.memResident), nil
}
// Gets the proc time and a timestamp and returns a DataTimestamp.
func (s *FakeSigarGetter) getProcTime(pid int) (uint64, error) {
return s.procUsed, nil
}
var r ResourceManager
func Setup() {
r = ResourceManager{
sigarInterface: &SigarGetter{},
cachedResources: map[string]uint64{},
}
}
func TestCalculateProcPercent(t *testing.T) {
Setup()
first := &DataTimestamp{
data: 2886, nanoTimestamp: 1.342471447022575e+18}
second := &DataTimestamp{
data: 3849, nanoTimestamp: 1.342471449022077e+18}
var data []*DataTimestamp
data = append(data, first)
data = append(data, second)
resourceHolder := ResourceHolder{dataTimestamps: data, maxDataToStore: 3}
proc, err := resourceHolder.calculateProcPercent(second, first)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, uint64(48), proc)
}
func TestGatherMem(t *testing.T) {
Setup()
duration, _ := time.ParseDuration("0s")
interval, _ := time.ParseDuration("1s")
r.SetSigarInterface(&FakeSigarGetter{memResident: 1024})
pe := &ParsedEvent{
resourceName: MEMORY_USED_NAME,
duration: duration,
interval: interval,
}
resourceVal, err := r.GetResource(pe, 1234)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, uint64(1024), resourceVal)
}
func TestgetMemResident(t *testing.T) {
Setup()
pid := os.Getpid()
val, err := r.sigarInterface.getMemResident(pid)
assert.Equal(t, nil, err)
assert.NotEqual(t, 0, val)
}
// When we have gotten proc percent twice, then we can get the proc time.
func TestGatherProcPercent(t *testing.T) {
Setup()
duration, _ := time.ParseDuration("2s")
interval, _ := time.ParseDuration("1s")
fsg := FakeSigarGetter{procUsed: 2886}
r.SetSigarInterface(&fsg)
pe := &ParsedEvent{
resourceName: "cpu_percent",
duration: duration,
interval: interval,
}
resourceVal, err := r.GetResource(pe, 1234)
if err != nil {
t.Fatal(err)
}
fsg.procUsed = 3849
r.SetSigarInterface(&fsg)
timeThen := r.resourceHolders[0].dataTimestamps[0].nanoTimestamp
// Set the time to be 1 second ago.
r.resourceHolders[0].dataTimestamps[0].nanoTimestamp = timeThen - 1000000000
r.ClearCachedResources()
resourceVal, err = r.GetResource(pe, 1234)
if err != nil {
t.Fatal(err)
}
assert.NotEqual(t, uint64(0), resourceVal)
}
func TestParseAmountErrors(t *testing.T) {
Setup()
_, err := r.ParseAmount(MEMORY_USED_NAME, "2k")
assert.Equal(t, "memory_used '2k' is not the correct format.", err.Error())
_, err = r.ParseAmount(MEMORY_USED_NAME, "$kb")
assert.Equal(t, "strconv.ParseUint: parsing \"$\": invalid syntax",
err.Error())
_, err = r.ParseAmount(MEMORY_USED_NAME, "5zb")
assert.Equal(t, "Invalid units 'zb' on 'memory_used'.", err.Error())
_, err = r.ParseAmount(CPU_PERCENT_NAME, "5zb")
assert.Equal(t, "strconv.ParseUint: parsing \"5zb\": invalid syntax",
err.Error())
}
func TestIsValidResourceName(t *testing.T) {
Setup()
assert.Equal(t, true, r.IsValidResourceName(MEMORY_USED_NAME))
assert.Equal(t, true, r.IsValidResourceName(CPU_PERCENT_NAME))
assert.Equal(t, true, r.IsValidResourceName(CPU_PERCENT_NAME))
}
func TestSaveDataReusesSlice(t *testing.T) {
rh := &ResourceHolder{}
rh.maxDataToStore = 3
for i := int64(0); i < rh.maxDataToStore; i++ {
rh.saveData(uint64(i))
}
data := rh.dataTimestamps
assert.Equal(t, uint64(0), data[0].data)
assert.Equal(t, uint64(rh.maxDataToStore-1), data[len(data)-1].data)
rh.saveData(1337)
assert.Equal(t, uint64(1337), data[0].data)
rh.saveData(1338)
assert.Equal(t, uint64(1338), data[1].data)
}
func TestCircularProcData(t *testing.T) {
Setup()
duration, _ := time.ParseDuration("3s")
interval, _ := time.ParseDuration("1s")
// 3 / 1 = 3 pieces of data max.
maxData := 3
pe := &ParsedEvent{
resourceName: "cpu_percent",
duration: duration,
interval: interval,
}
for i := 0; i < maxData; i++ {
fsg := &FakeSigarGetter{
procUsed: uint64(2886 + i),
}
r.SetSigarInterface(fsg)
r.ClearCachedResources()
_, err := r.GetResource(pe, 1234)
if err != nil {
t.Fatal(err)
}
}
rh := r.getResourceHolder(pe)
// Assert we have what we expect.
assert.Equal(t, uint64(2886), rh.dataTimestamps[0].data)
assert.Equal(t, uint64(2888),
rh.dataTimestamps[len(rh.dataTimestamps)-1].data)
// Now, make sure that we loop on the data slice.
fsg := &FakeSigarGetter{
procUsed: uint64(2897),
}
r.SetSigarInterface(fsg)
r.ClearCachedResources()
_, err := r.GetResource(pe, 1234)
if err != nil {
t.Fatal(err)
}
// Assert that we looped.
assert.Equal(t, uint64(2897), rh.dataTimestamps[0].data)
}
func TestDuration(t *testing.T) {
Setup()
duration, _ := time.ParseDuration("3s")
interval, _ := time.ParseDuration("1s")
pe := &ParsedEvent{
resourceName: "cpu_percent",
duration: duration,
interval: interval,
}
// Under 2 so we can manually add the 3rd piece.
for i := 0; i < 2; i++ {
fsg := &FakeSigarGetter{
procUsed: uint64(2886 + i),
}
r.SetSigarInterface(fsg)
r.ClearCachedResources()
_, err := r.GetResource(pe, 1234)
if err != nil {
t.Fatal(err)
}
}
rh := r.getResourceHolder(pe)
rh.dataTimestamps[0].nanoTimestamp =
rh.dataTimestamps[0].nanoTimestamp - 2000000000
rh.dataTimestamps[1].nanoTimestamp =
rh.dataTimestamps[1].nanoTimestamp - 1000000000
fsg := &FakeSigarGetter{
procUsed: uint64(4000),
}
r.SetSigarInterface(fsg)
r.ClearCachedResources()
resourceVal, err := r.GetResource(pe, 1234)
if err != nil {
t.Fatal(err)
}
timeDiff := float64(rh.dataTimestamps[2].nanoTimestamp-
rh.dataTimestamps[0].nanoTimestamp) / NANO_TO_MILLI
valDiff := rh.dataTimestamps[2].data - rh.dataTimestamps[0].data
expectedPercent := uint64(100 * (float64(valDiff) / float64(timeDiff)))
assert.Equal(t, expectedPercent, resourceVal)
}
func TestCleanProcData(t *testing.T) {
Setup()
dts := &DataTimestamp{
data: 1,
nanoTimestamp: 1,
}
rh := &ResourceHolder{
dataTimestamps: []*DataTimestamp{dts},
firstEntryIndex: 1,
processName: "process",
resourceName: "resource",
}
proc := &Process{Name: "process"}
r.resourceHolders = append(r.resourceHolders, rh)
assert.Equal(t, 1, len(r.resourceHolders[0].dataTimestamps))
assert.Equal(t, int64(1), r.resourceHolders[0].firstEntryIndex)
r.CleanDataForProcess(proc)
assert.Equal(t, 0, len(r.resourceHolders[0].dataTimestamps))
assert.Equal(t, int64(0), r.resourceHolders[0].firstEntryIndex)
}