master
go 183 lines 3.63 KB
Raw
1 package profile
2
3 import (
4 "archive/zip"
5 "bytes"
6 "context"
7 "testing"
8 "time"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 )
13
14 func TestProfiler(t *testing.T) {
15 allCollectors := []string{
16 CollectorGoroutinesStack,
17 CollectorGoroutinesPprof,
18 CollectorVersion,
19 CollectorHeap,
20 CollectorAllocs,
21 CollectorBin,
22 CollectorCPU,
23 CollectorMutex,
24 CollectorBlock,
25 CollectorTrace,
26 }
27
28 cases := []struct {
29 name string
30 opts Options
31 goos string
32
33 expectFiles []string
34 }{
35 {
36 name: "happy case",
37 opts: Options{
38 Collectors: allCollectors,
39 ProfileDuration: 1 * time.Millisecond,
40 MutexProfileFraction: 4,
41 BlockProfileRate: 50 * time.Nanosecond,
42 },
43 expectFiles: []string{
44 "goroutines.stacks",
45 "goroutines.pprof",
46 "version.json",
47 "heap.pprof",
48 "allocs.pprof",
49 "ipfs",
50 "cpu.pprof",
51 "mutex.pprof",
52 "block.pprof",
53 "trace",
54 },
55 },
56 {
57 name: "windows",
58 opts: Options{
59 Collectors: allCollectors,
60 ProfileDuration: 1 * time.Millisecond,
61 MutexProfileFraction: 4,
62 BlockProfileRate: 50 * time.Nanosecond,
63 },
64 goos: "windows",
65 expectFiles: []string{
66 "goroutines.stacks",
67 "goroutines.pprof",
68 "version.json",
69 "heap.pprof",
70 "allocs.pprof",
71 "ipfs.exe",
72 "cpu.pprof",
73 "mutex.pprof",
74 "block.pprof",
75 "trace",
76 },
77 },
78 {
79 name: "sampling profiling disabled",
80 opts: Options{
81 Collectors: allCollectors,
82 MutexProfileFraction: 4,
83 BlockProfileRate: 50 * time.Nanosecond,
84 },
85 expectFiles: []string{
86 "goroutines.stacks",
87 "goroutines.pprof",
88 "version.json",
89 "heap.pprof",
90 "allocs.pprof",
91 "ipfs",
92 },
93 },
94 {
95 name: "Mutex profiling disabled",
96 opts: Options{
97 Collectors: allCollectors,
98 ProfileDuration: 1 * time.Millisecond,
99 BlockProfileRate: 50 * time.Nanosecond,
100 },
101 expectFiles: []string{
102 "goroutines.stacks",
103 "goroutines.pprof",
104 "version.json",
105 "heap.pprof",
106 "allocs.pprof",
107 "ipfs",
108 "cpu.pprof",
109 "block.pprof",
110 "trace",
111 },
112 },
113 {
114 name: "block profiling disabled",
115 opts: Options{
116 Collectors: allCollectors,
117 ProfileDuration: 1 * time.Millisecond,
118 MutexProfileFraction: 4,
119 BlockProfileRate: 0,
120 },
121 expectFiles: []string{
122 "goroutines.stacks",
123 "goroutines.pprof",
124 "version.json",
125 "heap.pprof",
126 "allocs.pprof",
127 "ipfs",
128 "cpu.pprof",
129 "mutex.pprof",
130 "trace",
131 },
132 },
133 {
134 name: "single collector",
135 opts: Options{
136 Collectors: []string{CollectorVersion},
137 ProfileDuration: 1 * time.Millisecond,
138 MutexProfileFraction: 4,
139 BlockProfileRate: 0,
140 },
141 expectFiles: []string{
142 "version.json",
143 },
144 },
145 }
146 for _, c := range cases {
147 t.Run(c.name, func(t *testing.T) {
148 if c.goos != "" {
149 oldGOOS := goos
150 goos = c.goos
151 defer func() { goos = oldGOOS }()
152 }
153
154 buf := &bytes.Buffer{}
155 archive := zip.NewWriter(buf)
156 err := WriteProfiles(context.Background(), archive, c.opts)
157 require.NoError(t, err)
158
159 err = archive.Close()
160 require.NoError(t, err)
161
162 zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
163 require.NoError(t, err)
164
165 for _, f := range zr.File {
166 logger.Info("zip file: ", f.Name)
167 }
168
169 require.Equal(t, len(c.expectFiles), len(zr.File))
170
171 for _, expectedFile := range c.expectFiles {
172 func() {
173 f, err := zr.Open(expectedFile)
174 require.NoError(t, err)
175 defer f.Close()
176 fi, err := f.Stat()
177 require.NoError(t, err)
178 assert.NotZero(t, fi.Size())
179 }()
180 }
181 })
182 }
183 }