forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc_test.go
100 lines (84 loc) · 2.3 KB
/
alloc_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
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package tracing
import (
"context"
"fmt"
"testing"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/logtags"
)
// BenchmarkTracer_StartSpanCtx primarily helps keep
// tab on the allocation counts for starting a Span.
//
// This benchmark explicitly excludes construction of
// the SpanOptions, which require allocations as well.
func BenchmarkTracer_StartSpanCtx(b *testing.B) {
ctx := context.Background()
tr := NewTracer()
sv := settings.Values{}
tr.Configure(&sv)
staticLogTags := logtags.Buffer{}
staticLogTags.Add("foo", "bar")
b.ResetTimer()
parSp := tr.StartSpan("one-off", WithForceRealSpan())
defer parSp.Finish()
for _, tc := range []struct {
name string
opts []SpanOption
}{
{"none", nil},
{"real", []SpanOption{
WithForceRealSpan(),
}},
{"real,logtag", []SpanOption{
WithForceRealSpan(), WithLogTags(&staticLogTags),
}},
{"real,autoparent", []SpanOption{
WithForceRealSpan(), WithParentAndAutoCollection(parSp),
}},
{"real,manualparent", []SpanOption{
WithForceRealSpan(), WithParentAndManualCollection(parSp.Meta()),
}},
} {
b.Run(fmt.Sprintf("opts=%s", tc.name), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
newCtx, sp := tr.StartSpanCtx(ctx, "benching", tc.opts...)
_ = newCtx
sp.Finish() // clean up
}
})
}
}
// BenchmarkSpan_GetRecording microbenchmarks GetRecording.
func BenchmarkSpan_GetRecording(b *testing.B) {
var sv settings.Values
tr := NewTracer()
tr.Configure(&sv)
sp := tr.StartSpan("foo", WithForceRealSpan())
run := func(b *testing.B, sp *Span) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = sp.GetRecording()
}
}
b.ResetTimer()
b.Run("root-only", func(b *testing.B) {
run(b, sp)
})
child := tr.StartSpan("bar", WithParentAndAutoCollection(sp), WithForceRealSpan())
b.Run("child-only", func(b *testing.B) {
run(b, child)
})
b.Run("root-child", func(b *testing.B) {
run(b, sp)
})
}