This repository was archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprocess_test.go
180 lines (157 loc) · 5.03 KB
/
process_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
package checks
import (
"github.com/stretchr/testify/require"
"regexp"
"runtime"
"strings"
"testing"
"time"
"github.com/DataDog/datadog-process-agent/config"
"github.com/DataDog/gopsutil/cpu"
"github.com/DataDog/gopsutil/process"
"github.com/stretchr/testify/assert"
"github.com/DataDog/datadog-agent/pkg/util/containers"
)
func makeProcess(pid int32, cmdline string) *process.FilledProcess {
return &process.FilledProcess{
Pid: pid,
Cmdline: strings.Split(cmdline, " "),
MemInfo: &process.MemoryInfoStat{},
CtxSwitches: &process.NumCtxSwitchesStat{},
}
}
func TestProcessChunking(t *testing.T) {
p := []*process.FilledProcess{
makeProcess(1, "git clone google.com"),
makeProcess(2, "mine-bitcoins -all -x"),
makeProcess(3, "datadog-process-agent -ddconfig datadog.conf"),
makeProcess(4, "foo -bar -bim"),
}
containers := []*containers.Container{}
lastRun := time.Now().Add(-5 * time.Second)
syst1, syst2 := cpu.TimesStat{}, cpu.TimesStat{}
cfg := config.NewDefaultAgentConfig()
for i, tc := range []struct {
cur, last []*process.FilledProcess
maxSize int
blacklist []string
expectedTotal int
expectedChunks int
}{
{
cur: []*process.FilledProcess{p[0], p[1], p[2]},
last: []*process.FilledProcess{p[0], p[1], p[2]},
maxSize: 1,
blacklist: []string{},
expectedTotal: 3,
expectedChunks: 3,
},
{
cur: []*process.FilledProcess{p[0], p[1], p[2]},
last: []*process.FilledProcess{p[0], p[2]},
maxSize: 1,
blacklist: []string{},
expectedTotal: 2,
expectedChunks: 2,
},
{
cur: []*process.FilledProcess{p[0], p[1], p[2], p[3]},
last: []*process.FilledProcess{p[0], p[1], p[2], p[3]},
maxSize: 10,
blacklist: []string{"git", "datadog"},
expectedTotal: 2,
expectedChunks: 1,
},
{
cur: []*process.FilledProcess{p[0], p[1], p[2], p[3]},
last: []*process.FilledProcess{p[0], p[1], p[2], p[3]},
maxSize: 10,
blacklist: []string{"git", "datadog", "foo", "mine"},
expectedTotal: 0,
expectedChunks: 0,
},
} {
bl := make([]*regexp.Regexp, 0, len(tc.blacklist))
for _, s := range tc.blacklist {
bl = append(bl, regexp.MustCompile(s))
}
cfg.Blacklist = bl
cfg.MaxPerMessage = tc.maxSize
cur := make(map[int32]*process.FilledProcess)
for _, c := range tc.cur {
cur[c.Pid] = c
}
last := make(map[int32]*process.FilledProcess)
for _, c := range tc.last {
last[c.Pid] = c
}
chunked := fmtProcesses(cfg, cur, last, containers, syst2, syst1, lastRun)
assert.Len(t, chunked, tc.expectedChunks, "len %d", i)
total := 0
for _, c := range chunked {
total += len(c)
}
assert.Equal(t, tc.expectedTotal, total, "total test %d", i)
chunkedStat := fmtProcessStats(cfg, cur, last, containers, syst2, syst1, lastRun)
assert.Len(t, chunkedStat, tc.expectedChunks, "len stat %d", i)
total = 0
for _, c := range chunkedStat {
total += len(c)
}
assert.Equal(t, tc.expectedTotal, total, "total stat test %d", i)
}
}
func TestPercentCalculation(t *testing.T) {
// Capping at NUM CPU * 100 if we get odd values for delta-{Proc,Time}
assert.True(t, floatEquals(calculatePct(100, 50, 1), 100))
// Zero deltaTime case
assert.True(t, floatEquals(calculatePct(100, 0, 8), 0.0))
assert.True(t, floatEquals(calculatePct(0, 8.08, 8), 0.0))
if runtime.GOOS != "windows" {
assert.True(t, floatEquals(calculatePct(100, 200, 2), 100))
assert.True(t, floatEquals(calculatePct(0.04, 8.08, 8), 3.960396))
assert.True(t, floatEquals(calculatePct(1.09, 8.08, 8), 107.920792))
}
}
func TestRateCalculation(t *testing.T) {
now := time.Now()
prev := now.Add(-1 * time.Second)
var empty time.Time
assert.True(t, floatEquals(calculateRate(5, 1, prev), 4))
assert.True(t, floatEquals(calculateRate(5, 1, prev.Add(-2*time.Second)), float32(1.33333333)))
assert.True(t, floatEquals(calculateRate(5, 1, now), 0))
assert.True(t, floatEquals(calculateRate(5, 0, prev), 0))
assert.True(t, floatEquals(calculateRate(5, 1, empty), 0))
// Underflow on cur - prev
assert.True(t, floatEquals(calculateRate(0, 1, prev), 0))
}
func TestFormatIO(t *testing.T) {
fp := &process.FilledProcess{
IOStat: &process.IOCountersStat{
ReadCount: 6,
WriteCount: 8,
ReadBytes: 10,
WriteBytes: 12,
},
}
last := &process.IOCountersStat{
ReadCount: 1,
WriteCount: 2,
ReadBytes: 3,
WriteBytes: 4,
}
// fp.IoStat is nil
assert.NotNil(t, formatIO(&process.FilledProcess{}, last, time.Now().Add(-2*time.Second)))
// Elapsed time < 1s
assert.NotNil(t, formatIO(fp, last, time.Now()))
result := formatIO(fp, last, time.Now().Add(-1*time.Second))
require.NotNil(t, result)
assert.Equal(t, float32(5), result.ReadRate)
assert.Equal(t, float32(6), result.WriteRate)
assert.Equal(t, float32(7), result.ReadBytesRate)
assert.Equal(t, float32(8), result.WriteBytesRate)
}
func floatEquals(a, b float32) bool {
var e float32 = 0.00000001 // Difference less than some epsilon
return a-b < e && b-a < e
}