forked from daangn/autopprof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroupv2_test.go
79 lines (68 loc) · 1.81 KB
/
cgroupv2_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
//go:build linux
// +build linux
package autopprof
import (
"testing"
"time"
"github.com/containerd/cgroups"
)
func TestCgroupV2_cpuUsage(t *testing.T) {
mode := cgroups.Mode()
if mode != cgroups.Hybrid && mode != cgroups.Unified {
t.Skip("cgroup v2 is not available")
}
cgv2 := newCgroupsV2()
cgv2.cpuQuota = 2
cgv2.q = newCPUUsageSnapshotQueue(3)
usage, err := cgv2.cpuUsage()
if err != nil {
t.Errorf("cpuUsage() = %v, want nil", err)
}
if usage != 0 { // The cpu usage is 0 until the queue is full.
t.Errorf("cpuUsage() = %f, want 0", usage)
}
time.Sleep(1050 * time.Millisecond)
usage, err = cgv2.cpuUsage()
if err != nil {
t.Errorf("cpuUsage() = %v, want nil", err)
}
if usage != 0 { // The cpu usage is 0 until the queue is full.
t.Errorf("cpuUsage() = %f, want 0", usage)
}
time.Sleep(1050 * time.Millisecond)
usage, err = cgv2.cpuUsage()
if err != nil {
t.Errorf("cpuUsage() = %v, want nil", err)
}
if usage < 0 || usage > 1 {
t.Errorf("cpuUsage() = %f, want between 0 and 1", usage)
}
}
func TestCgroupV2_memUsage(t *testing.T) {
mode := cgroups.Mode()
if mode != cgroups.Hybrid && mode != cgroups.Unified {
t.Skip("cgroup v2 is not available")
}
cgv2 := newCgroupsV2()
usage, err := cgv2.memUsage()
if err != nil {
t.Errorf("memUsage() = %v, want nil", err)
}
if usage < 0 || usage > 1 {
t.Errorf("memUsage() = %f, want between 0 and 1", usage)
}
}
func TestCgroupV2_setCPUQuota(t *testing.T) {
mode := cgroups.Mode()
if mode != cgroups.Hybrid && mode != cgroups.Unified {
t.Skip("cgroup v2 is not available")
}
cgv2 := newCgroupsV2()
if err := cgv2.setCPUQuota(); err != nil {
t.Errorf("setCPUQuota() = %v, want nil", err)
}
// The cpu quota of test docker container is 1.5.
if cgv2.cpuQuota != 1.5 {
t.Errorf("cpuQuota = %f, want 1.5", cgv2.cpuQuota)
}
}