]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/dist: skip compiling 100 packages without benchmarks in race mode
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 14 Sep 2016 16:34:27 +0000 (16:34 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 14 Sep 2016 17:04:47 +0000 (17:04 +0000)
The go_test_bench:* tests run:

    go test -short -race -run=^$ -benchtime=.1s -cpu=4 $PKG

... on each discovered package with any tests. (The same set used for
the "go_test:*" tests)

That set was 168 packages:

$ go tool dist test -list | grep go_test: | wc -l
168

But only 76 of those have a "func Benchmark", and running each
"go_test_bench:" test and compiling it in race mode, just to do
nothing took 1-2 seconds each.

So stop doing that and filter out the useless packages earlier. Now:

$ go tool dist test -list -race | grep go_test_bench:  | wc -l
76

Should save 90-180 seconds. (or maybe 45 seconds for trybots, since
they're sharded)

Updates #17104

Change-Id: I08ccb072a0dc0454ea425540ee8e74b59f83b773
Reviewed-on: https://go-review.googlesource.com/29153
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/dist/test.go

index 27341f2069376358c66e41003ae135b663b7cc0f..accc54eb1a8b2db4962d867b411ace0850146b46 100644 (file)
@@ -9,6 +9,7 @@ import (
        "errors"
        "flag"
        "fmt"
+       "io/ioutil"
        "log"
        "os"
        "os/exec"
@@ -361,7 +362,9 @@ func (t *tester) registerTests() {
                }
                if t.race {
                        for _, pkg := range pkgs {
-                               t.registerRaceBenchTest(pkg)
+                               if t.packageHasBenchmarks(pkg) {
+                                       t.registerRaceBenchTest(pkg)
+                               }
                        }
                }
        }
@@ -1086,3 +1089,38 @@ var cgoPackages = []string{
        "net",
        "os/user",
 }
+
+var funcBenchmark = []byte("\nfunc Benchmark")
+
+// packageHasBenchmarks reports whether pkg has benchmarks.
+// On any error, it conservatively returns true.
+//
+// This exists just to eliminate work on the builders, since compiling
+// a test in race mode just to discover it has no benchmarks costs a
+// second or two per package, and this function returns false for
+// about 100 packages.
+func (t *tester) packageHasBenchmarks(pkg string) bool {
+       pkgDir := filepath.Join(t.goroot, "src", pkg)
+       d, err := os.Open(pkgDir)
+       if err != nil {
+               return true // conservatively
+       }
+       defer d.Close()
+       names, err := d.Readdirnames(-1)
+       if err != nil {
+               return true // conservatively
+       }
+       for _, name := range names {
+               if !strings.HasSuffix(name, "_test.go") {
+                       continue
+               }
+               slurp, err := ioutil.ReadFile(filepath.Join(pkgDir, name))
+               if err != nil {
+                       return true // conservatively
+               }
+               if bytes.Contains(slurp, funcBenchmark) {
+                       return true
+               }
+       }
+       return false
+}