Skip to content

Commit a192507

Browse files
committed
cmd/go: add benchmark that execs 'go env GOARCH'
'go env' is used for many quick operations, such as in go/packages to query GOARCH and GOMOD. It often is a bottleneck; for example, go/packages doesn't know whether or not to use Go modules until it has queried GOMOD. As such, this go command should be fast. Right now it's slower than it should be. This commit adds a simple benchmark with os/exec, since we're particularly interested in the cost of cmd/go's large init function. Updates #29382. Change-Id: Ifee6fb9997b9b89565fbfc2739a00c86117b1d37 Reviewed-on: https://go-review.googlesource.com/c/155961 Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent dd91269 commit a192507

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/cmd/go/init_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main_test
6+
7+
import (
8+
"internal/testenv"
9+
"os/exec"
10+
"testing"
11+
)
12+
13+
// BenchmarkExecGoEnv measures how long it takes for 'go env GOARCH' to run.
14+
// Since 'go' is executed, remember to run 'go install cmd/go' before running
15+
// the benchmark if any changes were done.
16+
func BenchmarkExecGoEnv(b *testing.B) {
17+
testenv.MustHaveExec(b)
18+
b.StopTimer()
19+
gotool, err := testenv.GoTool()
20+
if err != nil {
21+
b.Fatal(err)
22+
}
23+
for i := 0; i < b.N; i++ {
24+
cmd := exec.Command(gotool, "env", "GOARCH")
25+
26+
b.StartTimer()
27+
err := cmd.Run()
28+
b.StopTimer()
29+
30+
if err != nil {
31+
b.Fatal(err)
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)