Source file src/go/types/self_test.go

     1  // Copyright 2013 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 types_test
     6  
     7  import (
     8  	"go/ast"
     9  	"go/importer"
    10  	"go/parser"
    11  	"go/token"
    12  	"path"
    13  	"path/filepath"
    14  	"testing"
    15  	"time"
    16  
    17  	. "go/types"
    18  )
    19  
    20  func TestSelf(t *testing.T) {
    21  	fset := token.NewFileSet()
    22  	files, err := pkgFiles(fset, ".", 0)
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	conf := Config{Importer: importer.Default()}
    28  	_, err = conf.Check("go/types", fset, files, nil)
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  }
    33  
    34  func BenchmarkCheck(b *testing.B) {
    35  	for _, p := range []string{
    36  		"net/http",
    37  		"go/parser",
    38  		"go/constant",
    39  		"runtime",
    40  		filepath.Join("go", "internal", "gcimporter"),
    41  	} {
    42  		b.Run(path.Base(p), func(b *testing.B) {
    43  			path := filepath.Join("..", "..", p)
    44  			for _, ignoreFuncBodies := range []bool{false, true} {
    45  				name := "funcbodies"
    46  				if ignoreFuncBodies {
    47  					name = "nofuncbodies"
    48  				}
    49  				b.Run(name, func(b *testing.B) {
    50  					b.Run("info", func(b *testing.B) {
    51  						runbench(b, path, ignoreFuncBodies, true)
    52  					})
    53  					b.Run("noinfo", func(b *testing.B) {
    54  						runbench(b, path, ignoreFuncBodies, false)
    55  					})
    56  				})
    57  			}
    58  		})
    59  	}
    60  }
    61  
    62  func runbench(b *testing.B, path string, ignoreFuncBodies, writeInfo bool) {
    63  	fset := token.NewFileSet()
    64  	files, err := pkgFiles(fset, path, 0)
    65  	if err != nil {
    66  		b.Fatal(err)
    67  	}
    68  	// determine line count
    69  	lines := 0
    70  	fset.Iterate(func(f *token.File) bool {
    71  		lines += f.LineCount()
    72  		return true
    73  	})
    74  
    75  	b.ResetTimer()
    76  	start := time.Now()
    77  	for i := 0; i < b.N; i++ {
    78  		conf := Config{
    79  			IgnoreFuncBodies: ignoreFuncBodies,
    80  			Importer:         importer.Default(),
    81  		}
    82  		var info *Info
    83  		if writeInfo {
    84  			info = &Info{
    85  				Types:      make(map[ast.Expr]TypeAndValue),
    86  				Defs:       make(map[*ast.Ident]Object),
    87  				Uses:       make(map[*ast.Ident]Object),
    88  				Implicits:  make(map[ast.Node]Object),
    89  				Selections: make(map[*ast.SelectorExpr]*Selection),
    90  				Scopes:     make(map[ast.Node]*Scope),
    91  			}
    92  		}
    93  		if _, err := conf.Check(path, fset, files, info); err != nil {
    94  			b.Fatal(err)
    95  		}
    96  	}
    97  	b.StopTimer()
    98  	b.ReportMetric(float64(lines)*float64(b.N)/time.Since(start).Seconds(), "lines/s")
    99  }
   100  
   101  func pkgFiles(fset *token.FileSet, path string, mode parser.Mode) ([]*ast.File, error) {
   102  	filenames, err := pkgFilenames(path) // from stdlib_test.go
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	var files []*ast.File
   108  	for _, filename := range filenames {
   109  		file, err := parser.ParseFile(fset, filename, nil, mode)
   110  		if err != nil {
   111  			return nil, err
   112  		}
   113  		files = append(files, file)
   114  	}
   115  
   116  	return files, nil
   117  }
   118  

View as plain text