github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/ssa/stdlib_test.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Incomplete source tree on Android.
     6  
     7  //go:build !android
     8  // +build !android
     9  
    10  package ssa_test
    11  
    12  // This file runs the SSA builder in sanity-checking mode on all
    13  // packages beneath $GOROOT and prints some summary information.
    14  //
    15  // Run with "go test -cpu=8 to" set GOMAXPROCS.
    16  
    17  import (
    18  	"go/ast"
    19  	"go/token"
    20  	"runtime"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/powerman/golang-tools/go/ast/inspector"
    25  	"github.com/powerman/golang-tools/go/packages"
    26  	"github.com/powerman/golang-tools/go/ssa"
    27  	"github.com/powerman/golang-tools/go/ssa/ssautil"
    28  	"github.com/powerman/golang-tools/internal/testenv"
    29  	"github.com/powerman/golang-tools/internal/typeparams/genericfeatures"
    30  )
    31  
    32  func bytesAllocated() uint64 {
    33  	runtime.GC()
    34  	var stats runtime.MemStats
    35  	runtime.ReadMemStats(&stats)
    36  	return stats.Alloc
    37  }
    38  
    39  func TestStdlib(t *testing.T) {
    40  	if testing.Short() {
    41  		t.Skip("skipping in short mode; too slow (https://golang.org/issue/14113)")
    42  	}
    43  	testenv.NeedsTool(t, "go")
    44  
    45  	// Load, parse and type-check the program.
    46  	t0 := time.Now()
    47  	alloc0 := bytesAllocated()
    48  
    49  	cfg := &packages.Config{Mode: packages.LoadSyntax}
    50  	pkgs, err := packages.Load(cfg, "std", "cmd")
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	var nonGeneric int
    55  	for i := 0; i < len(pkgs); i++ {
    56  		pkg := pkgs[i]
    57  		inspect := inspector.New(pkg.Syntax)
    58  		features := genericfeatures.ForPackage(inspect, pkg.TypesInfo)
    59  		// Skip standard library packages that use generics. This won't be
    60  		// sufficient if any standard library packages start _importing_ packages
    61  		// that use generics.
    62  		if features != 0 {
    63  			t.Logf("skipping package %q which uses generics", pkg.PkgPath)
    64  			continue
    65  		}
    66  		pkgs[nonGeneric] = pkg
    67  		nonGeneric++
    68  	}
    69  	pkgs = pkgs[:nonGeneric]
    70  
    71  	t1 := time.Now()
    72  	alloc1 := bytesAllocated()
    73  
    74  	// Create SSA packages.
    75  	var mode ssa.BuilderMode
    76  	// Comment out these lines during benchmarking.  Approx SSA build costs are noted.
    77  	mode |= ssa.SanityCheckFunctions // + 2% space, + 4% time
    78  	mode |= ssa.GlobalDebug          // +30% space, +18% time
    79  	prog, _ := ssautil.Packages(pkgs, mode)
    80  
    81  	t2 := time.Now()
    82  
    83  	// Build SSA.
    84  	prog.Build()
    85  
    86  	t3 := time.Now()
    87  	alloc3 := bytesAllocated()
    88  
    89  	numPkgs := len(prog.AllPackages())
    90  	if want := 140; numPkgs < want {
    91  		t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
    92  	}
    93  
    94  	// Keep pkgs reachable until after we've measured memory usage.
    95  	if len(pkgs) == 0 {
    96  		panic("unreachable")
    97  	}
    98  
    99  	allFuncs := ssautil.AllFunctions(prog)
   100  
   101  	// Check that all non-synthetic functions have distinct names.
   102  	// Synthetic wrappers for exported methods should be distinct too,
   103  	// except for unexported ones (explained at (*Function).RelString).
   104  	byName := make(map[string]*ssa.Function)
   105  	for fn := range allFuncs {
   106  		if fn.Synthetic == "" || ast.IsExported(fn.Name()) {
   107  			str := fn.String()
   108  			prev := byName[str]
   109  			byName[str] = fn
   110  			if prev != nil {
   111  				t.Errorf("%s: duplicate function named %s",
   112  					prog.Fset.Position(fn.Pos()), str)
   113  				t.Errorf("%s:   (previously defined here)",
   114  					prog.Fset.Position(prev.Pos()))
   115  			}
   116  		}
   117  	}
   118  
   119  	// Dump some statistics.
   120  	var numInstrs int
   121  	for fn := range allFuncs {
   122  		for _, b := range fn.Blocks {
   123  			numInstrs += len(b.Instrs)
   124  		}
   125  	}
   126  
   127  	// determine line count
   128  	var lineCount int
   129  	prog.Fset.Iterate(func(f *token.File) bool {
   130  		lineCount += f.LineCount()
   131  		return true
   132  	})
   133  
   134  	// NB: when benchmarking, don't forget to clear the debug +
   135  	// sanity builder flags for better performance.
   136  
   137  	t.Log("GOMAXPROCS:           ", runtime.GOMAXPROCS(0))
   138  	t.Log("#Source lines:        ", lineCount)
   139  	t.Log("Load/parse/typecheck: ", t1.Sub(t0))
   140  	t.Log("SSA create:           ", t2.Sub(t1))
   141  	t.Log("SSA build:            ", t3.Sub(t2))
   142  
   143  	// SSA stats:
   144  	t.Log("#Packages:            ", numPkgs)
   145  	t.Log("#Functions:           ", len(allFuncs))
   146  	t.Log("#Instructions:        ", numInstrs)
   147  	t.Log("#MB AST+types:        ", int64(alloc1-alloc0)/1e6)
   148  	t.Log("#MB SSA:              ", int64(alloc3-alloc1)/1e6)
   149  }