-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathsigar_shared_test.go
More file actions
130 lines (106 loc) · 3.47 KB
/
sigar_shared_test.go
File metadata and controls
130 lines (106 loc) · 3.47 KB
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
package sigar
import (
"fmt"
"os"
"os/exec"
"runtime"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
var _ = Describe("SigarShared", func() {
Describe("ProcCpu", func() {
var (
cpuGenerator *exec.Cmd
noCPUGenerator *exec.Cmd
)
BeforeEach(func() {
pathToStress, err := gexec.Build("github.com/cloudfoundry/gosigar/fixtures/stress")
Expect(err).NotTo(HaveOccurred())
cpuGenerator = exec.Command(pathToStress)
if err := cpuGenerator.Start(); err != nil {
panic("failed to start cpu generator")
}
noCPUCommand := "cat"
if runtime.GOOS == "windows" {
noCPUCommand = "notepad.exe"
}
noCPUGenerator = exec.Command(noCPUCommand)
if err := noCPUGenerator.Start(); err != nil {
panic("failed to start no cpu generator")
}
})
AfterEach(func() {
cpuGenerator.Process.Signal(os.Kill) //nolint:errcheck
noCPUGenerator.Process.Signal(os.Kill) //nolint:errcheck
})
It("calculates percentage", func() {
if runtime.GOOS == "darwin" {
Skip(fmt.Sprintf("Timing is different on %s", runtime.GOOS))
}
time.Sleep(time.Second) // High CPU process needs a second to spool up
pCpu := &ProcCpu{}
err := pCpu.Get(cpuGenerator.Process.Pid)
Expect(err).ToNot(HaveOccurred())
Expect(pCpu.Percent).To(BeNumerically(">=", 0.7))
Expect(pCpu.Percent).To(BeNumerically("<=", 1.6))
})
It("does not conflate multiple processes", func() {
if runtime.GOOS == "darwin" {
Skip(fmt.Sprintf("Not supported on %s", runtime.GOOS))
}
time.Sleep(time.Second) // High CPU process needs a second to spool up
pCpu := &ProcCpu{}
err := pCpu.Get(cpuGenerator.Process.Pid)
Expect(err).ToNot(HaveOccurred())
err = pCpu.Get(noCPUGenerator.Process.Pid)
Expect(err).ToNot(HaveOccurred())
Expect(pCpu.Percent).To(BeNumerically("~", 0.0, 0.02))
})
})
Describe("ProcMem", func() {
var memGenerator *exec.Cmd
var noMemGenerator *exec.Cmd
BeforeEach(func() {
pathToMemory, err := gexec.Build("github.com/cloudfoundry/gosigar/fixtures/memory")
Expect(err).NotTo(HaveOccurred())
memGenerator = exec.Command(pathToMemory, "-count", "16000000")
if err := memGenerator.Start(); err != nil {
panic("failed to start mem generator")
}
noMemGenerator = exec.Command(pathToMemory, "-count", "0")
if err := noMemGenerator.Start(); err != nil {
panic("failed to start no mem generator")
}
})
AfterEach(func() {
memGenerator.Process.Signal(os.Kill) //nolint:errcheck
noMemGenerator.Process.Signal(os.Kill) //nolint:errcheck
})
It("calculates memory usage", func() {
pMem := &ProcMem{}
Eventually(func() uint64 {
err := pMem.Get(memGenerator.Process.Pid)
Expect(err).NotTo(HaveOccurred())
return pMem.Resident
}, 5*time.Second).Should(BeNumerically("~", 18000000, 5*1024*1024))
Eventually(func() uint64 {
err := pMem.Get(memGenerator.Process.Pid)
Expect(err).NotTo(HaveOccurred())
return pMem.Size
}, 5*time.Second).Should(BeNumerically(">=", pMem.Resident))
pNoMem := &ProcMem{}
Eventually(func() uint64 {
err := pNoMem.Get(noMemGenerator.Process.Pid)
Expect(err).NotTo(HaveOccurred())
return pNoMem.Resident
}, 5*time.Second).Should(BeNumerically("~", 2000000, 5*1024*1024))
Eventually(func() uint64 {
err := pNoMem.Get(noMemGenerator.Process.Pid)
Expect(err).NotTo(HaveOccurred())
return pNoMem.Size
}, 5*time.Second).Should(BeNumerically(">=", pNoMem.Resident))
})
})
})