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

View as plain text