modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/bench/go1/parser_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 go1
     6  
     7  // go parser benchmark based on go/parser/performance_test.go
     8  
     9  import (
    10  	"compress/bzip2"
    11  	"encoding/base64"
    12  	"go/parser"
    13  	"go/token"
    14  	"io"
    15  	"io/ioutil"
    16  	"strings"
    17  	"testing"
    18  )
    19  
    20  var (
    21  	parserbytes = makeParserBytes()
    22  )
    23  
    24  func makeParserBytes() []byte {
    25  	var r io.Reader
    26  	r = strings.NewReader(parserbz2_base64)
    27  	r = base64.NewDecoder(base64.StdEncoding, r)
    28  	r = bzip2.NewReader(r)
    29  	b, err := ioutil.ReadAll(r)
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	return b
    34  }
    35  
    36  func BenchmarkGoParse(b *testing.B) {
    37  	b.SetBytes(int64(len(parserbytes)))
    38  	for i := 0; i < b.N; i++ {
    39  		if _, err := parser.ParseFile(token.NewFileSet(), "", parserbytes, parser.ParseComments); err != nil {
    40  			b.Fatalf("benchmark failed due to parse error: %s", err)
    41  		}
    42  	}
    43  }