Skip to content

Commit 47ebadd

Browse files
committed
refactor code
1 parent 75863b9 commit 47ebadd

2 files changed

Lines changed: 7 additions & 46 deletions

File tree

checks/process_common.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ type ProcessCommon struct {
2323
Tags []string
2424
}
2525

26-
// Process tags for top usage
27-
//const (
28-
// TopCPU string = "usage:top-cpu"
29-
// TopMemory string = "usage:top-mem"
30-
// TopIORead string = "usage:top-io-read"
31-
// TopIOWrite string = "usage:top-io-write"
32-
//)
33-
3426
// returns a function to filter short-lived and blacklisted processes based on the configuration provided
3527
func keepProcess(cfg *config.AgentConfig) func(*ProcessCommon) bool {
3628
return func(process *ProcessCommon) bool {
@@ -68,13 +60,6 @@ func sortAndTakeN(processes []*ProcessCommon, sortingFunc func([]*ProcessCommon)
6860
return topNProcesses
6961
}
7062

71-
func addTagToProcessCommon(tag string) func(*ProcessCommon) *ProcessCommon {
72-
return func(process *ProcessCommon) *ProcessCommon {
73-
process.Tags = append(process.Tags, tag)
74-
return process
75-
}
76-
}
77-
7863
func getProcessInclusions(commonProcesses []*ProcessCommon, cfg *config.AgentConfig, totalCPUUsage float32, totalMemUsage uint64) []*ProcessCommon {
7964
cpuProcessChan := make(chan []*ProcessCommon)
8065
cpuProcesses := make([]*ProcessCommon, len(commonProcesses))

checks/process_test.go

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ func TestProcessBlacklisting(t *testing.T) {
274274
}
275275

276276
func TestProcessInclusions(t *testing.T) {
277-
type Tags = map[string]struct{}
278277
for _, tc := range []struct {
279278
name string
280279
processes []*ProcessCommon
@@ -284,10 +283,7 @@ func TestProcessInclusions(t *testing.T) {
284283
amountTopIOWriteUsage int
285284
amountTopMemoryUsage int
286285
memoryUsageThreshold int
287-
expectedPidsTags []struct {
288-
Pid int
289-
Tags Tags
290-
}
286+
expectedPids []int
291287
totalCPUpercentage float32
292288
totalMemory uint64
293289
}{
@@ -349,15 +345,7 @@ func TestProcessInclusions(t *testing.T) {
349345
amountTopIOWriteUsage: 1,
350346
amountTopMemoryUsage: 1,
351347
memoryUsageThreshold: 35,
352-
expectedPidsTags: []struct {
353-
Pid int
354-
Tags Tags
355-
}{
356-
{2, map[string]struct{}{}},
357-
{4, map[string]struct{}{}},
358-
{6, map[string]struct{}{}},
359-
{8, map[string]struct{}{}},
360-
},
348+
expectedPids: []int {2, 4, 6, 8},
361349
totalCPUpercentage: 25,
362350
totalMemory: 40,
363351
},
@@ -419,10 +407,7 @@ func TestProcessInclusions(t *testing.T) {
419407
amountTopIOWriteUsage: 1,
420408
amountTopMemoryUsage: 1,
421409
memoryUsageThreshold: 35,
422-
expectedPidsTags: []struct {
423-
Pid int
424-
Tags Tags
425-
}{{1, map[string]struct{}{}}},
410+
expectedPids: []int {1},
426411
totalCPUpercentage: 25,
427412
totalMemory: 40,
428413
},
@@ -484,13 +469,7 @@ func TestProcessInclusions(t *testing.T) {
484469
amountTopIOWriteUsage: 1,
485470
amountTopMemoryUsage: 1,
486471
memoryUsageThreshold: 35,
487-
expectedPidsTags: []struct {
488-
Pid int
489-
Tags Tags
490-
}{
491-
{6, map[string]struct{}{}},
492-
{8, map[string]struct{}{}},
493-
},
472+
expectedPids: []int {6, 8},
494473
totalCPUpercentage: 10,
495474
totalMemory: 10,
496475
},
@@ -509,12 +488,9 @@ func TestProcessInclusions(t *testing.T) {
509488
assert.True(t, len(processInclusions) <= maxTopProcesses, fmt.Sprintf("Way too many top processes reported: %d > %d", len(processInclusions), maxTopProcesses))
510489

511490
for _, proc := range processInclusions {
512-
sort.Strings(proc.Tags)
513-
pidTags := struct {
514-
Pid int
515-
Tags Tags
516-
}{int(proc.Pid), deriveSet(proc.Tags)}
517-
assert.Contains(t, tc.expectedPidsTags, pidTags, fmt.Sprintf("Expected pids/tags: %v, found pid/tag: %v", tc.expectedPidsTags, pidTags))
491+
pids := make([]int, 0)
492+
pids = append(pids, int(proc.Pid))
493+
assert.Contains(t, tc.expectedPids, pids, fmt.Sprintf("Expected pids/tags: %v, found pid/tag: %v", tc.expectedPids, pids))
518494
}
519495
})
520496
}

0 commit comments

Comments
 (0)