github.com/lovishpuri/go-40569/src@v0.0.0-20230519171745-f8623e7c56cf/go/types/lookup_test.go (about) 1 // Copyright 2022 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/importer" 9 "go/token" 10 "path/filepath" 11 "runtime" 12 "testing" 13 14 . "go/types" 15 ) 16 17 // BenchmarkLookupFieldOrMethod measures types.LookupFieldOrMethod performance. 18 // LookupFieldOrMethod is a performance hotspot for both type-checking and 19 // external API calls. 20 func BenchmarkLookupFieldOrMethod(b *testing.B) { 21 // Choose an arbitrary, large package. 22 path := filepath.Join(runtime.GOROOT(), "src", "net", "http") 23 24 fset := token.NewFileSet() 25 files, err := pkgFiles(fset, path) 26 if err != nil { 27 b.Fatal(err) 28 } 29 30 conf := Config{ 31 Importer: importer.Default(), 32 } 33 34 pkg, err := conf.Check("http", fset, files, nil) 35 if err != nil { 36 b.Fatal(err) 37 } 38 39 scope := pkg.Scope() 40 names := scope.Names() 41 42 // Look up an arbitrary name for each type referenced in the package scope. 43 lookup := func() { 44 for _, name := range names { 45 typ := scope.Lookup(name).Type() 46 LookupFieldOrMethod(typ, true, pkg, "m") 47 } 48 } 49 50 // Perform a lookup once, to ensure that any lazily-evaluated state is 51 // complete. 52 lookup() 53 54 b.ResetTimer() 55 for i := 0; i < b.N; i++ { 56 lookup() 57 } 58 }