github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/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  	"golang.org/x/tools/go/packages"
    25  	"golang.org/x/tools/go/ssa"
    26  	"golang.org/x/tools/go/ssa/ssautil"
    27  	"golang.org/x/tools/internal/testenv"
    28  )
    29  
    30  func bytesAllocated() uint64 {
    31  	runtime.GC()
    32  	var stats runtime.MemStats
    33  	runtime.ReadMemStats(&stats)
    34  	return stats.Alloc
    35  }
    36  
    37  func TestStdlib(t *testing.T) {
    38  	if testing.Short() {
    39  		t.Skip("skipping in short mode; too slow (https://golang.org/issue/14113)")
    40  	}
    41  	testenv.NeedsTool(t, "go")
    42  
    43  	// Load, parse and type-check the program.
    44  	t0 := time.Now()
    45  	alloc0 := bytesAllocated()
    46  
    47  	cfg := &packages.Config{Mode: packages.LoadSyntax}
    48  	pkgs, err := packages.Load(cfg, "std", "cmd")
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	t1 := time.Now()
    54  	alloc1 := bytesAllocated()
    55  
    56  	// Create SSA packages.
    57  	var mode ssa.BuilderMode
    58  	// Comment out these lines during benchmarking.  Approx SSA build costs are noted.
    59  	mode |= ssa.SanityCheckFunctions // + 2% space, + 4% time
    60  	mode |= ssa.GlobalDebug          // +30% space, +18% time
    61  	mode |= ssa.InstantiateGenerics  // + 0% space, + 2% time (unlikely to reproduce outside of stdlib)
    62  	prog, _ := ssautil.Packages(pkgs, mode)
    63  
    64  	t2 := time.Now()
    65  
    66  	// Build SSA.
    67  	prog.Build()
    68  
    69  	t3 := time.Now()
    70  	alloc3 := bytesAllocated()
    71  
    72  	numPkgs := len(prog.AllPackages())
    73  	if want := 140; numPkgs < want {
    74  		t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
    75  	}
    76  
    77  	// Keep pkgs reachable until after we've measured memory usage.
    78  	if len(pkgs) == 0 {
    79  		panic("unreachable")
    80  	}
    81  
    82  	allFuncs := ssautil.AllFunctions(prog)
    83  
    84  	// Check that all non-synthetic functions have distinct names.
    85  	// Synthetic wrappers for exported methods should be distinct too,
    86  	// except for unexported ones (explained at (*Function).RelString).
    87  	byName := make(map[string]*ssa.Function)
    88  	for fn := range allFuncs {
    89  		if fn.Synthetic == "" || ast.IsExported(fn.Name()) {
    90  			str := fn.String()
    91  			prev := byName[str]
    92  			byName[str] = fn
    93  			if prev != nil {
    94  				t.Errorf("%s: duplicate function named %s",
    95  					prog.Fset.Position(fn.Pos()), str)
    96  				t.Errorf("%s:   (previously defined here)",
    97  					prog.Fset.Position(prev.Pos()))
    98  			}
    99  		}
   100  	}
   101  
   102  	// Dump some statistics.
   103  	var numInstrs int
   104  	for fn := range allFuncs {
   105  		for _, b := range fn.Blocks {
   106  			numInstrs += len(b.Instrs)
   107  		}
   108  	}
   109  
   110  	// determine line count
   111  	var lineCount int
   112  	prog.Fset.Iterate(func(f *token.File) bool {
   113  		lineCount += f.LineCount()
   114  		return true
   115  	})
   116  
   117  	// NB: when benchmarking, don't forget to clear the debug +
   118  	// sanity builder flags for better performance.
   119  
   120  	t.Log("GOMAXPROCS:           ", runtime.GOMAXPROCS(0))
   121  	t.Log("#Source lines:        ", lineCount)
   122  	t.Log("Load/parse/typecheck: ", t1.Sub(t0))
   123  	t.Log("SSA create:           ", t2.Sub(t1))
   124  	t.Log("SSA build:            ", t3.Sub(t2))
   125  
   126  	// SSA stats:
   127  	t.Log("#Packages:            ", numPkgs)
   128  	t.Log("#Functions:           ", len(allFuncs))
   129  	t.Log("#Instructions:        ", numInstrs)
   130  	t.Log("#MB AST+types:        ", int64(alloc1-alloc0)/1e6)
   131  	t.Log("#MB SSA:              ", int64(alloc3-alloc1)/1e6)
   132  }