File: parser_test.go

package info (click to toggle)
golang-github-getsentry-sentry-go 0.29.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,404 kB
  • sloc: makefile: 75; sh: 21
file content (427 lines) | stat: -rw-r--r-- 13,926 bytes parent folder | download
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package traceparser

import (
	"bytes"
	"fmt"
	"runtime"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestGenerateTrace(t *testing.T) {
	stacks := make(chan string)
	go func() {
		var stacksBuffer = make([]byte, 1000)
		for {
			// Capture stacks for all existing goroutines.
			// Note: runtime.GoroutineProfile() would be better but we can't use it at the moment because
			//       it doesn't give us `gid` for each routine, see https://github.com/golang/go/issues/59663
			n := runtime.Stack(stacksBuffer, true)

			// If we couldn't read everything, increase the buffer and try again.
			if n >= len(stacksBuffer) {
				stacksBuffer = make([]byte, n*2)
			} else {
				stacks <- string(stacksBuffer[0:n])
				break
			}
		}
	}()

	t.Log(<-stacks)

	// Note: uncomment to show the output so you can update it manually in tests below.
	// t.Fail()
}

func TestParseEmpty(t *testing.T) {
	var require = require.New(t)

	require.Zero(Parse(nil).Length())
	require.Zero(Parse([]byte{}).Length())
}

var tracetext = []byte(`
goroutine 7 [running]:
github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace.func1()
	c:/dev/sentry-go/internal/traceparser/parser_test.go:23 +0x6c
created by github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace in goroutine 6
	c:/dev/sentry-go/internal/traceparser/parser_test.go:17 +0x7f

goroutine 1 [chan receive]:
testing.(*T).Run(0xc00006f6c0, {0x672288?, 0x180fd3?}, 0x6b5f98)
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1630 +0x405
testing.runTests.func1(0xa36e00?)
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:2036 +0x45
testing.tRunner(0xc00006f6c0, 0xc0000b3c88)
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1576 +0x10b
testing.runTests(0xc000035ea0?, {0xa31240, 0xcd, 0xcd}, {0xc0000befa0?, 0x102df4ae6c418?, 0xa363a0?})
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:2034 +0x489
testing.(*M).Run(0xc000035ea0)
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1906 +0x63a
main.main()
	_testmain.go:465 +0x1aa

goroutine 6 [chan send]:
github.com/getsentry/sentry-go.startProfiling.func3()
	c:/dev/sentry-go/profiler.go:46 +0x2b
github.com/getsentry/sentry-go.TestStart(0x0?)
	c:/dev/sentry-go/profiler_test.go:13 +0x3e
testing.tRunner(0xc00006f860, 0x6b5f98)
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1576 +0x10b
created by testing.(*T).Run
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1629 +0x3ea

goroutine 7 [stopping the world]:
runtime.Stack({0xc000200000, 0x100000, 0x100000}, 0x1)
	C:/Users/name/scoop/apps/go/current/src/runtime/mprof.go:1193 +0x4d
github.com/getsentry/sentry-go.(*profileRecorder).Collect(0xc00008a820)
	c:/dev/sentry-go/profiler.go:73 +0x3b
github.com/getsentry/sentry-go.startProfiling.func2()
	c:/dev/sentry-go/profiler.go:38 +0xb1
created by github.com/getsentry/sentry-go.startProfiling
	c:/dev/sentry-go/profiler.go:31 +0x36c

goroutine 19 [chan send]:
github.com/getsentry/sentry-go.startProfiling.func1()
	c:/dev/sentry-go/profiler.go:29 +0x25
...additional frames elided...
created by time.goFunc
	C:/Users/name/scoop/apps/go/current/src/time/sleep.go:176 +0x32
`)

func TestParse(t *testing.T) {
	var require = require.New(t)

	var traces = Parse(tracetext)
	var i = 0
	var checkTrace = func(id int, stack string) {
		var trace = traces.Item(i)
		require.NotNil(trace)
		require.Equal(uint64(id), trace.GoID())
		require.Equal(stack, string(trace.UniqueIdentifier()))
		i++
	}

	checkTrace(7, `github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace.func1()
	c:/dev/sentry-go/internal/traceparser/parser_test.go:23 +0x6c
created by github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace in goroutine 6
	c:/dev/sentry-go/internal/traceparser/parser_test.go:17 +0x7f`)

	checkTrace(1, `testing.(*T).Run(0xc00006f6c0, {0x672288?, 0x180fd3?}, 0x6b5f98)
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1630 +0x405
testing.runTests.func1(0xa36e00?)
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:2036 +0x45
testing.tRunner(0xc00006f6c0, 0xc0000b3c88)
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1576 +0x10b
testing.runTests(0xc000035ea0?, {0xa31240, 0xcd, 0xcd}, {0xc0000befa0?, 0x102df4ae6c418?, 0xa363a0?})
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:2034 +0x489
testing.(*M).Run(0xc000035ea0)
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1906 +0x63a
main.main()
	_testmain.go:465 +0x1aa`)

	checkTrace(6, `github.com/getsentry/sentry-go.startProfiling.func3()
	c:/dev/sentry-go/profiler.go:46 +0x2b
github.com/getsentry/sentry-go.TestStart(0x0?)
	c:/dev/sentry-go/profiler_test.go:13 +0x3e
testing.tRunner(0xc00006f860, 0x6b5f98)
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1576 +0x10b
created by testing.(*T).Run
	C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1629 +0x3ea`)

	checkTrace(7, `runtime.Stack({0xc000200000, 0x100000, 0x100000}, 0x1)
	C:/Users/name/scoop/apps/go/current/src/runtime/mprof.go:1193 +0x4d
github.com/getsentry/sentry-go.(*profileRecorder).Collect(0xc00008a820)
	c:/dev/sentry-go/profiler.go:73 +0x3b
github.com/getsentry/sentry-go.startProfiling.func2()
	c:/dev/sentry-go/profiler.go:38 +0xb1
created by github.com/getsentry/sentry-go.startProfiling
	c:/dev/sentry-go/profiler.go:31 +0x36c`)

	checkTrace(19, `github.com/getsentry/sentry-go.startProfiling.func1()
	c:/dev/sentry-go/profiler.go:29 +0x25
...additional frames elided...
created by time.goFunc
	C:/Users/name/scoop/apps/go/current/src/time/sleep.go:176 +0x32`)

	require.Equal(traces.Length(), i)
}

//nolint:dupl
func TestFrames(t *testing.T) {
	var require = require.New(t)

	var output = ""
	var traces = Parse(tracetext)
	for i := 0; i < traces.Length(); i++ {
		var trace = traces.Item(i)
		var framesIter = trace.Frames()
		output += fmt.Sprintf("Trace %d: goroutine %d with at most %d frames\n", i, trace.GoID(), framesIter.LengthUpperBound())

		for framesIter.HasNext() {
			var frame = framesIter.Next()
			output += fmt.Sprintf("  Func = %s\n", frame.Func())
			file, line := frame.File()
			output += fmt.Sprintf("  File = %s\n", file)
			output += fmt.Sprintf("  Line = %d\n", line)
		}
	}

	var expected = strings.Split(strings.TrimLeft(`
Trace 0: goroutine 7 with at most 2 frames
  Func = github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace.func1
  File = c:/dev/sentry-go/internal/traceparser/parser_test.go
  Line = 23
  Func = github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace
  File = c:/dev/sentry-go/internal/traceparser/parser_test.go
  Line = 17
Trace 1: goroutine 1 with at most 6 frames
  Func = testing.(*T).Run
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 1630
  Func = testing.runTests.func1
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 2036
  Func = testing.tRunner
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 1576
  Func = testing.runTests
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 2034
  Func = testing.(*M).Run
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 1906
  Func = main.main
  File = _testmain.go
  Line = 465
Trace 2: goroutine 6 with at most 4 frames
  Func = github.com/getsentry/sentry-go.startProfiling.func3
  File = c:/dev/sentry-go/profiler.go
  Line = 46
  Func = github.com/getsentry/sentry-go.TestStart
  File = c:/dev/sentry-go/profiler_test.go
  Line = 13
  Func = testing.tRunner
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 1576
  Func = testing.(*T).Run
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 1629
Trace 3: goroutine 7 with at most 4 frames
  Func = runtime.Stack
  File = C:/Users/name/scoop/apps/go/current/src/runtime/mprof.go
  Line = 1193
  Func = github.com/getsentry/sentry-go.(*profileRecorder).Collect
  File = c:/dev/sentry-go/profiler.go
  Line = 73
  Func = github.com/getsentry/sentry-go.startProfiling.func2
  File = c:/dev/sentry-go/profiler.go
  Line = 38
  Func = github.com/getsentry/sentry-go.startProfiling
  File = c:/dev/sentry-go/profiler.go
  Line = 31
Trace 4: goroutine 19 with at most 2 frames
  Func = github.com/getsentry/sentry-go.startProfiling.func1
  File = c:/dev/sentry-go/profiler.go
  Line = 29
  Func = time.goFunc
  File = C:/Users/name/scoop/apps/go/current/src/time/sleep.go
  Line = 176
`, "\n"), "\n")
	require.Equal(expected, strings.Split(output, "\n"))
}

//nolint:dupl
func TestFramesReversed(t *testing.T) {
	var require = require.New(t)

	var output = ""
	var traces = Parse(tracetext)
	for i := 0; i < traces.Length(); i++ {
		var trace = traces.Item(i)
		var framesIter = trace.FramesReversed()
		output += fmt.Sprintf("Trace %d: goroutine %d with at most %d frames\n", i, trace.GoID(), framesIter.LengthUpperBound())

		for framesIter.HasNext() {
			var frame = framesIter.Next()
			output += fmt.Sprintf("  Func = %s\n", frame.Func())
			file, line := frame.File()
			output += fmt.Sprintf("  File = %s\n", file)
			output += fmt.Sprintf("  Line = %d\n", line)
		}
	}

	var expected = strings.Split(strings.TrimLeft(`
Trace 0: goroutine 7 with at most 2 frames
  Func = github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace
  File = c:/dev/sentry-go/internal/traceparser/parser_test.go
  Line = 17
  Func = github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace.func1
  File = c:/dev/sentry-go/internal/traceparser/parser_test.go
  Line = 23
Trace 1: goroutine 1 with at most 6 frames
  Func = main.main
  File = _testmain.go
  Line = 465
  Func = testing.(*M).Run
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 1906
  Func = testing.runTests
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 2034
  Func = testing.tRunner
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 1576
  Func = testing.runTests.func1
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 2036
  Func = testing.(*T).Run
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 1630
Trace 2: goroutine 6 with at most 4 frames
  Func = testing.(*T).Run
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 1629
  Func = testing.tRunner
  File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
  Line = 1576
  Func = github.com/getsentry/sentry-go.TestStart
  File = c:/dev/sentry-go/profiler_test.go
  Line = 13
  Func = github.com/getsentry/sentry-go.startProfiling.func3
  File = c:/dev/sentry-go/profiler.go
  Line = 46
Trace 3: goroutine 7 with at most 4 frames
  Func = github.com/getsentry/sentry-go.startProfiling
  File = c:/dev/sentry-go/profiler.go
  Line = 31
  Func = github.com/getsentry/sentry-go.startProfiling.func2
  File = c:/dev/sentry-go/profiler.go
  Line = 38
  Func = github.com/getsentry/sentry-go.(*profileRecorder).Collect
  File = c:/dev/sentry-go/profiler.go
  Line = 73
  Func = runtime.Stack
  File = C:/Users/name/scoop/apps/go/current/src/runtime/mprof.go
  Line = 1193
Trace 4: goroutine 19 with at most 2 frames
  Func = time.goFunc
  File = C:/Users/name/scoop/apps/go/current/src/time/sleep.go
  Line = 176
  Func = github.com/getsentry/sentry-go.startProfiling.func1
  File = c:/dev/sentry-go/profiler.go
  Line = 29
`, "\n"), "\n")
	require.Equal(expected, strings.Split(output, "\n"))
}

func BenchmarkEqualBytes(b *testing.B) {
	lines := bytes.Split(tracetext, lineSeparator)
	var framesElided = []byte(framesElided)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for n := 0; n < len(lines); n++ {
			if bytes.Equal(lines[n], framesElided) {
				break
			}
		}
	}
}

