From 5930cff093c44b86a299c1841ab4e6a2c7a239d2 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Wed, 29 Sep 2021 10:16:47 -0700 Subject: [PATCH] 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 Trust: Brad Fitzpatrick Run-TryBot: Josh Bleecher Snyder Reviewed-by: Brad Fitzpatrick TryBot-Result: Go Bot --- src/debug/gosym/pclntab_test.go | 91 ++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 7 deletions(-) diff --git a/src/debug/gosym/pclntab_test.go b/src/debug/gosym/pclntab_test.go index 7347139b5df..8589227157d 100644 --- a/src/debug/gosym/pclntab_test.go +++ b/src/debug/gosym/pclntab_test.go @@ -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) + } + } + }) +}