Skip to content

Commit

Permalink
go/packages: add BenchmarkNetHTTP
Browse files Browse the repository at this point in the history
It reports the elapsed time of LoadAllSyntax on net/http.

Change-Id: I122d188465dc27cd4a76814af9989c691ee03acc
Reviewed-on: https://go-review.googlesource.com/c/tools/+/625095
Auto-Submit: Alan Donovan <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
adonovan authored and gopherbot committed Nov 4, 2024
1 parent ad28b93 commit f0379e0
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions go/packages/stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,33 @@ func TestStdlibMetadata(t *testing.T) {
t.Log("Metadata: ", t1.Sub(t0)) // ~800ms on 12 threads
t.Log("#MB: ", int64(memstats.Alloc-alloc)/1000000) // ~1MB
}

// BenchmarkNetHTTP measures the time to load/parse/typecheck the
// net/http package and all dependencies.
func BenchmarkNetHTTP(b *testing.B) {
testenv.NeedsGoPackages(b)
b.ReportAllocs()

var bytes int64

for i := range b.N {
cfg := &packages.Config{Mode: packages.LoadAllSyntax}
pkgs, err := packages.Load(cfg, "net/http")
if err != nil {
b.Fatalf("failed to load metadata: %v", err)
}
if packages.PrintErrors(pkgs) > 0 {
b.Fatal("there were errors loading net/http")
}

if i == 0 {
packages.Visit(pkgs, nil, func(pkg *packages.Package) {
for _, f := range pkg.Syntax {
bytes += int64(f.FileEnd - f.FileStart)
}
})
}
}

b.SetBytes(bytes) // total source bytes
}

0 comments on commit f0379e0

Please sign in to comment.