Debian Codegraph @

stack/bucket_test.go

raw
1 // Copyright 2018 Marc-Antoine Ruel. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package stack
6
7 import (
8 "bytes"
9 "io"
10 "strings"
11 "testing"
12
13 "github.com/google/go-cmp/cmp"
14 "github.com/maruel/panicparse/v2/internal/internaltest"
15 )
16
17 func TestAggregateNotAggressive(t *testing.T) {
18 t.Parallel()
19 // 2 goroutines with similar but not exact same signature.
20 data := []string{
21 "panic: runtime error: index out of range",
22 "",
23 "goroutine 6 [chan receive]:",
24 "main.func·001({0x11000000, 2}, 3)",
25 "\t/gopath/src/github.com/maruel/panicparse/stack/stack.go:72 +0x49",
26 "",
27 "goroutine 7 [chan receive]:",
28 "main.func·001({0x21000000, 2}, 3)",
29 "\t/gopath/src/github.com/maruel/panicparse/stack/stack.go:72 +0x49",
30 "",
31 }
32 s, suffix, err := ScanSnapshot(bytes.NewBufferString(strings.Join(data, "\n")), io.Discard, defaultOpts())
33 if err != io.EOF {
34 t.Fatal(err)
35 }
36 if s == nil {
37 t.Fatal("expected snapshot")
38 }
39 want := []*Bucket{
40 {
41 Signature: Signature{
42 State: "chan receive",
43 Stack: Stack{
44 Calls: []Call{
45 newCall(
46 "main.func·001",
47 Args{Values: []Arg{
48 {IsAggregate: true, Fields: Args{
49 Values: []Arg{{Value: 0x11000000, IsPtr: true}, {Value: 2}},
50 }},
51 {Value: 3},
52 }},
53 "/gopath/src/github.com/maruel/panicparse/stack/stack.go",
54 72),
55 },
56 },
57 },
58 IDs: []int{6},
59 First: true,
60 },
61 {
62 Signature: Signature{
63 State: "chan receive",
64 Stack: Stack{
65 Calls: []Call{
66 newCall(
67 "main.func·001",
68 Args{Values: []Arg{
69 {IsAggregate: true, Fields: Args{
70 Values: []Arg{{Value: 0x21000000, Name: "#1", IsPtr: true}, {Value: 2}},
71 }},
72 {Value: 3},
73 }},
74 "/gopath/src/github.com/maruel/panicparse/stack/stack.go",
75 72),
76 },
77 },
78 },
79 IDs: []int{7},
80 },
81 }
82 a := s.Aggregate(ExactLines)
83 compareBuckets(t, want, a.Buckets)
84 if a.Snapshot != s {
85 t.Fatal("unexpected snapshot")
86 }
87 compareString(t, "", string(suffix))
88 }
89
90 func TestAggregateExactMatching(t *testing.T) {
91 t.Parallel()
92 // 2 goroutines with the exact same signature.
93 data := []string{
94 "panic: runtime error: index out of range",
95 "",
96 "goroutine 6 [chan receive]:",
97 "main.func·001({0x11000000, 2}, 3)",
98 "\t/gopath/src/github.com/maruel/panicparse/stack/stack.go:72 +0x49",
99 "created by main.mainImpl",
100 "\t/gopath/src/github.com/maruel/panicparse/stack/stack.go:74 +0xeb",
101 "",
102 "goroutine 7 [chan receive]:",
103 "main.func·001({0x11000000, 2}, 3)",
104 "\t/gopath/src/github.com/maruel/panicparse/stack/stack.go:72 +0x49",
105 "created by main.mainImpl",
106 "\t/gopath/src/github.com/maruel/panicparse/stack/stack.go:74 +0xeb",
107 "",
108 }
109 s, suffix, err := ScanSnapshot(bytes.NewBufferString(strings.Join(data, "\n")), io.Discard, defaultOpts())
110 if err != io.EOF {
111 t.Fatal(err)
112 }
113 if s == nil {
114 t.Fatal("expected snapshot")
115 }
116 want := []*Bucket{
117 {
118 Signature: Signature{
119 State: "chan receive",
120 CreatedBy: Stack{
121 Calls: []Call{
122 newCall(
123 "main.mainImpl",
124 Args{},
125 "/gopath/src/github.com/maruel/panicparse/stack/stack.go",
126 74),
127 },
128 },
129 Stack: Stack{
130 Calls: []Call{
131 newCall(
132 "main.func·001",
133 Args{Values: []Arg{
134 {IsAggregate: true, Fields: Args{
135 Values: []Arg{{Value: 0x11000000, Name: "#1", IsPtr: true}, {Value: 2}},
136 }},
137 {Value: 3},
138 }},
139 "/gopath/src/github.com/maruel/panicparse/stack/stack.go",
140 72),
141 },
142 },
143 },
144 IDs: []int{6, 7},
145 First: true,
146 },
147 }
148 compareBuckets(t, want, s.Aggregate(ExactLines).Buckets)
149 compareString(t, "", string(suffix))
150 }
151
152 func TestAggregateAggressive(t *testing.T) {
153 t.Parallel()
154 // 3 goroutines with similar signatures.
155 data := []string{
156 "panic: runtime error: index out of range",
157 "",
158 "goroutine 6 [chan receive, 10 minutes]:",
159 "main.func·001({0x11000000, 2}, 3)",
160 "\t/gopath/src/github.com/maruel/panicparse/stack/stack.go:72 +0x49",
161 "",
162 "goroutine 7 [chan receive, 50 minutes]:",
163 "main.func·001({0x21000000, 2}, 3)",
164 "\t/gopath/src/github.com/maruel/panicparse/stack/stack.go:72 +0x49",
165 "",
166 "goroutine 8 [chan receive, 100 minutes]:",
167 "main.func·001({0x31000000, 2}, 3)",
168 "\t/gopath/src/github.com/maruel/panicparse/stack/stack.go:72 +0x49",
169 "",
170 }
171 s, suffix, err := ScanSnapshot(bytes.NewBufferString(strings.Join(data, "\n")), io.Discard, defaultOpts())
172 if err != io.EOF {
173 t.Fatal(err)
174 }
175 if s == nil {
176 t.Fatal("expected snapshot")
177 }
178 want := []*Bucket{
179 {
180 Signature: Signature{
181 State: "chan receive",
182 SleepMin: 10,
183 SleepMax: 100,
184 Stack: Stack{
185 Calls: []Call{
186 newCall(
187 "main.func·001",
188 Args{Values: []Arg{
189 {IsAggregate: true, Fields: Args{
190 Values: []Arg{{Value: 0x11000000, Name: "*", IsPtr: true}, {Value: 2}},
191 }},
192 {Value: 3},
193 }},
194 "/gopath/src/github.com/maruel/panicparse/stack/stack.go",
195 72),
196 },
197 },
198 },
199 IDs: []int{6, 7, 8},
200 First: true,
201 },
202 }
203 compareBuckets(t, want, s.Aggregate(AnyPointer).Buckets)
204 compareString(t, "", string(suffix))
205 }
206
207 func TestAggregateDeadlockPanic(t *testing.T) {
208 t.Parallel()
209 // Test for crash found at https://github.com/maruel/panicparse/issues/56.
210 data := []string{
211 "panic: deadlock detected at fmut",
212 "",
213 "goroutine 11 [select, 55 minutes]:",
214 "foo.Bar()",
215 " foo/foo.go:467 +0x2b8",
216 "foo.baz(0x3)",
217 " foo/foo.go:643 +0x69",
218 "created by main",
219 " foo/foo.go:631 +0x4b",
220 "",
221 "goroutine 52 [select, 55 minutes]:",
222 "foo.Bar()",
223 " foo/foo.go:467 +0x2b8",
224 "created by bozo",
225 " foo/foo.go:420 +0x33",
226 "",
227 "goroutine 55 [select, 55 minutes]:",
228 "foo.Bar()",
229 " foo/foo.go:467 +0x2b8",
230 "foo.baz(0x1)",
231 " foo/foo.go:643 +0x69",
232 "created by main",
233 " foo/foo.go:631 +0x4b",
234 }
235 s, suffix, err := ScanSnapshot(bytes.NewBufferString(strings.Join(data, "\n")), io.Discard, defaultOpts())
236 if err != io.EOF {
237 t.Fatal(err)
238 }
239 if s == nil {
240 t.Fatal("expected snapshot")
241 }
242 want := []*Bucket{
243 {
244 Signature: Signature{
245 State: "select",
246 CreatedBy: Stack{Calls: []Call{
247 {
248 Func: Func{Complete: "main", Name: "main"},
249 RemoteSrcPath: "foo/foo.go",
250 Line: 631,
251 SrcName: "foo.go",
252 },
253 }},
254 SleepMin: 55,
255 SleepMax: 55,
256 Stack: Stack{
257 Calls: []Call{
258 {
259 Func: Func{
260 Complete: "foo.Bar",
261 ImportPath: "foo",
262 DirName: "foo",
263 Name: "Bar",
264 IsExported: true,
265 },
266 RemoteSrcPath: "foo/foo.go",
267 Line: 467,
268 SrcName: "foo.go",
269 ImportPath: "foo",
270 },
271 {
272 Func: Func{Complete: "foo.baz", ImportPath: "foo", DirName: "foo", Name: "baz"},
273 Args: Args{Values: []Arg{{Value: 3}}},
274 RemoteSrcPath: "foo/foo.go",
275 Line: 643,
276 SrcName: "foo.go",
277 ImportPath: "foo",
278 },
279 },
280 },
281 },
282 IDs: []int{11},
283 First: true,
284 },
285 {
286 Signature: Signature{
287 State: "select",
288 CreatedBy: Stack{Calls: []Call{
289 {
290 Func: Func{Complete: "main", Name: "main"},
291 RemoteSrcPath: "foo/foo.go",
292 Line: 631,
293 SrcName: "foo.go",
294 },
295 }},
296 SleepMin: 55,
297 SleepMax: 55,
298 Stack: Stack{
299 Calls: []Call{
300 {
301 Func: Func{
302 Complete: "foo.Bar",
303 ImportPath: "foo",
304 DirName: "foo",
305 Name: "Bar",
306 IsExported: true,
307 },
308 RemoteSrcPath: "foo/foo.go",
309 Line: 467,
310 SrcName: "foo.go",
311 ImportPath: "foo",
312 },
313 {
314 Func: Func{Complete: "foo.baz", ImportPath: "foo", DirName: "foo", Name: "baz"},
315 Args: Args{Values: []Arg{{Value: 1}}},
316 RemoteSrcPath: "foo/foo.go",
317 Line: 643,
318 SrcName: "foo.go",
319 ImportPath: "foo",
320 },
321 },
322 },
323 },
324 IDs: []int{55},
325 },
326 {
327 Signature: Signature{
328 State: "select",
329 CreatedBy: Stack{
330 Calls: []Call{
331 {
332 Func: Func{Complete: "bozo", Name: "bozo"},
333 RemoteSrcPath: "foo/foo.go",
334 Line: 420,
335 SrcName: "foo.go",
336 },
337 },
338 },
339 SleepMin: 55,
340 SleepMax: 55,
341 Stack: Stack{
342 Calls: []Call{
343 {
344 Func: Func{
345 Complete: "foo.Bar",
346 ImportPath: "foo",
347 DirName: "foo",
348 Name: "Bar",
349 IsExported: true,
350 },
351 RemoteSrcPath: "foo/foo.go",
352 Line: 467,
353 SrcName: "foo.go",
354 ImportPath: "foo",
355 },
356 },
357 },
358 },
359 IDs: []int{52},
360 },
361 }
362 compareBuckets(t, want, s.Aggregate(AnyPointer).Buckets)
363 compareString(t, "", string(suffix))
364 }
365
366 func BenchmarkAggregate(b *testing.B) {
367 b.ReportAllocs()
368 s, suffix, err := ScanSnapshot(bytes.NewReader(internaltest.StaticPanicwebOutput()), io.Discard, defaultOpts())
369 if err != io.EOF {
370 b.Fatal(err)
371 }
372 if s == nil {
373 b.Fatal("missing context")
374 }
375 if string(suffix) != "" {
376 b.Fatalf("unexpected suffix: %q", string(suffix))
377 }
378 b.ResetTimer()
379 for i := 0; i < b.N; i++ {
380 buckets := s.Aggregate(AnyPointer).Buckets
381 if len(buckets) < 5 {
382 b.Fatal("expected more buckets")
383 }
384 }
385 }
386
387 func compareBuckets(t *testing.T, want, got []*Bucket) {
388 if diff := cmp.Diff(want, got); diff != "" {
389 t.Helper()
390 t.Fatalf("Bucket mismatch (-want +got):\n%s", diff)
391 }
392 }