]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/inline/inlheur/dumpscores_test.go
cmd/compile/internal/inline: rework call scoring for non-inlinable funcs
[gostls13.git] / src / cmd / compile / internal / inline / inlheur / dumpscores_test.go
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
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                 demoted   int
23                 unchanged int
24         }{
25                 {
26                         name:      "dumpscores",
27                         promoted:  1,
28                         demoted:   1,
29                         unchanged: 5,
30                 },
31         }
32
33         for _, scen := range scenarios {
34                 dumpfile, err := gatherInlCallSitesScoresForFile(t, scen.name, td)
35                 if err != nil {
36                         t.Fatalf("dumping callsite scores for %q: error %v", scen.name, err)
37                 }
38                 var lines []string
39                 if content, err := os.ReadFile(dumpfile); err != nil {
40                         t.Fatalf("reading dump %q: error %v", dumpfile, err)
41                 } else {
42                         lines = strings.Split(string(content), "\n")
43                 }
44                 prom, dem, unch := 0, 0, 0
45                 for _, line := range lines {
46                         switch {
47                         case strings.TrimSpace(line) == "":
48                         case strings.HasPrefix(line, "#"):
49                         case strings.Contains(line, "PROMOTED"):
50                                 prom++
51                         case strings.Contains(line, "DEMOTED"):
52                                 dem++
53                         default:
54                                 unch++
55                         }
56                 }
57                 showout := false
58                 if prom != scen.promoted {
59                         t.Errorf("testcase %q, got %d promoted want %d promoted",
60                                 scen.name, prom, scen.promoted)
61                         showout = true
62                 }
63                 if dem != scen.demoted {
64                         t.Errorf("testcase %q, got %d demoted want %d demoted",
65                                 scen.name, dem, scen.demoted)
66                         showout = true
67                 }
68                 if unch != scen.unchanged {
69                         t.Errorf("testcase %q, got %d unchanged want %d unchanged",
70                                 scen.name, unch, scen.unchanged)
71                         showout = true
72                 }
73                 if showout {
74                         t.Logf(">> dump output: %s", strings.Join(lines, "\n"))
75                 }
76         }
77 }
78
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) {
84         t.Helper()
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()
91         if err != nil {
92                 return "", err
93         }
94         if err := os.WriteFile(dumpfile, out, 0666); err != nil {
95                 return "", err
96         }
97         return dumpfile, err
98 }