.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.1-0.20210319172145-bda8f5cee399 / go / ssa / stdlib_test.go
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/build"
20         "go/token"
21         "runtime"
22         "testing"
23         "time"
24
25         "golang.org/x/tools/go/buildutil"
26         "golang.org/x/tools/go/loader"
27         "golang.org/x/tools/go/ssa"
28         "golang.org/x/tools/go/ssa/ssautil"
29         "golang.org/x/tools/internal/testenv"
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         // Load, parse and type-check the program.
50         ctxt := build.Default // copy
51         ctxt.GOPATH = ""      // disable GOPATH
52         conf := loader.Config{Build: &ctxt}
53         for _, path := range buildutil.AllPackages(conf.Build) {
54                 conf.ImportWithTests(path)
55         }
56
57         iprog, err := conf.Load()
58         if err != nil {
59                 t.Fatalf("Load failed: %v", err)
60         }
61
62         t1 := time.Now()
63         alloc1 := bytesAllocated()
64
65         // Create SSA packages.
66         var mode ssa.BuilderMode
67         // Comment out these lines during benchmarking.  Approx SSA build costs are noted.
68         mode |= ssa.SanityCheckFunctions // + 2% space, + 4% time
69         mode |= ssa.GlobalDebug          // +30% space, +18% time
70         prog := ssautil.CreateProgram(iprog, mode)
71
72         t2 := time.Now()
73
74         // Build SSA.
75         prog.Build()
76
77         t3 := time.Now()
78         alloc3 := bytesAllocated()
79
80         numPkgs := len(prog.AllPackages())
81         if want := 140; numPkgs < want {
82                 t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
83         }
84
85         // Keep iprog reachable until after we've measured memory usage.
86         if len(iprog.AllPackages) == 0 {
87                 panic("unreachable")
88         }
89
90         allFuncs := ssautil.AllFunctions(prog)
91
92         // Check that all non-synthetic functions have distinct names.
93         // Synthetic wrappers for exported methods should be distinct too,
94         // except for unexported ones (explained at (*Function).RelString).
95         byName := make(map[string]*ssa.Function)
96         for fn := range allFuncs {
97                 if fn.Synthetic == "" || ast.IsExported(fn.Name()) {
98                         str := fn.String()
99                         prev := byName[str]
100                         byName[str] = fn
101                         if prev != nil {
102                                 t.Errorf("%s: duplicate function named %s",
103                                         prog.Fset.Position(fn.Pos()), str)
104                                 t.Errorf("%s:   (previously defined here)",
105                                         prog.Fset.Position(prev.Pos()))
106                         }
107                 }
108         }
109
110         // Dump some statistics.
111         var numInstrs int
112         for fn := range allFuncs {
113                 for _, b := range fn.Blocks {
114                         numInstrs += len(b.Instrs)
115                 }
116         }
117
118         // determine line count
119         var lineCount int
120         prog.Fset.Iterate(func(f *token.File) bool {
121                 lineCount += f.LineCount()
122                 return true
123         })
124
125         // NB: when benchmarking, don't forget to clear the debug +
126         // sanity builder flags for better performance.
127
128         t.Log("GOMAXPROCS:           ", runtime.GOMAXPROCS(0))
129         t.Log("#Source lines:        ", lineCount)
130         t.Log("Load/parse/typecheck: ", t1.Sub(t0))
131         t.Log("SSA create:           ", t2.Sub(t1))
132         t.Log("SSA build:            ", t3.Sub(t2))
133
134         // SSA stats:
135         t.Log("#Packages:            ", numPkgs)
136         t.Log("#Functions:           ", len(allFuncs))
137         t.Log("#Instructions:        ", numInstrs)
138         t.Log("#MB AST+types:        ", int64(alloc1-alloc0)/1e6)
139         t.Log("#MB SSA:              ", int64(alloc3-alloc1)/1e6)
140 }