Skip to content

Commit 8dfc82d

Browse files
committed
util/stop: add tracing case to stopper benchmarks
This is helpful for measuring impact of changes when tracing is enabled. Epic: None Release note: None
1 parent 3824cc2 commit 8dfc82d

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

pkg/util/stop/stopper_bench_test.go

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"sync"
1111
"testing"
1212

13+
"github.com/cockroachdb/cockroach/pkg/testutils"
1314
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
15+
"github.com/cockroachdb/cockroach/pkg/util/tracing"
1416
)
1517

1618
func BenchmarkStopper(b *testing.B) {
@@ -19,52 +21,64 @@ func BenchmarkStopper(b *testing.B) {
1921
s := NewStopper()
2022
defer s.Stop(ctx)
2123

22-
opts := TaskOpts{
23-
TaskName: "testTask",
24-
}
24+
tracer := tracing.NewTracer()
2525

26-
b.Run("RunTask", func(b *testing.B) {
27-
b.ReportAllocs()
28-
var wg sync.WaitGroup // for fairness
29-
wg.Add(b.N)
30-
for i := 0; i < b.N; i++ {
31-
if err := s.RunTask(ctx, opts.TaskName, func(context.Context) { wg.Done() }); err != nil {
32-
b.Fatal(err)
33-
}
26+
testutils.RunTrueAndFalse(b, "tracing", func(b *testing.B, tracingEnabled bool) {
27+
ctx := context.Background()
28+
opts := TaskOpts{
29+
TaskName: "testTask",
3430
}
35-
wg.Wait() // noop
36-
})
37-
38-
b.Run("AsyncTaskEx", func(b *testing.B) {
39-
b.ReportAllocs()
40-
var wg sync.WaitGroup
41-
for i := 0; i < b.N; i++ {
42-
wg.Add(1)
43-
if err := s.RunAsyncTaskEx(ctx, opts, func(ctx context.Context) {
44-
defer wg.Done()
45-
}); err != nil {
46-
b.Fatal(err)
47-
}
48-
wg.Wait()
31+
if tracingEnabled {
32+
opts.SpanOpt = ChildSpan
33+
sp := tracer.StartSpan("test span")
34+
defer sp.Finish()
35+
ctx = tracing.ContextWithSpan(ctx, sp)
4936
}
50-
})
5137

52-
hdlf := func(ctx context.Context, hdl *Handle, wg *sync.WaitGroup) {
53-
defer hdl.Activate(ctx).Release(ctx)
54-
defer wg.Done()
55-
}
38+
b.Run("RunTask", func(b *testing.B) {
39+
b.ReportAllocs()
40+
var wg sync.WaitGroup // for fairness
41+
wg.Add(b.N)
42+
for i := 0; i < b.N; i++ {
43+
if err := s.RunTask(ctx, opts.TaskName, func(context.Context) { wg.Done() }); err != nil {
44+
b.Fatal(err)
45+
}
46+
}
47+
wg.Wait() // noop
48+
})
5649

57-
b.Run("Handle", func(b *testing.B) {
58-
b.ReportAllocs()
59-
var wg sync.WaitGroup
60-
for i := 0; i < b.N; i++ {
61-
ctx, hdl, err := s.GetHandle(ctx, opts)
62-
if err != nil {
63-
b.Fatal(err)
50+
b.Run("AsyncTaskEx", func(b *testing.B) {
51+
b.ReportAllocs()
52+
var wg sync.WaitGroup
53+
for i := 0; i < b.N; i++ {
54+
wg.Add(1)
55+
if err := s.RunAsyncTaskEx(ctx, opts, func(ctx context.Context) {
56+
defer wg.Done()
57+
}); err != nil {
58+
b.Fatal(err)
59+
}
60+
wg.Wait()
6461
}
65-
wg.Add(1)
66-
go hdlf(ctx, hdl, &wg)
67-
wg.Wait()
62+
})
63+
64+
hdlf := func(ctx context.Context, hdl *Handle, wg *sync.WaitGroup) {
65+
defer hdl.Activate(ctx).Release(ctx)
66+
defer wg.Done()
6867
}
68+
69+
b.Run("Handle", func(b *testing.B) {
70+
b.ReportAllocs()
71+
var wg sync.WaitGroup
72+
for i := 0; i < b.N; i++ {
73+
ctx, hdl, err := s.GetHandle(ctx, opts)
74+
if err != nil {
75+
b.Fatal(err)
76+
}
77+
wg.Add(1)
78+
go hdlf(ctx, hdl, &wg)
79+
wg.Wait()
80+
}
81+
})
6982
})
83+
7084
}

0 commit comments

Comments
 (0)