mirror of https://github.com/golang/go.git
cmd/pprof: use the test binary as 'pprof' instead of rebuilding it
This not only reduces the latency of the test, but also respects build flags like '-race' and '-cover' passed to the 'go test' command. Change-Id: Icdf256420c4dce2da7a187513b7dd08393b76146 Reviewed-on: https://go-review.googlesource.com/c/go/+/450708 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com>
This commit is contained in:
parent
3e1519d05a
commit
a68f9113a2
|
|
@ -5,55 +5,48 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"internal/testenv"
|
"internal/testenv"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var tmp, pprofExe string // populated by buildPprof
|
// TestMain executes the test binary as the pprof command if
|
||||||
|
// GO_PPROFTEST_IS_PPROF is set, and runs the tests otherwise.
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
if !testenv.HasGoBuild() {
|
if os.Getenv("GO_PPROFTEST_IS_PPROF") != "" {
|
||||||
return
|
main()
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
var exitcode int
|
os.Setenv("GO_PPROFTEST_IS_PPROF", "1") // Set for subprocesses to inherit.
|
||||||
if err := buildPprof(); err == nil {
|
os.Exit(m.Run())
|
||||||
exitcode = m.Run()
|
|
||||||
} else {
|
|
||||||
fmt.Println(err)
|
|
||||||
exitcode = 1
|
|
||||||
}
|
|
||||||
os.RemoveAll(tmp)
|
|
||||||
os.Exit(exitcode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildPprof() error {
|
// pprofPath returns the path to the "pprof" binary to run.
|
||||||
var err error
|
func pprofPath(t testing.TB) string {
|
||||||
tmp, err = os.MkdirTemp("", "TestPprof")
|
t.Helper()
|
||||||
if err != nil {
|
testenv.MustHaveExec(t)
|
||||||
return fmt.Errorf("TempDir failed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pprofExe = filepath.Join(tmp, "testpprof.exe")
|
pprofPathOnce.Do(func() {
|
||||||
gotool, err := testenv.GoTool()
|
pprofExePath, pprofPathErr = os.Executable()
|
||||||
if err != nil {
|
})
|
||||||
return err
|
if pprofPathErr != nil {
|
||||||
|
t.Fatal(pprofPathErr)
|
||||||
}
|
}
|
||||||
out, err := exec.Command(gotool, "build", "-o", pprofExe, "cmd/pprof").CombinedOutput()
|
return pprofExePath
|
||||||
if err != nil {
|
|
||||||
os.RemoveAll(tmp)
|
|
||||||
return fmt.Errorf("go build -o %v cmd/pprof: %v\n%s", pprofExe, err, string(out))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
pprofPathOnce sync.Once
|
||||||
|
pprofExePath string
|
||||||
|
pprofPathErr error
|
||||||
|
)
|
||||||
|
|
||||||
// See also runtime/pprof.cpuProfilingBroken.
|
// See also runtime/pprof.cpuProfilingBroken.
|
||||||
func mustHaveCPUProfiling(t *testing.T) {
|
func mustHaveCPUProfiling(t *testing.T) {
|
||||||
switch runtime.GOOS {
|
switch runtime.GOOS {
|
||||||
|
|
@ -112,13 +105,13 @@ func TestDisasm(t *testing.T) {
|
||||||
t.Fatalf("cpu failed: %v\n%s", err, out)
|
t.Fatalf("cpu failed: %v\n%s", err, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd = exec.Command(pprofExe, "-disasm", "main.main", cpuExe, profile)
|
cmd = exec.Command(pprofPath(t), "-disasm", "main.main", cpuExe, profile)
|
||||||
out, err = cmd.CombinedOutput()
|
out, err = cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("pprof -disasm failed: %v\n%s", err, out)
|
t.Errorf("pprof -disasm failed: %v\n%s", err, out)
|
||||||
|
|
||||||
// Try to print out profile content for debugging.
|
// Try to print out profile content for debugging.
|
||||||
cmd = exec.Command(pprofExe, "-raw", cpuExe, profile)
|
cmd = exec.Command(pprofPath(t), "-raw", cpuExe, profile)
|
||||||
out, err = cmd.CombinedOutput()
|
out, err = cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf("pprof -raw failed: %v\n%s", err, out)
|
t.Logf("pprof -raw failed: %v\n%s", err, out)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue