cmd/compilebench: use go command in goroot, not environment

The "go" command is a random "go" in the environment, not
necessarily the one under test.  Use the go command in the
goroot we're testing.

This CL removes the need to add $GOROOT/bin to your path
before running compilebench.

Change-Id: Ieb7f441f8287105e13446006e73b760d80e51e03
Reviewed-on: https://go-review.googlesource.com/42932
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Keith Randall 2017-05-08 15:47:55 -07:00 committed by Keith Randall
parent 11b386927a
commit a6971f4c11
1 changed files with 15 additions and 9 deletions

View File

@ -76,20 +76,20 @@ import (
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
var (
goroot = runtime.GOROOT()
goroot string
compiler string
runRE *regexp.Regexp
is6g bool
)
var (
flagGoCmd = flag.String("go", "go", "path to \"go\" command")
flagAlloc = flag.Bool("alloc", false, "report allocations")
flagObj = flag.Bool("obj", false, "report object file stats")
flagCompiler = flag.String("compile", "", "use `exe` as the cmd/compile binary")
@ -139,14 +139,20 @@ func main() {
usage()
}
s, err := exec.Command(*flagGoCmd, "env", "GOROOT").CombinedOutput()
if err != nil {
log.Fatalf("%s env GOROOT: %v", *flagGoCmd, err)
}
goroot = strings.TrimSpace(string(s))
compiler = *flagCompiler
if compiler == "" {
out, err := exec.Command("go", "tool", "-n", "compile").CombinedOutput()
out, err := exec.Command(*flagGoCmd, "tool", "-n", "compile").CombinedOutput()
if err != nil {
out, err = exec.Command("go", "tool", "-n", "6g").CombinedOutput()
out, err = exec.Command(*flagGoCmd, "tool", "-n", "6g").CombinedOutput()
is6g = true
if err != nil {
out, err = exec.Command("go", "tool", "-n", "compile").CombinedOutput()
out, err = exec.Command(*flagGoCmd, "tool", "-n", "compile").CombinedOutput()
log.Fatalf("go tool -n compiler: %v\n%s", err, out)
}
}
@ -193,14 +199,14 @@ func runStdCmd() {
args = append(args, "-gcflags", *flagCompilerFlags)
}
args = append(args, "std", "cmd")
cmd := exec.Command("go", args...)
cmd.Dir = filepath.Join(runtime.GOROOT(), "src")
cmd := exec.Command(*flagGoCmd, args...)
cmd.Dir = filepath.Join(goroot, "src")
runCmd("BenchmarkStdCmd", cmd)
}
// path is either a path to a file ("$GOROOT/test/helloworld.go") or a package path ("cmd/go").
func runSize(name, path string) {
cmd := exec.Command("go", "build", "-o", "_compilebenchout_", path)
cmd := exec.Command(*flagGoCmd, "build", "-o", "_compilebenchout_", path)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
@ -240,7 +246,7 @@ func runBuild(name, dir string, count int) {
runSize("BenchmarkCmdGoSize", "cmd/go")
return
case "BenchmarkHelloSize":
runSize("BenchmarkHelloSize", filepath.Join(runtime.GOROOT(), "test/helloworld.go"))
runSize("BenchmarkHelloSize", filepath.Join(goroot, "test/helloworld.go"))
return
}