github.com/AndrienkoAleksandr/go@v0.0.19/src/go/parser/performance_test.go (about)

     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  var src = readFile("../printer/nodes.go")
    14  
    15  func readFile(filename string) []byte {
    16  	data, err := os.ReadFile(filename)
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  	return data
    21  }
    22  
    23  func BenchmarkParse(b *testing.B) {
    24  	b.SetBytes(int64(len(src)))
    25  	for i := 0; i < b.N; i++ {
    26  		if _, err := ParseFile(token.NewFileSet(), "", src, ParseComments); err != nil {
    27  			b.Fatalf("benchmark failed due to parse error: %s", err)
    28  		}
    29  	}
    30  }
    31  
    32  func BenchmarkParseOnly(b *testing.B) {
    33  	b.SetBytes(int64(len(src)))
    34  	for i := 0; i < b.N; i++ {
    35  		if _, err := ParseFile(token.NewFileSet(), "", src, ParseComments|SkipObjectResolution); err != nil {
    36  			b.Fatalf("benchmark failed due to parse error: %s", err)
    37  		}
    38  	}
    39  }
    40  
    41  func BenchmarkResolve(b *testing.B) {
    42  	b.SetBytes(int64(len(src)))
    43  	for i := 0; i < b.N; i++ {
    44  		b.StopTimer()
    45  		fset := token.NewFileSet()
    46  		file, err := ParseFile(fset, "", src, SkipObjectResolution)
    47  		if err != nil {
    48  			b.Fatalf("benchmark failed due to parse error: %s", err)
    49  		}
    50  		b.StartTimer()
    51  		handle := fset.File(file.Package)
    52  		resolveFile(file, handle, nil)
    53  	}
    54  }