github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/go/types/self_test.go (about)

     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/internal/gcimporter"
    19  	. "go/types"
    20  )
    21  
    22  var benchmark = flag.Bool("b", false, "run benchmarks")
    23  
    24  func TestSelf(t *testing.T) {
    25  	fset := token.NewFileSet()
    26  	files, err := pkgFiles(fset, ".")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	conf := Config{Importer: importer.Default()}
    32  	_, err = conf.Check("go/types", fset, files, nil)
    33  	if err != nil {
    34  		// Importing go.tools/go/exact doensn't work in the
    35  		// build dashboard environment. Don't report an error
    36  		// for now so that the build remains green.
    37  		// TODO(gri) fix this
    38  		t.Log(err) // replace w/ t.Fatal eventually
    39  		return
    40  	}
    41  }
    42  
    43  func TestBenchmark(t *testing.T) {
    44  	if !*benchmark {
    45  		return
    46  	}
    47  
    48  	// We're not using testing's benchmarking mechanism directly
    49  	// because we want custom output.
    50  
    51  	for _, p := range []string{"types", "exact", "gcimporter"} {
    52  		path := filepath.Join("..", p)
    53  		runbench(t, path, false)
    54  		runbench(t, path, true)
    55  		fmt.Println()
    56  	}
    57  }
    58  
    59  func runbench(t *testing.T, path string, ignoreFuncBodies bool) {
    60  	fset := token.NewFileSet()
    61  	files, err := pkgFiles(fset, path)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  
    66  	b := testing.Benchmark(func(b *testing.B) {
    67  		for i := 0; i < b.N; i++ {
    68  			conf := Config{IgnoreFuncBodies: ignoreFuncBodies}
    69  			conf.Check(path, fset, files, nil)
    70  		}
    71  	})
    72  
    73  	// determine line count
    74  	lines := 0
    75  	fset.Iterate(func(f *token.File) bool {
    76  		lines += f.LineCount()
    77  		return true
    78  	})
    79  
    80  	d := time.Duration(b.NsPerOp())
    81  	fmt.Printf(
    82  		"%s: %s for %d lines (%d lines/s), ignoreFuncBodies = %v\n",
    83  		filepath.Base(path), d, lines, int64(float64(lines)/d.Seconds()), ignoreFuncBodies,
    84  	)
    85  }
    86  
    87  func pkgFiles(fset *token.FileSet, path string) ([]*ast.File, error) {
    88  	filenames, err := pkgFilenames(path) // from stdlib_test.go
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	var files []*ast.File
    94  	for _, filename := range filenames {
    95  		file, err := parser.ParseFile(fset, filename, nil, 0)
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  		files = append(files, file)
   100  	}
   101  
   102  	return files, nil
   103  }