1
0
mirror of https://github.com/golang/go synced 2024-09-29 10:14:29 -06:00

debug/gosym: add benchmark

Use a Go 1.15 executable for the benchmark, because it is handy.
Most of the code paths are shared for Go 1.2+.

Change-Id: Id7ddc76a05d76335108c58ff9f1ab2ff837b7227
Reviewed-on: https://go-review.googlesource.com/c/go/+/353131
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Josh Bleecher Snyder 2021-09-29 10:16:47 -07:00
parent e213c72fb9
commit 5930cff093

View File

@ -267,7 +267,8 @@ func TestPCLine(t *testing.T) {
}
}
// Test that we can parse a pclntab from 1.15.
// read115Executable returns a hello world executable compiled by Go 1.15.
//
// The file was compiled in /tmp/hello.go:
// [BEGIN]
// package main
@ -276,25 +277,30 @@ func TestPCLine(t *testing.T) {
// println("hello")
// }
// [END]
func Test115PclnParsing(t *testing.T) {
func read115Executable(tb testing.TB) []byte {
zippedDat, err := os.ReadFile("testdata/pcln115.gz")
if err != nil {
t.Fatal(err)
tb.Fatal(err)
}
var gzReader *gzip.Reader
gzReader, err = gzip.NewReader(bytes.NewBuffer(zippedDat))
if err != nil {
t.Fatal(err)
tb.Fatal(err)
}
var dat []byte
dat, err = io.ReadAll(gzReader)
if err != nil {
t.Fatal(err)
tb.Fatal(err)
}
return dat
}
// Test that we can parse a pclntab from 1.15.
func Test115PclnParsing(t *testing.T) {
dat := read115Executable(t)
const textStart = 0x1001000
pcln := NewLineTable(dat, textStart)
var tab *Table
tab, err = NewTable(nil, pcln)
tab, err := NewTable(nil, pcln)
if err != nil {
t.Fatal(err)
}
@ -314,3 +320,74 @@ func Test115PclnParsing(t *testing.T) {
t.Fatalf("expected to parse name as main.main, got %v", f.Name)
}
}
var (
sinkLineTable *LineTable
sinkTable *Table
)
func Benchmark115(b *testing.B) {
dat := read115Executable(b)
const textStart = 0x1001000
b.Run("NewLineTable", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
sinkLineTable = NewLineTable(dat, textStart)
}
})
pcln := NewLineTable(dat, textStart)
b.Run("NewTable", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var err error
sinkTable, err = NewTable(nil, pcln)
if err != nil {
b.Fatal(err)
}
}
})
tab, err := NewTable(nil, pcln)
if err != nil {
b.Fatal(err)
}
b.Run("LineToPC", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var f *Func
var pc uint64
pc, f, err = tab.LineToPC("/tmp/hello.go", 3)
if err != nil {
b.Fatal(err)
}
if pcln.version != ver12 {
b.Fatalf("want version=%d, got %d", ver12, pcln.version)
}
if pc != 0x105c280 {
b.Fatalf("want pc=0x105c280, got 0x%x", pc)
}
if f.Name != "main.main" {
b.Fatalf("want name=main.main, got %q", f.Name)
}
}
})
b.Run("PCToLine", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
file, line, fn := tab.PCToLine(0x105c280)
if file != "/tmp/hello.go" {
b.Fatalf("want name=/tmp/hello.go, got %q", file)
}
if line != 3 {
b.Fatalf("want line=3, got %d", line)
}
if fn.Name != "main.main" {
b.Fatalf("want name=main.main, got %q", fn.Name)
}
}
})
}