-
Notifications
You must be signed in to change notification settings - Fork 2
/
perfstat_test.go
118 lines (107 loc) · 3.04 KB
/
perfstat_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
package perfstat
import (
"context"
"fmt"
"testing"
"time"
"github.com/flaviostutz/perfstat/detectors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
var p Perfstat
func TestDetectNow(t *testing.T) {
opt := detectors.NewOptions()
//force alarms
opt.HighCPUPercRange = [2]float64{0.0, 0.9}
opt.CPULoadAvgDuration = 1 * time.Second
opt.IORateLoadDuration = 1 * time.Second
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
p := Start(ctx, opt)
p.SetLogLevel(logrus.DebugLevel)
time.Sleep(6 * time.Second)
issues, err := p.DetectNow()
assert.Nil(t, err)
assert.GreaterOrEqual(t, len(issues), 0)
checkOneEqual(t, issues, "bottleneck", "cpu-low")
}
func TestRollingDetections(t *testing.T) {
opt := detectors.NewOptions()
opt.CPULoadAvgDuration = 10 * time.Second
opt.IORateLoadDuration = 10 * time.Second
opt.IOLimitsSpan = 30 * time.Second
opt.MemAvgDuration = 30 * time.Second
opt.MemLeakDuration = 30 * time.Second
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
p := Start(ctx, opt)
p.SetLogLevel(logrus.DebugLevel)
time.Sleep(6 * time.Second)
for {
fmt.Printf("\n\n\n")
crit := p.TopCriticity(0.01, "", "", false)
for _, is := range crit {
// fmt.Printf(">>>> %s\n", is.String())
fmt.Printf("[ %.2f %s ] (%s %s=%.2f) (%s)\n", is.Score, is.ID, is.Res.Name, is.Res.PropertyName, is.Res.PropertyValue, is.Typ)
for _, r := range is.Related {
fmt.Printf(" > RELATED: %s [%s=%.2f]\n", r.Name, r.PropertyName, r.PropertyValue)
}
}
time.Sleep(5 * time.Second)
}
// checkOneEqual(t, crit, "bottleneck", "cpu-low-idle")
// checkOneEqual(t, crit, "risk", "disk-low-space")
// checkOneEqual(t, crit, "risk", "disk-high-util")
// checkOneEqual(t, crit, "bottleneck", "cpu-high-iowait")
// checkOneEqual(t, crit, "bottleneck", "mem-low")
// fmt.Printf("\n\n\nCPU\n")
// time.Sleep(1 * time.Second)
// crit = p.TopCriticity(0, "", "cpu", true)
// for _, is := range crit {
// fmt.Printf(">>>> %s\n", is.String())
// }
// fmt.Printf("\n\n\nMEM\n")
// time.Sleep(1 * time.Second)
// crit = p.TopCriticity(0, "", "mem", true)
// for _, is := range crit {
// fmt.Printf(">>>> %s\n", is.String())
// }
// fmt.Printf("\n\n\nNET\n")
// time.Sleep(1 * time.Second)
// crit = p.TopCriticity(0, "", "net", true)
// for _, is := range crit {
// fmt.Printf(">>>> %s\n", is.String())
// }
// fmt.Printf("\n\n\nDISK\n")
// time.Sleep(1 * time.Second)
// crit = p.TopCriticity(0, "", "disk", true)
// for _, is := range crit {
// fmt.Printf(">>>> %s\n", is.String())
// }
}
func checkOneEqual(t *testing.T, issues []detectors.DetectionResult, typ string, id string) {
found := false
for _, is := range issues {
if typ != "" && id != "" {
if is.ID == id && is.Typ == typ {
found = true
break
}
}
if typ != "" && id == "" {
if is.Typ == typ {
found = true
break
}
}
if typ == "" && id != "" {
if is.ID == id {
found = true
break
}
}
}
if !found {
t.Errorf("No issues found with type='%s', id='%s'", typ, id)
}
}