...

Source file src/go/types/self_test.go

Documentation: go/types

		 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  		// Importing go/constant doesn't work in the
		31  		// build dashboard environment. Don't report an error
		32  		// for now so that the build remains green.
		33  		// TODO(gri) fix this
		34  		t.Log(err) // replace w/ t.Fatal eventually
		35  		return
		36  	}
		37  }
		38  
		39  func BenchmarkCheck(b *testing.B) {
		40  	for _, p := range []string{
		41  		"net/http",
		42  		"go/parser",
		43  		"go/constant",
		44  		filepath.Join("go", "internal", "gcimporter"),
		45  	} {
		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  		})
		63  	}
		64  }
		65  
		66  func runbench(b *testing.B, path string, ignoreFuncBodies, writeInfo bool) {
		67  	fset := token.NewFileSet()
		68  	files, err := pkgFiles(fset, path, 0)
		69  	if err != nil {
		70  		b.Fatal(err)
		71  	}
		72  	// determine line count
		73  	lines := 0
		74  	fset.Iterate(func(f *token.File) bool {
		75  		lines += f.LineCount()
		76  		return true
		77  	})
		78  
		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")
	 103  }
	 104  
	 105  func pkgFiles(fset *token.FileSet, path string, mode parser.Mode) ([]*ast.File, error) {
	 106  	filenames, err := pkgFilenames(path) // from stdlib_test.go
	 107  	if err != nil {
	 108  		return nil, err
	 109  	}
	 110  
	 111  	var files []*ast.File
	 112  	for _, filename := range filenames {
	 113  		file, err := parser.ParseFile(fset, filename, nil, mode)
	 114  		if err != nil {
	 115  			return nil, err
	 116  		}
	 117  		files = append(files, file)
	 118  	}
	 119  
	 120  	return files, nil
	 121  }
	 122  

View as plain text