1
2
3
4
5 package inlheur
6
7 import (
8 "internal/testenv"
9 "os"
10 "path/filepath"
11 "strings"
12 "testing"
13 )
14
15 func TestDumpCallSiteScoreDump(t *testing.T) {
16 td := t.TempDir()
17 testenv.MustHaveGoBuild(t)
18
19 scenarios := []struct {
20 name string
21 promoted int
22 indirectlyPromoted int
23 demoted int
24 unchanged int
25 }{
26 {
27 name: "dumpscores",
28 promoted: 1,
29 indirectlyPromoted: 1,
30 demoted: 1,
31 unchanged: 5,
32 },
33 }
34
35 for _, scen := range scenarios {
36 dumpfile, err := gatherInlCallSitesScoresForFile(t, scen.name, td)
37 if err != nil {
38 t.Fatalf("dumping callsite scores for %q: error %v", scen.name, err)
39 }
40 var lines []string
41 if content, err := os.ReadFile(dumpfile); err != nil {
42 t.Fatalf("reading dump %q: error %v", dumpfile, err)
43 } else {
44 lines = strings.Split(string(content), "\n")
45 }
46 prom, indprom, dem, unch := 0, 0, 0, 0
47 for _, line := range lines {
48 switch {
49 case strings.TrimSpace(line) == "":
50 case !strings.Contains(line, "|"):
51 case strings.HasPrefix(line, "#"):
52 case strings.Contains(line, "PROMOTED"):
53 prom++
54 case strings.Contains(line, "INDPROM"):
55 indprom++
56 case strings.Contains(line, "DEMOTED"):
57 dem++
58 default:
59 unch++
60 }
61 }
62 showout := false
63 if prom != scen.promoted {
64 t.Errorf("testcase %q, got %d promoted want %d promoted",
65 scen.name, prom, scen.promoted)
66 showout = true
67 }
68 if indprom != scen.indirectlyPromoted {
69 t.Errorf("testcase %q, got %d indirectly promoted want %d",
70 scen.name, indprom, scen.indirectlyPromoted)
71 showout = true
72 }
73 if dem != scen.demoted {
74 t.Errorf("testcase %q, got %d demoted want %d demoted",
75 scen.name, dem, scen.demoted)
76 showout = true
77 }
78 if unch != scen.unchanged {
79 t.Errorf("testcase %q, got %d unchanged want %d unchanged",
80 scen.name, unch, scen.unchanged)
81 showout = true
82 }
83 if showout {
84 t.Logf(">> dump output: %s", strings.Join(lines, "\n"))
85 }
86 }
87 }
88
89
90
91
92
93 func gatherInlCallSitesScoresForFile(t *testing.T, testcase string, td string) (string, error) {
94 t.Helper()
95 gopath := "testdata/" + testcase + ".go"
96 outpath := filepath.Join(td, testcase+".a")
97 dumpfile := filepath.Join(td, testcase+".callsites.txt")
98 run := []string{testenv.GoToolPath(t), "build",
99 "-gcflags=-d=dumpinlcallsitescores=1", "-o", outpath, gopath}
100 out, err := testenv.Command(t, run[0], run[1:]...).CombinedOutput()
101 t.Logf("run: %+v\n", run)
102 if err != nil {
103 return "", err
104 }
105 if err := os.WriteFile(dumpfile, out, 0666); err != nil {
106 return "", err
107 }
108 return dumpfile, err
109 }
110
View as plain text