Debian Codegraph @

go/callgraph/callgraph_test.go

raw symbols SCIP
1 // Copyright 2023 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 package callgraph_test
5
6 import (
7 "sync"
8 "testing"
9
10 "golang.org/x/tools/go/callgraph"
11 "golang.org/x/tools/go/callgraph/cha"
12 "golang.org/x/tools/go/callgraph/rta"
13 "golang.org/x/tools/go/callgraph/static"
14 "golang.org/x/tools/go/callgraph/vta"
15 "golang.org/x/tools/go/ssa"
16 "golang.org/x/tools/go/ssa/ssautil"
17 "golang.org/x/tools/internal/testfiles"
18 "golang.org/x/tools/txtar"
19 )
20
21 // Benchmarks comparing different callgraph algorithms implemented in
22 // x/tools/go/callgraph. Comparison is on both speed, memory and precision.
23 // Fewer edges and fewer reachable nodes implies a more precise result.
24 // Comparison is done on a hello world http server using net/http.
25 //
26 // Current results were on an i7 macbook on go version devel go1.20-2730.
27 // Number of nodes, edges, and reachable function are expected to vary between
28 // go versions. Timing results are expected to vary between machines.
29 // BenchmarkStatic-12 53 ms/op 6 MB/op 12113 nodes 37355 edges 1522 reachable
30 // BenchmarkCHA-12 86 ms/op 16 MB/op 12113 nodes 131717 edges 7640 reachable
31 // BenchmarkRTA-12 110 ms/op 12 MB/op 6566 nodes 42291 edges 5099 reachable
32 // BenchmarkPTA-12 1427 ms/op 600 MB/op 8714 nodes 28244 edges 4184 reachable
33 // BenchmarkVTA-12 600 ms/op 78 MB/op 12114 nodes 44861 edges 4919 reachable
34 // BenchmarkVTA2-12 793 ms/op 104 MB/op 5450 nodes 22208 edges 4042 reachable
35 // BenchmarkVTA3-12 977 ms/op 124 MB/op 4621 nodes 19331 edges 3700 reachable
36 // BenchmarkVTAAlt-12 372 ms/op 57 MB/op 7763 nodes 29912 edges 4258 reachable
37 // BenchmarkVTAAlt2-12 570 ms/op 78 MB/op 4838 nodes 20169 edges 3737 reachable
38 //
39 // Note:
40 // * Static is unsound and may miss real edges.
41 // * RTA starts from a main function and only includes reachable functions.
42 // * CHA starts from all functions.
43 // * VTA, VTA2, and VTA3 are starting from all functions and the CHA callgraph.
44 // VTA2 and VTA3 are the result of re-applying VTA to the functions reachable
45 // from main() via the callgraph of the previous stage.
46 // * VTAAlt, and VTAAlt2 start from the functions reachable from main via the
47 // CHA callgraph.
48 // * All algorithms are unsound w.r.t. reflection.
49
50 const httpEx = `
51 -- go.mod --
52 module x.io
53
54 -- main.go --
55 package main
56
57 import (
58 "fmt"
59 "net/http"
60 )
61
62 func hello(w http.ResponseWriter, req *http.Request) {
63 fmt.Fprintf(w, "hello world\n")
64 }
65
66 func main() {
67 http.HandleFunc("/hello", hello)
68 http.ListenAndServe(":8090", nil)
69 }
70 `
71
72 var (
73 once sync.Once
74 main *ssa.Function
75 )
76
77 func example(t testing.TB) (*ssa.Program, *ssa.Function) {
78 once.Do(func() {
79 pkgs := testfiles.LoadPackages(t, txtar.Parse([]byte(httpEx)), ".")
80 prog, ssapkgs := ssautil.Packages(pkgs, ssa.InstantiateGenerics)
81 prog.Build()
82 main = ssapkgs[0].Members["main"].(*ssa.Function)
83 })
84 return main.Prog, main
85 }
86
87 var stats bool = false // print stats?
88
89 func logStats(b *testing.B, cnd bool, name string, cg *callgraph.Graph, main *ssa.Function) {
90 if cnd && stats {
91 e := 0
92 for _, n := range cg.Nodes {
93 e += len(n.Out)
94 }
95 r := len(reaches(main, cg, false))
96 b.Logf("%s:\t%d nodes\t%d edges\t%d reachable", name, len(cg.Nodes), e, r)
97 }
98 }
99
100 func BenchmarkStatic(b *testing.B) {
101
102 prog, main := example(b)
103
104 for i := 0; b.Loop(); i++ {
105 cg := static.CallGraph(prog)
106 logStats(b, i == 0, "static", cg, main)
107 }
108 }
109
110 func BenchmarkCHA(b *testing.B) {
111
112 prog, main := example(b)
113
114 for i := 0; b.Loop(); i++ {
115 cg := cha.CallGraph(prog)
116 logStats(b, i == 0, "cha", cg, main)
117 }
118 }
119
120 func BenchmarkRTA(b *testing.B) {
121
122 _, main := example(b)
123
124 for i := 0; b.Loop(); i++ {
125 res := rta.Analyze([]*ssa.Function{main}, true)
126 cg := res.CallGraph
127 logStats(b, i == 0, "rta", cg, main)
128 }
129 }
130
131 func BenchmarkVTA(b *testing.B) {
132
133 prog, main := example(b)
134
135 for i := 0; b.Loop(); i++ {
136 cg := vta.CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))
137 logStats(b, i == 0, "vta", cg, main)
138 }
139 }
140
141 func BenchmarkVTA2(b *testing.B) {
142
143 prog, main := example(b)
144
145 for i := 0; b.Loop(); i++ {
146 vta1 := vta.CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))
147 cg := vta.CallGraph(reaches(main, vta1, true), vta1)
148 logStats(b, i == 0, "vta2", cg, main)
149 }
150 }
151
152 func BenchmarkVTA3(b *testing.B) {
153
154 prog, main := example(b)
155
156 for i := 0; b.Loop(); i++ {
157 vta1 := vta.CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))
158 vta2 := vta.CallGraph(reaches(main, vta1, true), vta1)
159 cg := vta.CallGraph(reaches(main, vta2, true), vta2)
160 logStats(b, i == 0, "vta3", cg, main)
161 }
162 }
163
164 func BenchmarkVTAAlt(b *testing.B) {
165
166 prog, main := example(b)
167
168 for i := 0; b.Loop(); i++ {
169 cha := cha.CallGraph(prog)
170 cg := vta.CallGraph(reaches(main, cha, true), cha) // start from only functions reachable by CHA.
171 logStats(b, i == 0, "vta-alt", cg, main)
172 }
173 }
174
175 func BenchmarkVTAAlt2(b *testing.B) {
176
177 prog, main := example(b)
178
179 for i := 0; b.Loop(); i++ {
180 cha := cha.CallGraph(prog)
181 vta1 := vta.CallGraph(reaches(main, cha, true), cha)
182 cg := vta.CallGraph(reaches(main, vta1, true), vta1)
183 logStats(b, i == 0, "vta-alt2", cg, main)
184 }
185 }
186
187 // reaches computes the transitive closure of functions forward reachable
188 // via calls in cg starting from `sources`. If refs is true, include
189 // functions referred to in an instruction.
190 func reaches(source *ssa.Function, cg *callgraph.Graph, refs bool) map[*ssa.Function]bool {
191 seen := make(map[*ssa.Function]bool)
192 var visit func(f *ssa.Function)
193 visit = func(f *ssa.Function) {
194 if seen[f] {
195 return
196 }
197 seen[f] = true
198
199 if n := cg.Nodes[f]; n != nil {
200 for _, e := range n.Out {
201 if e.Site != nil {
202 visit(e.Callee.Func)
203 }
204 }
205 }
206
207 if refs {
208 var buf [10]*ssa.Value // avoid alloc in common case
209 for _, b := range f.Blocks {
210 for _, instr := range b.Instrs {
211 for _, op := range instr.Operands(buf[:0]) {
212 if fn, ok := (*op).(*ssa.Function); ok {
213 visit(fn)
214 }
215 }
216 }
217 }
218 }
219 }
220 visit(source)
221 return seen
222 }