cmd/bench: add flag overrides for all environment variables

This change adds a flag for the experiment and baseline GOROOTs, as well
as a flag for the branch. This both adds documentation as to where
cmd/bench gets these values from, and allows users running this command
manually to override them without modifying their environment.

Change-Id: Iba9e3bc70687989fe6fc40e3a810b21aa5091dfe
Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/385735
Reviewed-by: Michael Pratt <mpratt@google.com>
Trust: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/cmd/bench/main.go b/cmd/bench/main.go
index 82460b5..0b8e216 100644
--- a/cmd/bench/main.go
+++ b/cmd/bench/main.go
@@ -21,7 +21,12 @@
 	"golang.org/x/benchmarks/sweet/common"
 )
 
-var wait = flag.Bool("wait", true, "wait for system idle before starting benchmarking")
+var (
+	wait             = flag.Bool("wait", true, "wait for system idle before starting benchmarking")
+	gorootExperiment = flag.String("goroot", "", "GOROOT to test (default $GOROOT or 'go env GOROOT')")
+	gorootBaseline   = flag.String("goroot-baseline", "", "baseline GOROOT to test against (optional) (default $BENCH_BASELINE_GOROOT)")
+	goBranch         = flag.String("go-branch", "", "git branch of the commits we're testing against (default $BENCH_BRANCH or unknown)")
+)
 
 func determineGOROOT() (string, error) {
 	g, ok := os.LookupEnv("GOROOT")
@@ -90,24 +95,34 @@
 	}
 
 	// Find the toolchain under test.
-	gorootExperiment, err := determineGOROOT()
-	if err != nil {
-		log.Fatalf("Unable to determine GOROOT: %v", err)
+	gorootExperiment := *gorootExperiment
+	if gorootExperiment == "" {
+		var err error
+		gorootExperiment, err = determineGOROOT()
+		if err != nil {
+			log.Fatalf("Unable to determine GOROOT: %v", err)
+		}
 	}
 	toolchains := []*toolchain{toolchainFromGOROOT("experiment", gorootExperiment)}
 
 	// Find the baseline toolchain, if applicable.
-	gorootBaseline := os.Getenv("BENCH_BASELINE_GOROOT")
+	gorootBaseline := *gorootBaseline
+	if gorootBaseline == "" {
+		gorootBaseline = os.Getenv("BENCH_BASELINE_GOROOT")
+	}
 	if gorootBaseline != "" {
 		toolchains = append(toolchains, toolchainFromGOROOT("baseline", gorootBaseline))
 	}
 
 	// Try to identify the Go branch. If we can't, just make sure we say so explicitly.
-	branch := os.Getenv("BENCH_BRANCH")
-	if branch == "" {
-		branch = "unknown"
+	goBranch := *goBranch
+	if goBranch == "" {
+		goBranch = os.Getenv("BENCH_BRANCH")
 	}
-	fmt.Printf("branch: %s\n", branch)
+	if goBranch == "" {
+		goBranch = "unknown"
+	}
+	fmt.Printf("branch: %s\n", goBranch)
 
 	// Run benchmarks against the toolchains.
 	if err := run(toolchains); err != nil {