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.
15 func TestDumpCallSiteScoreDump(t *testing.T) {
17 testenv.MustHaveGoBuild(t)
19 scenarios := []struct {
33 for _, scen := range scenarios {
34 dumpfile, err := gatherInlCallSitesScoresForFile(t, scen.name, td)
36 t.Fatalf("dumping callsite scores for %q: error %v", scen.name, err)
39 if content, err := os.ReadFile(dumpfile); err != nil {
40 t.Fatalf("reading dump %q: error %v", dumpfile, err)
42 lines = strings.Split(string(content), "\n")
44 prom, dem, unch := 0, 0, 0
45 for _, line := range lines {
47 case strings.TrimSpace(line) == "":
48 case strings.HasPrefix(line, "#"):
49 case strings.Contains(line, "PROMOTED"):
51 case strings.Contains(line, "DEMOTED"):
58 if prom != scen.promoted {
59 t.Errorf("testcase %q, got %d promoted want %d promoted",
60 scen.name, prom, scen.promoted)
63 if dem != scen.demoted {
64 t.Errorf("testcase %q, got %d demoted want %d demoted",
65 scen.name, dem, scen.demoted)
68 if unch != scen.unchanged {
69 t.Errorf("testcase %q, got %d unchanged want %d unchanged",
70 scen.name, unch, scen.unchanged)
74 t.Logf(">> dump output: %s", strings.Join(lines, "\n"))
79 // gatherInlCallSitesScoresForFile builds the specified testcase 'testcase'
80 // from testdata/props passing the "-d=dumpinlcallsitescores=1"
81 // compiler option, to produce a dump, then returns the path of the
82 // newly created file.
83 func gatherInlCallSitesScoresForFile(t *testing.T, testcase string, td string) (string, error) {
85 gopath := "testdata/" + testcase + ".go"
86 outpath := filepath.Join(td, testcase+".a")
87 dumpfile := filepath.Join(td, testcase+".callsites.txt")
88 run := []string{testenv.GoToolPath(t), "build",
89 "-gcflags=-d=dumpinlcallsitescores=1", "-o", outpath, gopath}
90 out, err := testenv.Command(t, run[0], run[1:]...).CombinedOutput()
94 if err := os.WriteFile(dumpfile, out, 0666); err != nil {