Home | History | Annotate | Download | only in 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 	"flag"
      9 	"fmt"
     10 	"go/ast"
     11 	"go/importer"
     12 	"go/parser"
     13 	"go/token"
     14 	"path/filepath"
     15 	"testing"
     16 	"time"
     17 
     18 	. "go/types"
     19 )
     20 
     21 var benchmark = flag.Bool("b", false, "run benchmarks")
     22 
     23 func TestSelf(t *testing.T) {
     24 	fset := token.NewFileSet()
     25 	files, err := pkgFiles(fset, ".")
     26 	if err != nil {
     27 		t.Fatal(err)
     28 	}
     29 
     30 	conf := Config{Importer: importer.Default()}
     31 	_, err = conf.Check("go/types", fset, files, nil)
     32 	if err != nil {
     33 		// Importing go/constant doesn't work in the
     34 		// build dashboard environment. Don't report an error
     35 		// for now so that the build remains green.
     36 		// TODO(gri) fix this
     37 		t.Log(err) // replace w/ t.Fatal eventually
     38 		return
     39 	}
     40 }
     41 
     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 
     50 	for _, p := range []string{"types", "constant", filepath.Join("internal", "gcimporter")} {
     51 		path := filepath.Join("..", p)
     52 		runbench(t, path, false)
     53 		runbench(t, path, true)
     54 		fmt.Println()
     55 	}
     56 }
     57 
     58 func runbench(t *testing.T, path string, ignoreFuncBodies bool) {
     59 	fset := token.NewFileSet()
     60 	files, err := pkgFiles(fset, path)
     61 	if err != nil {
     62 		t.Fatal(err)
     63 	}
     64 
     65 	b := testing.Benchmark(func(b *testing.B) {
     66 		for i := 0; i < b.N; i++ {
     67 			conf := Config{IgnoreFuncBodies: ignoreFuncBodies}
     68 			conf.Check(path, fset, files, nil)
     69 		}
     70 	})
     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 	d := time.Duration(b.NsPerOp())
     80 	fmt.Printf(
     81 		"%s: %s for %d lines (%d lines/s), ignoreFuncBodies = %v\n",
     82 		filepath.Base(path), d, lines, int64(float64(lines)/d.Seconds()), ignoreFuncBodies,
     83 	)
     84 }
     85 
     86 func pkgFiles(fset *token.FileSet, path string) ([]*ast.File, error) {
     87 	filenames, err := pkgFilenames(path) // from stdlib_test.go
     88 	if err != nil {
     89 		return nil, err
     90 	}
     91 
     92 	var files []*ast.File
     93 	for _, filename := range filenames {
     94 		file, err := parser.ParseFile(fset, filename, nil, 0)
     95 		if err != nil {
     96 			return nil, err
     97 		}
     98 		files = append(files, file)
     99 	}
    100 
    101 	return files, nil
    102 }
    103