1
0
mirror of https://github.com/golang/go synced 2024-11-26 07:17:59 -07:00

go/scanner: improve variety in benchmarks for file scanning

BenchmarkScanFile was scanning scanner.go, which makes comparison
difficult for a CL modifying that file. That file is also is not
necessarily representative syntax.

Add a few additional files as subtests to provide a larger variety of
metrics.

Change-Id: Ib78303c2546debd84a0b5478ae438ba891d9e6e9
Reviewed-on: https://go-review.googlesource.com/c/go/+/308610
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Rob Findley 2021-04-07 17:43:09 -04:00 committed by Robert Findley
parent 6fa12172d9
commit bc6288794d

View File

@ -889,26 +889,37 @@ func BenchmarkScan(b *testing.B) {
}
}
func BenchmarkScanFile(b *testing.B) {
b.StopTimer()
const filename = "scanner.go"
src, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
fset := token.NewFileSet()
file := fset.AddFile(filename, fset.Base(), len(src))
b.SetBytes(int64(len(src)))
var s Scanner
b.StartTimer()
for i := 0; i < b.N; i++ {
s.Init(file, src, nil, ScanComments)
for {
_, tok, _ := s.Scan()
if tok == token.EOF {
break
func BenchmarkScanFiles(b *testing.B) {
// Scan a few arbitrary large files, and one small one, to provide some
// variety in benchmarks.
for _, p := range []string{
"go/types/expr.go",
"go/parser/parser.go",
"net/http/server.go",
"go/scanner/errors.go",
} {
b.Run(p, func(b *testing.B) {
b.StopTimer()
filename := filepath.Join("..", "..", filepath.FromSlash(p))
src, err := os.ReadFile(filename)
if err != nil {
b.Fatal(err)
}
}
fset := token.NewFileSet()
file := fset.AddFile(filename, fset.Base(), len(src))
b.SetBytes(int64(len(src)))
var s Scanner
b.StartTimer()
for i := 0; i < b.N; i++ {
s.Init(file, src, nil, ScanComments)
for {
_, tok, _ := s.Scan()
if tok == token.EOF {
break
}
}
}
})
}
}