-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tracer_test.go
72 lines (64 loc) · 1.98 KB
/
tracer_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
package tracer_test
import (
"context"
"strings"
"testing"
. "github.com/kamilsk/tracer"
)
func TestTrace_Start(t *testing.T) {
(*Trace)(nil).Start("no panic")
(&Trace{}).Start("one allocation")
}
func TestTrace_String(t *testing.T) {
t.Run("nil pointer", func(t *testing.T) {
if expected, obtained := "", (*Trace)(nil).String(); expected != obtained {
t.Errorf("\n expected: %+#v \n obtained: %+#v", expected, obtained)
}
})
t.Run("allocations", func(t *testing.T) {
trace := &Trace{}
if expected, obtained := "allocates at call stack: 0, detailed call stack: ~",
trace.String(); !strings.Contains(obtained, expected) {
t.Errorf("\n expected: %+#v \n obtained: %+#v", expected, obtained)
}
call := trace.Start("fn call")
call.Checkpoint("checkpoint")
call.Stop()
if expected, obtained := "allocates at call stack: 1", trace.String(); !strings.Contains(obtained, expected) {
t.Errorf("\n expected: %+#v \n obtained: %+#v", expected, obtained)
}
})
}
func TestCall_Checkpoint(t *testing.T) {
(*Call)(nil).Checkpoint("no panic")
(&Call{}).Checkpoint("one allocation")
}
func TestCall_Stop(t *testing.T) {
(*Call)(nil).Stop()
(&Call{}).Stop()
}
// BenchmarkTracing/overhead-12 2000000 616 ns/op 144 B/op 9 allocs/op
// BenchmarkTracing/silent-12 200000 7478 ns/op 2320 B/op 27 allocs/op
// BenchmarkTracing/active-12 200000 9221 ns/op 4448 B/op 48 allocs/op
func BenchmarkTracing(b *testing.B) {
b.Run("overhead", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
traceRoot(context.Background())
}
})
b.Run("silent", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
traceRoot(Inject(context.Background(), make([]*Call, 0, 9)))
}
})
b.Run("active", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ctx := Inject(context.Background(), make([]*Call, 0, 9))
traceRoot(ctx)
_ = Fetch(ctx).String()
}
})
}