avoid rebuilding garble in the main benchmark

Similar to what testscript does, we can reuse the test binary by telling
TestMain to run the main function rather than the Go tests.

This saves a few hundred milliseconds out of each benchmark run.
pull/713/head
Daniel Martí 3 years ago committed by lu4p
parent 1f39d0af72
commit a186419d3d

@ -11,7 +11,6 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@ -53,19 +52,12 @@ func BenchmarkBuild(b *testing.B) {
} }
tdir := b.TempDir() tdir := b.TempDir()
garbleBin := filepath.Join(tdir, "garble")
if runtime.GOOS == "windows" {
garbleBin += ".exe"
}
err := exec.Command("go", "build", "-o="+garbleBin).Run()
qt.Assert(b, err, qt.IsNil)
// We collect extra metrics. // We collect extra metrics.
var memoryAllocs, cachedTime, systemTime int64 var memoryAllocs, cachedTime, systemTime int64
outputBin := filepath.Join(tdir, "output") outputBin := filepath.Join(tdir, "output")
sourceDir := filepath.Join(tdir, "src") sourceDir := filepath.Join(tdir, "src")
err = os.Mkdir(sourceDir, 0o777) err := os.Mkdir(sourceDir, 0o777)
qt.Assert(b, err, qt.IsNil) qt.Assert(b, err, qt.IsNil)
writeSourceFile := func(name string, content []byte) { writeSourceFile := func(name string, content []byte) {
@ -85,6 +77,7 @@ func BenchmarkBuild(b *testing.B) {
gocache, err := os.MkdirTemp(tdir, "gocache-*") gocache, err := os.MkdirTemp(tdir, "gocache-*")
qt.Assert(b, err, qt.IsNil) qt.Assert(b, err, qt.IsNil)
env := append(os.Environ(), env := append(os.Environ(),
"RUN_GARBLE_MAIN=true",
"GOCACHE="+gocache, "GOCACHE="+gocache,
"GARBLE_WRITE_ALLOCS=true", "GARBLE_WRITE_ALLOCS=true",
) )
@ -97,7 +90,7 @@ func BenchmarkBuild(b *testing.B) {
writeSourceFile("rebuild.go", []byte(fmt.Sprintf("package main\nvar v%d int", i))) writeSourceFile("rebuild.go", []byte(fmt.Sprintf("package main\nvar v%d int", i)))
} }
cmd := exec.Command(garbleBin, args...) cmd := exec.Command(os.Args[0], args...)
cmd.Env = env cmd.Env = env
cmd.Dir = sourceDir cmd.Dir = sourceDir

@ -31,6 +31,9 @@ import (
var proxyURL string var proxyURL string
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
if os.Getenv("RUN_GARBLE_MAIN") == "true" {
os.Exit(main1())
}
os.Exit(testscript.RunMain(garbleMain{m}, map[string]func() int{ os.Exit(testscript.RunMain(garbleMain{m}, map[string]func() int{
"garble": main1, "garble": main1,
})) }))

Loading…
Cancel
Save