Source file test/bench/go1/parser_test.go

     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  	"strings"
    16  	"testing"
    17  )
    18  
    19  var (
    20  	parserbytes = makeParserBytes()
    21  )
    22  
    23  func makeParserBytes() []byte {
    24  	var r io.Reader
    25  	r = strings.NewReader(parserbz2_base64)
    26  	r = base64.NewDecoder(base64.StdEncoding, r)
    27  	r = bzip2.NewReader(r)
    28  	b, err := io.ReadAll(r)
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  	return b
    33  }
    34  
    35  func BenchmarkGoParse(b *testing.B) {
    36  	b.SetBytes(int64(len(parserbytes)))
    37  	for i := 0; i < b.N; i++ {
    38  		if _, err := parser.ParseFile(token.NewFileSet(), "", parserbytes, parser.ParseComments); err != nil {
    39  			b.Fatalf("benchmark failed due to parse error: %s", err)
    40  		}
    41  	}
    42  }
    43  

View as plain text