Skip to content

Commit f0379e0

Browse files
adonovangopherbot
authored andcommitted
go/packages: add BenchmarkNetHTTP
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]>
1 parent ad28b93 commit f0379e0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

go/packages/stdlib_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,33 @@ func TestStdlibMetadata(t *testing.T) {
5050
t.Log("Metadata: ", t1.Sub(t0)) // ~800ms on 12 threads
5151
t.Log("#MB: ", int64(memstats.Alloc-alloc)/1000000) // ~1MB
5252
}
53+
54+
// BenchmarkNetHTTP measures the time to load/parse/typecheck the
55+
// net/http package and all dependencies.
56+
func BenchmarkNetHTTP(b *testing.B) {
57+
testenv.NeedsGoPackages(b)
58+
b.ReportAllocs()
59+
60+
var bytes int64
61+
62+
for i := range b.N {
63+
cfg := &packages.Config{Mode: packages.LoadAllSyntax}
64+
pkgs, err := packages.Load(cfg, "net/http")
65+
if err != nil {
66+
b.Fatalf("failed to load metadata: %v", err)
67+
}
68+
if packages.PrintErrors(pkgs) > 0 {
69+
b.Fatal("there were errors loading net/http")
70+
}
71+
72+
if i == 0 {
73+
packages.Visit(pkgs, nil, func(pkg *packages.Package) {
74+
for _, f := range pkg.Syntax {
75+
bytes += int64(f.FileEnd - f.FileStart)
76+
}
77+
})
78+
}
79+
}
80+
81+
b.SetBytes(bytes) // total source bytes
82+
}

0 commit comments

Comments
 (0)