Source file
src/cmd/pprof/pprof_test.go
1
2
3
4
5 package main
6
7 import (
8 "fmt"
9 "internal/testenv"
10 "os"
11 "os/exec"
12 "path/filepath"
13 "runtime"
14 "strings"
15 "testing"
16 )
17
18 var tmp, pprofExe string
19
20 func TestMain(m *testing.M) {
21 if !testenv.HasGoBuild() {
22 return
23 }
24
25 var exitcode int
26 if err := buildPprof(); err == nil {
27 exitcode = m.Run()
28 } else {
29 fmt.Println(err)
30 exitcode = 1
31 }
32 os.RemoveAll(tmp)
33 os.Exit(exitcode)
34 }
35
36 func buildPprof() error {
37 var err error
38 tmp, err = os.MkdirTemp("", "TestPprof")
39 if err != nil {
40 return fmt.Errorf("TempDir failed: %v", err)
41 }
42
43 pprofExe = filepath.Join(tmp, "testpprof.exe")
44 gotool, err := testenv.GoTool()
45 if err != nil {
46 return err
47 }
48 out, err := exec.Command(gotool, "build", "-o", pprofExe, "cmd/pprof").CombinedOutput()
49 if err != nil {
50 os.RemoveAll(tmp)
51 return fmt.Errorf("go build -o %v cmd/pprof: %v\n%s", pprofExe, err, string(out))
52 }
53
54 return nil
55 }
56
57
58 func mustHaveCPUProfiling(t *testing.T) {
59 switch runtime.GOOS {
60 case "plan9":
61 t.Skipf("skipping on %s, unimplemented", runtime.GOOS)
62 case "aix":
63 t.Skipf("skipping on %s, issue 45170", runtime.GOOS)
64 case "ios", "dragonfly", "netbsd", "illumos", "solaris":
65 t.Skipf("skipping on %s, issue 13841", runtime.GOOS)
66 case "openbsd":
67 if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
68 t.Skipf("skipping on %s/%s, issue 13841", runtime.GOOS, runtime.GOARCH)
69 }
70 }
71 }
72
73 func mustHaveDisasm(t *testing.T) {
74 switch runtime.GOARCH {
75 case "mips", "mipsle", "mips64", "mips64le":
76 t.Skipf("skipping on %s, issue 12559", runtime.GOARCH)
77 case "riscv64":
78 t.Skipf("skipping on %s, issue 36738", runtime.GOARCH)
79 case "s390x":
80 t.Skipf("skipping on %s, issue 15255", runtime.GOARCH)
81 }
82
83
84 if runtime.GOOS == "windows" {
85 t.Skipf("skipping on %s, issue 46639", runtime.GOOS)
86 }
87 if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
88 t.Skipf("skipping on %s/%s, issue 46639", runtime.GOOS, runtime.GOARCH)
89 }
90 }
91
92
93
94
95 func TestDisasm(t *testing.T) {
96 mustHaveCPUProfiling(t)
97 mustHaveDisasm(t)
98 testenv.MustHaveGoBuild(t)
99
100 tmpdir := t.TempDir()
101 cpuExe := filepath.Join(tmpdir, "cpu.exe")
102 cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", cpuExe, "cpu.go")
103 cmd.Dir = "testdata/"
104 out, err := cmd.CombinedOutput()
105 if err != nil {
106 t.Fatalf("build failed: %v\n%s", err, out)
107 }
108
109 profile := filepath.Join(tmpdir, "cpu.pprof")
110 cmd = exec.Command(cpuExe, "-output", profile)
111 out, err = cmd.CombinedOutput()
112 if err != nil {
113 t.Fatalf("cpu failed: %v\n%s", err, out)
114 }
115
116 cmd = exec.Command(pprofExe, "-disasm", "main.main", cpuExe, profile)
117 out, err = cmd.CombinedOutput()
118 if err != nil {
119 t.Fatalf("pprof failed: %v\n%s", err, out)
120 }
121
122 sout := string(out)
123 want := "ROUTINE ======================== main.main"
124 if !strings.Contains(sout, want) {
125 t.Errorf("pprof disasm got %s want contains %q", sout, want)
126 }
127 }
128
View as plain text