// Benchmark results: this is the best performing implementation.
func BenchmarkStringEqual(b *testing.B) {
	lines := bytes.Split(tracetext, lineSeparator)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for n := 0; n < len(lines); n++ {
			if string(lines[n]) == framesElided {
				break
			}
		}
	}
}

func BenchmarkEqualPrefix(b *testing.B) {
	lines := bytes.Split(tracetext, lineSeparator)
	var framesElided = []byte(framesElided)
	var ln = len(framesElided)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for n := 0; n < len(lines); n++ {
			if len(lines[n]) == ln && bytes.HasPrefix(lines[n], framesElided) {
				break
			}
		}
	}
}

func BenchmarkFullParse(b *testing.B) {
	b.SetBytes(int64(len(tracetext)))
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		var traces = Parse(tracetext)
		for i := traces.Length() - 1; i >= 0; i-- {
			var trace = traces.Item(i)
			_ = trace.GoID()

			var iter = trace.FramesReversed()
			_ = iter.LengthUpperBound()
			for iter.HasNext() {
				var frame = iter.Next()
				_ = frame.Func()
				_, _ = frame.File()
			}
		}
	}
}

func BenchmarkFramesIterator(b *testing.B) {
	b.ReportAllocs()
	var traces = Parse(tracetext)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for i := traces.Length() - 1; i >= 0; i-- {
			var trace = traces.Item(i)
			var iter = trace.Frames()
			_ = iter.LengthUpperBound()
			for iter.HasNext() {
				var frame = iter.Next()
				_ = frame.Func()
				_, _ = frame.File()
			}
		}
	}
}

func BenchmarkFramesReversedIterator(b *testing.B) {
	b.ReportAllocs()
	var traces = Parse(tracetext)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for i := traces.Length() - 1; i >= 0; i-- {
			var trace = traces.Item(i)
			var iter = trace.FramesReversed()
			_ = iter.LengthUpperBound()
			for iter.HasNext() {
				var frame = iter.Next()
				_ = frame.Func()
				_, _ = frame.File()
			}
		}
	}
}

func BenchmarkSplitOnly(b *testing.B) {
	b.SetBytes(int64(len(tracetext)))
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		var traces = Parse(tracetext)
		for i := traces.Length() - 1; i >= 0; i-- {
			var trace = traces.Item(i)
			_ = trace.UniqueIdentifier()
		}
	}
}