-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_test.go
237 lines (213 loc) · 6.06 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package autoprof_test
import (
"archive/zip"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/fs"
"runtime/pprof"
"runtime/trace"
"testing"
"time"
"github.com/rhysh/autoprof"
)
func TestZipCollector(t *testing.T) {
const (
profileName = "pprof/profile"
traceName = "pprof/trace"
profileDuringTraceName = "pprof/profile-during-trace"
)
checkExist := func(t *testing.T, zr *zip.Reader, name string) {
f, err := zr.Open(name)
if err != nil {
t.Errorf("zip.Reader.Open; err = %v", err)
return
}
defer f.Close()
}
checkNotExist := func(t *testing.T, zr *zip.Reader, name string) {
f, err := zr.Open(name)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return
}
t.Errorf("zip.Reader.Open; err = %v", err)
return
}
t.Errorf("found file %q", name)
defer f.Close()
}
ctx := context.Background()
meta := autoprof.CurrentArchiveMeta()
t.Run("basic", func(t *testing.T) {
_, err := collect(ctx, meta, &autoprof.ArchiveOptions{})
if err != nil {
t.Fatalf("collect; err = %v", err)
}
})
t.Run("profile only", func(t *testing.T) {
if profileIsEnabled() {
t.Skip("a CPU profile is already active")
}
defer func() {
if profileIsEnabled() {
t.Errorf("a CPU profile remained active")
}
}()
// Bundles that request a CPU profile should include one
zr, err := collect(ctx, meta, &autoprof.ArchiveOptions{
CPUProfileDuration: 100 * time.Millisecond,
})
if err != nil {
t.Fatalf("collect; err = %v", err)
}
checkExist(t, zr, profileName)
checkNotExist(t, zr, traceName)
checkNotExist(t, zr, profileDuringTraceName)
})
t.Run("trace only", func(t *testing.T) {
if trace.IsEnabled() {
t.Skip("an execution trace is already active")
}
defer func() {
if trace.IsEnabled() {
t.Errorf("an execution trace remained active")
}
}()
// Bundles that request an execution trace should include one
zr, err := collect(ctx, meta, &autoprof.ArchiveOptions{
ExecutionTraceDuration: 100 * time.Millisecond,
})
if err != nil {
t.Fatalf("collect; err = %v", err)
}
checkNotExist(t, zr, profileName)
checkExist(t, zr, traceName)
checkNotExist(t, zr, profileDuringTraceName)
})
t.Run("profile and trace", func(t *testing.T) {
if profileIsEnabled() {
t.Skip("a CPU profile is already active")
}
if trace.IsEnabled() {
t.Skip("an execution trace is already active")
}
defer func() {
if profileIsEnabled() {
t.Errorf("a CPU profile remained active")
}
if trace.IsEnabled() {
t.Errorf("an execution trace remained active")
}
}()
// Bundles that request both a CPU profile and an execution trace should
// include both, plus a second CPU profile covering the duration of the
// execution trace.
zr, err := collect(ctx, meta, &autoprof.ArchiveOptions{
CPUProfileDuration: 100 * time.Millisecond,
ExecutionTraceDuration: 100 * time.Millisecond,
})
if err != nil {
t.Fatalf("collect; err = %v", err)
}
checkExist(t, zr, profileName)
checkExist(t, zr, traceName)
checkExist(t, zr, profileDuringTraceName)
})
t.Run("trace but profile already running", func(t *testing.T) {
if profileIsEnabled() {
t.Skip("a CPU profile is already active")
}
if trace.IsEnabled() {
t.Skip("an execution trace is already active")
}
defer func() {
if profileIsEnabled() {
t.Errorf("a CPU profile remained active")
}
if trace.IsEnabled() {
t.Errorf("an execution trace remained active")
}
}()
err := pprof.StartCPUProfile(io.Discard)
defer func() {
if !profileIsEnabled() {
t.Errorf("something stopped our CPU profile")
}
pprof.StopCPUProfile()
}()
// If a CPU profile is already running, we should still be able to
// collect most of the bundle: It won't be able to include a CPU
// profile, and it won't be able to include a CPU profile covering the
// execution trace. But the collection should not result in an error,
// and it should still include the execution trace.
//
// Furthermore, collecting the bundle (including the attempts to start a
// CPU profile) should not interrupt the running CPU profile.
zr, err := collect(ctx, meta, &autoprof.ArchiveOptions{
CPUProfileDuration: 100 * time.Millisecond,
ExecutionTraceDuration: 100 * time.Millisecond,
})
if err != nil {
t.Fatalf("collect; err = %v", err)
}
checkNotExist(t, zr, profileName)
checkExist(t, zr, traceName)
checkNotExist(t, zr, profileDuringTraceName)
})
t.Run("trace already running", func(t *testing.T) {
if trace.IsEnabled() {
t.Skip("an execution trace is already active")
}
defer func() {
if trace.IsEnabled() {
t.Errorf("an execution trace remained active")
}
}()
err := trace.Start(io.Discard)
defer func() {
if !trace.IsEnabled() {
t.Errorf("something stopped our execution trace")
}
trace.Stop()
}()
// If an execution trace is already running, we should still be able to
// collect most of the bundle: it won't be able to include an execution
// trace, but it should not result in an error.
//
// Furthermore, collecting the bundle should not interrupt the running
// execution trace.
zr, err := collect(ctx, meta, &autoprof.ArchiveOptions{
ExecutionTraceDuration: 100 * time.Millisecond,
})
if err != nil {
t.Fatalf("collect; err = %v", err)
}
checkNotExist(t, zr, profileName)
checkNotExist(t, zr, traceName)
checkNotExist(t, zr, profileDuringTraceName)
})
}
func collect(ctx context.Context, meta *autoprof.ArchiveMeta, opt *autoprof.ArchiveOptions) (*zip.Reader, error) {
var buf bytes.Buffer
err := autoprof.NewZipCollector(&buf, meta, opt).Run(context.Background())
if err != nil {
return nil, fmt.Errorf("autoprof.NewZipCollector.Run: %w", err)
}
zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
if err != nil {
return nil, fmt.Errorf("zip.NewReader: %w", err)
}
return zr, nil
}
// profileIsEnabled returns whether a CPU profile is currently running.
func profileIsEnabled() bool {
err := pprof.StartCPUProfile(io.Discard)
if err != nil {
return true
}
pprof.StopCPUProfile()
return false
}