Skip to content

Commit

Permalink
go/ir: remove statistics collection from test
Browse files Browse the repository at this point in the history
We've never cared about these statistics when running the test. Now it
is much simpler to tell what the test is actually testing: that building
the IR form of all packages in stdlib doesn't panic.
  • Loading branch information
dominikh committed Apr 24, 2024
1 parent 1d23b79 commit 3cbb542
Showing 1 changed file with 7 additions and 60 deletions.
67 changes: 7 additions & 60 deletions go/ir/stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,88 +17,35 @@ package ir_test
// Run with "go test -cpu=8 to" set GOMAXPROCS.

import (
"go/token"
"runtime"
"testing"
"time"

"honnef.co/go/tools/go/ir"
"honnef.co/go/tools/go/ir/irutil"

"golang.org/x/tools/go/packages"
)

func bytesAllocated() uint64 {
runtime.GC()
var stats runtime.MemStats
runtime.ReadMemStats(&stats)
return stats.TotalAlloc
}

func TestStdlib(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode; too slow (golang.org/issue/14113)")
}

// Load, parse and type-check the program.
t0 := time.Now()
alloc0 := bytesAllocated()

cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedTypesSizes,
}
pkgs, err := packages.Load(cfg, "std")
if err != nil {
t.Fatalf("Load failed: %v", err)
}
allocLoad := bytesAllocated() - alloc0
dLoad := time.Since(t0)
for _, pkg := range pkgs {
if len(pkg.Errors) != 0 {
t.Fatalf("Load failed: %v", pkg.Errors[0])
}
}

alloc0 = bytesAllocated()
var mode ir.BuilderMode
// Comment out these lines during benchmarking. Approx IR build costs are noted.
mode |= ir.SanityCheckFunctions // + 2% space, + 4% time
mode |= ir.GlobalDebug // +30% space, +18% time
t0 = time.Now()
mode |= ir.SanityCheckFunctions
mode |= ir.GlobalDebug
prog, _ := irutil.Packages(pkgs, mode, nil)
dCreate := time.Since(t0)

t0 = time.Now()
prog.Build()
dBuild := time.Since(t0)

allFuncs := irutil.AllFunctions(prog)
numFuncs := len(allFuncs)

// Dump some statistics.
var numInstrs int
for fn := range allFuncs {
for _, b := range fn.Blocks {
numInstrs += len(b.Instrs)
}
}
allocBuild := bytesAllocated() - alloc0

// determine line count
var lineCount int
pkgs[0].Fset.Iterate(func(f *token.File) bool {
lineCount += f.LineCount()
return true
})

// NB: when benchmarking, don't forget to clear the debug +
// sanity builder flags for better performance.

t.Log("GOMAXPROCS: ", runtime.GOMAXPROCS(0))
t.Log("#Source lines: ", lineCount)
t.Log("Load/parse/typecheck: ", dLoad)
t.Log("IR create: ", dCreate)
t.Log("IR build: ", dBuild)

// IR stats:
t.Log("#Packages: ", len(pkgs))
t.Log("#Functions: ", numFuncs)
t.Log("#Instructions: ", numInstrs)
t.Log("#MB AST+types: ", allocLoad/1e6)
t.Log("#MB IR: ", allocBuild/1e6)
}

0 comments on commit 3cbb542

Please sign in to comment.