From 7588ef0d9020c8e628adcd0a0046231252f0d90d Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 11 Mar 2021 18:10:08 -0800 Subject: [PATCH] cmd/compile/internal/types2: use self_test.go from go/types This CL replaces self_test.go with the (improved) version from go/types, modified for types2. To see the differences between go/types/self_test.go and this version, compare against patch set 1. Change-Id: I7ae830a17f7a0de40cc1f5063166a7247f78ec27 Reviewed-on: https://go-review.googlesource.com/c/go/+/300997 Trust: Robert Griesemer Reviewed-by: Robert Findley --- src/cmd/compile/internal/types2/self_test.go | 86 ++++++++++++-------- 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/src/cmd/compile/internal/types2/self_test.go b/src/cmd/compile/internal/types2/self_test.go index 6d7971e50f..4722fec988 100644 --- a/src/cmd/compile/internal/types2/self_test.go +++ b/src/cmd/compile/internal/types2/self_test.go @@ -1,4 +1,3 @@ -// UNREVIEWED // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -7,17 +6,15 @@ package types2_test import ( "cmd/compile/internal/syntax" - "flag" - "fmt" + "path" "path/filepath" + "runtime" "testing" "time" . "cmd/compile/internal/types2" ) -var benchmark = flag.Bool("b", false, "run benchmarks") - func TestSelf(t *testing.T) { files, err := pkgFiles(".") if err != nil { @@ -25,7 +22,7 @@ func TestSelf(t *testing.T) { } conf := Config{Importer: defaultImporter()} - _, err = conf.Check("go/types", files, nil) + _, err = conf.Check("cmd/compile/internal/types2", files, nil) if err != nil { // Importing go/constant doesn't work in the // build dashboard environment. Don't report an error @@ -36,46 +33,69 @@ func TestSelf(t *testing.T) { } } -func TestBenchmark(t *testing.T) { - if !*benchmark { - return - } - - // We're not using testing's benchmarking mechanism directly - // because we want custom output. - - for _, p := range []string{"types", "constant", filepath.Join("internal", "gcimporter")} { - path := filepath.Join("..", p) - runbench(t, path, false) - runbench(t, path, true) - fmt.Println() +func BenchmarkCheck(b *testing.B) { + for _, p := range []string{ + filepath.Join("src", "net", "http"), + filepath.Join("src", "go", "parser"), + filepath.Join("src", "go", "constant"), + filepath.Join("src", "go", "internal", "gcimporter"), + } { + b.Run(path.Base(p), func(b *testing.B) { + path := filepath.Join(runtime.GOROOT(), p) + for _, ignoreFuncBodies := range []bool{false, true} { + name := "funcbodies" + if ignoreFuncBodies { + name = "nofuncbodies" + } + b.Run(name, func(b *testing.B) { + b.Run("info", func(b *testing.B) { + runbench(b, path, ignoreFuncBodies, true) + }) + b.Run("noinfo", func(b *testing.B) { + runbench(b, path, ignoreFuncBodies, false) + }) + }) + } + }) } } -func runbench(t *testing.T, path string, ignoreFuncBodies bool) { +func runbench(b *testing.B, path string, ignoreFuncBodies, writeInfo bool) { files, err := pkgFiles(path) if err != nil { - t.Fatal(err) + b.Fatal(err) } - b := testing.Benchmark(func(b *testing.B) { - for i := 0; i < b.N; i++ { - conf := Config{IgnoreFuncBodies: ignoreFuncBodies} - conf.Check(path, files, nil) - } - }) - // determine line count var lines uint for _, f := range files { lines += f.EOF.Line() } - d := time.Duration(b.NsPerOp()) - fmt.Printf( - "%s: %s for %d lines (%d lines/s), ignoreFuncBodies = %v\n", - filepath.Base(path), d, lines, int64(float64(lines)/d.Seconds()), ignoreFuncBodies, - ) + b.ResetTimer() + start := time.Now() + for i := 0; i < b.N; i++ { + conf := Config{ + IgnoreFuncBodies: ignoreFuncBodies, + Importer: defaultImporter(), + } + var info *Info + if writeInfo { + info = &Info{ + Types: make(map[syntax.Expr]TypeAndValue), + Defs: make(map[*syntax.Name]Object), + Uses: make(map[*syntax.Name]Object), + Implicits: make(map[syntax.Node]Object), + Selections: make(map[*syntax.SelectorExpr]*Selection), + Scopes: make(map[syntax.Node]*Scope), + } + } + if _, err := conf.Check(path, files, info); err != nil { + b.Fatal(err) + } + } + b.StopTimer() + b.ReportMetric(float64(lines)*float64(b.N)/time.Since(start).Seconds(), "lines/s") } func pkgFiles(path string) ([]*syntax.File, error) { -- 2.48.1