1
0
mirror of https://github.com/DataDog/go-profiler-notes.git synced 2025-07-15 23:54:16 +02:00
Files
Felix Geisendörfer f8f6e88e3a callers-func-length
2021-04-03 12:47:19 +02:00

158 lines
1.6 KiB
Go

package main
import (
"runtime"
"testing"
)
var callers = make([]uintptr, 32)
func BenchmarkCallers(b *testing.B) {
b.Run("short", func(b *testing.B) {
short(func() {
for i := 0; i < b.N; i++ {
runtime.Callers(0, callers)
}
})
})
b.Run("loop", func(b *testing.B) {
loop(func() {
for i := 0; i < b.N; i++ {
runtime.Callers(0, callers)
}
})
})
b.Run("long", func(b *testing.B) {
long(func() {
for i := 0; i < b.N; i++ {
runtime.Callers(0, callers)
}
})
})
}
func short(fn func()) {
other()
fn()
}
func loop(fn func()) {
for i := 0; i < 1000; i++ {
other()
}
fn()
}
func long(fn func()) {
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
other()
fn()
}
func other() int {
m := map[int]string{}
m[0] = "foo"
m[100] = "bar"
return len(m)
}