-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_test.go
81 lines (64 loc) · 1.38 KB
/
collector_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
package stats
import (
"encoding/json"
"runtime"
"sync"
"testing"
)
func TestCapture(t *testing.T) {
runtime.GC()
var wg sync.WaitGroup
col := &testCollector{wg: &wg}
cl := New(CollectionClient(col))
cl.capture()
runData(&wg)
if col.Memory <= 0 {
t.Errorf("Expected memory to be captured")
}
if col.Goroutines <= 0 {
t.Errorf("Exected a goroutine to be created")
}
if col.NumGC <= 0 {
t.Errorf("Exected a GC to have run")
}
if len(col.GCPauses) <= 0 {
t.Errorf("Exected a GC Pause to have been recorded")
}
if col.NumCgo <= 0 {
// TODO: Need to figure out a way to test this with stdlib
//t.Errorf("Expected CGO calls to have happened")
}
}
func BenchmarkCapture(b *testing.B) {
b.ReportAllocs()
c := New()
for i := 0; i < b.N; i++ {
c.capture()
}
}
// runData generates some information for all the stats that we are trying to
// collect. This assures that we can run our tests against something.
func runData(wg *sync.WaitGroup) {
// WaitGroup to initialise the data run
wg.Add(1)
// allocate memory
bts := []byte(`{"name": "Jelmer Snoeck"}`)
var tp struct {
Name string `json:"name"`
}
json.Unmarshal(bts, &tp)
// get goroutine counter
wg.Add(1)
go func(g *sync.WaitGroup) {
defer g.Done()
}(wg)
wg.Wait()
}
type testCollector struct {
AppStat
wg *sync.WaitGroup
}
func (c *testCollector) Collect(a AppStat) {
c.AppStat = a
c.wg.Done()
}