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

View as plain text