Skip to content

Commit 7347907

Browse files
committed
go/types: turn TestBenchmark into a normal benchmark
TestBenchmark doesn't use the -bench flag, so that it can format custom output -- the number of checked lines per second. This is a barrier both to discoverability, and to piping benchmark output into analysis tools such as benchstat. Using testing.B.ReportMetric and a bit of manual timing, we can achieve similar results while conforming to normal benchmark output. Do this, and rename the test func to BenchmarkCheck (for symmetry with TestCheck). Change-Id: Ie8f2259c1ca9e6986f0137287acf8eb2843f96b8 Reviewed-on: https://go-review.googlesource.com/c/go/+/257958 Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 069aef4 commit 7347907

File tree

1 file changed

+45
-35
lines changed

1 file changed

+45
-35
lines changed

src/go/types/self_test.go

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@
55
package types_test
66

77
import (
8-
"flag"
9-
"fmt"
108
"go/ast"
119
"go/importer"
1210
"go/parser"
1311
"go/token"
12+
"path"
1413
"path/filepath"
1514
"testing"
1615
"time"
1716

1817
. "go/types"
1918
)
2019

21-
var benchmark = flag.Bool("b", false, "run benchmarks")
22-
2320
func TestSelf(t *testing.T) {
2421
fset := token.NewFileSet()
2522
files, err := pkgFiles(fset, ".")
@@ -39,57 +36,70 @@ func TestSelf(t *testing.T) {
3936
}
4037
}
4138

42-
func TestBenchmark(t *testing.T) {
43-
if !*benchmark {
44-
return
45-
}
46-
47-
// We're not using testing's benchmarking mechanism directly
48-
// because we want custom output.
49-
39+
func BenchmarkCheck(b *testing.B) {
5040
for _, p := range []string{
5141
"net/http",
5242
"go/parser",
5343
"go/constant",
5444
filepath.Join("go", "internal", "gcimporter"),
5545
} {
56-
path := filepath.Join("..", "..", p)
57-
runbench(t, path, false)
58-
runbench(t, path, true)
59-
fmt.Println()
46+
b.Run(path.Base(p), func(b *testing.B) {
47+
path := filepath.Join("..", "..", p)
48+
for _, ignoreFuncBodies := range []bool{false, true} {
49+
name := "funcbodies"
50+
if ignoreFuncBodies {
51+
name = "nofuncbodies"
52+
}
53+
b.Run(name, func(b *testing.B) {
54+
b.Run("info", func(b *testing.B) {
55+
runbench(b, path, ignoreFuncBodies, true)
56+
})
57+
b.Run("noinfo", func(b *testing.B) {
58+
runbench(b, path, ignoreFuncBodies, false)
59+
})
60+
})
61+
}
62+
})
6063
}
6164
}
6265

63-
func runbench(t *testing.T, path string, ignoreFuncBodies bool) {
66+
func runbench(b *testing.B, path string, ignoreFuncBodies, writeInfo bool) {
6467
fset := token.NewFileSet()
6568
files, err := pkgFiles(fset, path)
6669
if err != nil {
67-
t.Fatal(err)
70+
b.Fatal(err)
6871
}
69-
70-
b := testing.Benchmark(func(b *testing.B) {
71-
for i := 0; i < b.N; i++ {
72-
conf := Config{
73-
IgnoreFuncBodies: ignoreFuncBodies,
74-
Importer: importer.Default(),
75-
}
76-
if _, err := conf.Check(path, fset, files, nil); err != nil {
77-
t.Fatal(err)
78-
}
79-
}
80-
})
81-
8272
// determine line count
8373
lines := 0
8474
fset.Iterate(func(f *token.File) bool {
8575
lines += f.LineCount()
8676
return true
8777
})
8878

89-
d := time.Duration(b.NsPerOp())
90-
fmt.Printf("%s (ignoreFuncBodies = %v):\n", filepath.Base(path), ignoreFuncBodies)
91-
fmt.Printf("\t%s for %d lines (%.0f lines/s)\n", d, lines, float64(lines)/d.Seconds())
92-
fmt.Printf("\t%s\n", b.MemString())
79+
b.ResetTimer()
80+
start := time.Now()
81+
for i := 0; i < b.N; i++ {
82+
conf := Config{
83+
IgnoreFuncBodies: ignoreFuncBodies,
84+
Importer: importer.Default(),
85+
}
86+
var info *Info
87+
if writeInfo {
88+
info = &Info{
89+
Types: make(map[ast.Expr]TypeAndValue),
90+
Defs: make(map[*ast.Ident]Object),
91+
Uses: make(map[*ast.Ident]Object),
92+
Implicits: make(map[ast.Node]Object),
93+
Selections: make(map[*ast.SelectorExpr]*Selection),
94+
Scopes: make(map[ast.Node]*Scope),
95+
}
96+
}
97+
if _, err := conf.Check(path, fset, files, info); err != nil {
98+
b.Fatal(err)
99+
}
100+
}
101+
b.StopTimer()
102+
b.ReportMetric(float64(lines)*float64(b.N)/time.Since(start).Seconds(), "lines/s")
93103
}
94104

95105
func pkgFiles(fset *token.FileSet, path string) ([]*ast.File, error) {

0 commit comments

Comments
 (0)