...

Source file src/go/parser/performance_test.go

Documentation: go/parser

		 1  // Copyright 2012 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 parser
		 6  
		 7  import (
		 8  	"go/token"
		 9  	"os"
		10  	"testing"
		11  )
		12  
		13  // TODO(rFindley): use a testdata file or file from another package here, to
		14  //								 avoid a moving target.
		15  var src = readFile("parser.go")
		16  
		17  func readFile(filename string) []byte {
		18  	data, err := os.ReadFile(filename)
		19  	if err != nil {
		20  		panic(err)
		21  	}
		22  	return data
		23  }
		24  
		25  func BenchmarkParse(b *testing.B) {
		26  	b.SetBytes(int64(len(src)))
		27  	for i := 0; i < b.N; i++ {
		28  		if _, err := ParseFile(token.NewFileSet(), "", src, ParseComments); err != nil {
		29  			b.Fatalf("benchmark failed due to parse error: %s", err)
		30  		}
		31  	}
		32  }
		33  
		34  func BenchmarkParseOnly(b *testing.B) {
		35  	b.SetBytes(int64(len(src)))
		36  	for i := 0; i < b.N; i++ {
		37  		if _, err := ParseFile(token.NewFileSet(), "", src, ParseComments|SkipObjectResolution); err != nil {
		38  			b.Fatalf("benchmark failed due to parse error: %s", err)
		39  		}
		40  	}
		41  }
		42  
		43  func BenchmarkResolve(b *testing.B) {
		44  	b.SetBytes(int64(len(src)))
		45  	for i := 0; i < b.N; i++ {
		46  		b.StopTimer()
		47  		fset := token.NewFileSet()
		48  		file, err := ParseFile(fset, "", src, SkipObjectResolution)
		49  		if err != nil {
		50  			b.Fatalf("benchmark failed due to parse error: %s", err)
		51  		}
		52  		b.StartTimer()
		53  		handle := fset.File(file.Package)
		54  		resolveFile(file, handle, nil)
		55  	}
		56  }
		57  

View as plain text