| 1 |
// Copyright 2025 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package runtime_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"runtime" |
| 9 |
"strconv" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func BenchmarkTraceStack(b *testing.B) { |
| 14 |
for _, stackDepth := range []int{1, 10, 100} { |
| 15 |
b.Run("stackDepth="+strconv.Itoa(stackDepth), func(b *testing.B) { |
| 16 |
benchmarkTraceStack(b, stackDepth) |
| 17 |
}) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
func benchmarkTraceStack(b *testing.B, stackDepth int) { |
| 22 |
var tab runtime.TraceStackTable |
| 23 |
defer tab.Reset() |
| 24 |
|
| 25 |
wait := make(chan struct{}) |
| 26 |
ready := make(chan struct{}) |
| 27 |
done := make(chan struct{}) |
| 28 |
var gp *runtime.G |
| 29 |
go func() { |
| 30 |
gp = runtime.Getg() |
| 31 |
useStackAndCall(stackDepth, func() { |
| 32 |
ready <- struct{}{} |
| 33 |
<-wait |
| 34 |
}) |
| 35 |
done <- struct{}{} |
| 36 |
}() |
| 37 |
<-ready |
| 38 |
|
| 39 |
for b.Loop() { |
| 40 |
runtime.TraceStack(gp, &tab) |
| 41 |
} |
| 42 |
|
| 43 |
// Clean up. |
| 44 |
wait <- struct{}{} |
| 45 |
<-done |
| 46 |
} |