in cmd/bent/configuration.go [152:274]
func (config *Configuration) compileOne(bench *Benchmark, cwd string, count int) string {
root := config.rootCopy
gocmd := config.goCommandCopy()
gopath := path.Join(cwd, "gopath")
if explicitAll != 1 { // clear cache unless "-a[=1]" which requests -a on compilation.
cmd := exec.Command(gocmd, "clean", "-cache")
cmd.Env = defaultEnv
if !bench.NotSandboxed {
cmd.Env = replaceEnv(cmd.Env, "GOOS", "linux")
}
if root != "" {
cmd.Env = replaceEnv(cmd.Env, "GOROOT", root)
}
cmd.Env = replaceEnvs(cmd.Env, bench.GcEnv)
cmd.Env = replaceEnvs(cmd.Env, config.GcEnv)
cmd.Dir = gopath // Only want the cache-cleaning effect, not the binary-deleting effect. It's okay to clean gopath.
s, _ := config.runBinary("", cmd, true)
if s != "" {
fmt.Println("Error running go clean -cache, ", s)
}
}
cmd := exec.Command(gocmd, "test", "-vet=off", "-c")
compileTo := path.Join(dirs.wd, dirs.testBinDir, config.benchName(bench))
cmd.Args = append(cmd.Args, "-o", compileTo)
cmd.Args = append(cmd.Args, bench.BuildFlags...)
// Do not normally need -a because cache was emptied first and std was -a installed with these flags.
// But for -a=1, do it anyway
if explicitAll == 1 {
cmd.Args = append(cmd.Args, "-a")
}
cmd.Args = append(cmd.Args, config.BuildFlags...)
if config.GcFlags != "" {
cmd.Args = append(cmd.Args, "-gcflags="+config.GcFlags)
}
cmd.Args = append(cmd.Args, bench.Repo)
cmd.Dir = bench.BuildDir // use module-mode
cmd.Env = defaultEnv
if !bench.NotSandboxed {
cmd.Env = replaceEnv(cmd.Env, "GOOS", "linux")
}
if root != "" {
cmd.Env = replaceEnv(cmd.Env, "GOROOT", root)
}
cmd.Env = replaceEnvs(cmd.Env, bench.GcEnv)
cmd.Env = replaceEnvs(cmd.Env, config.GcEnv)
if verbose > 0 {
fmt.Println(asCommandLine(cwd, cmd))
} else {
fmt.Print(".")
}
defer cleanup(gopath)
start := time.Now()
output, err := cmd.CombinedOutput()
realTime := time.Since(start)
if err != nil {
s := ""
switch e := err.(type) {
case *exec.ExitError:
s = fmt.Sprintf("There was an error running 'go test', output = %s", output)
default:
s = fmt.Sprintf("There was an error running 'go test', output = %s, error = %v", output, e)
}
fmt.Println(s + "DISABLING benchmark " + bench.Name)
bench.Disabled = true // if it won't compile, it won't run, either.
return s + "(" + bench.Name + ")\n"
}
soutput := string(output)
bs := BenchStat{
Name: bench.Name,
RealTime: realTime,
UserTime: cmd.ProcessState.UserTime(),
SysTime: cmd.ProcessState.SystemTime(),
}
config.buildStats = append(config.buildStats, bs)
// Report and record build stats to testbin
buf := new(bytes.Buffer)
configGoArch := getenv(config.GcEnv, "GOARCH")
if configGoArch != runtime.GOARCH && configGoArch != "" {
s := fmt.Sprintf("goarch: %s-%s\n", runtime.GOARCH, configGoArch)
if verbose > 0 {
fmt.Print(s)
}
buf.WriteString(s)
}
s := fmt.Sprintf("Benchmark%s 1 %d build-real-ns/op %d build-user-ns/op %d build-sys-ns/op\n",
strings.Title(bench.Name), bs.RealTime.Nanoseconds(), bs.UserTime.Nanoseconds(), bs.SysTime.Nanoseconds())
if verbose > 0 {
fmt.Print(s)
}
buf.WriteString(s)
f, err := os.OpenFile(config.buildBenchName(), os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
fmt.Printf("There was an error opening %s for append, error %v\n", config.buildBenchName(), err)
cleanup(gopath)
os.Exit(2)
}
f.Write(buf.Bytes())
f.Sync()
f.Close()
// Trim /usr/bin/time info from soutput, it's ugly
if verbose > 0 {
i := strings.LastIndex(soutput, "real")
if i >= 0 {
soutput = soutput[:i]
}
fmt.Print(soutput)
}
// Do this here before any cleanup.
if count == 0 {
config.runOtherBenchmarks(bench, cwd)
}
return ""
}