mirror of
https://github.com/golang/go
synced 2024-11-21 15:24:45 -07:00
go/printer: added simple performance benchmark
R=r, dfc, bradfitzwork, bradfitz CC=golang-dev https://golang.org/cl/4441078
This commit is contained in:
parent
548c9c8624
commit
da9b8b8352
62
src/pkg/go/printer/performance_test.go
Normal file
62
src/pkg/go/printer/performance_test.go
Normal file
@ -0,0 +1,62 @@
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// This file implements a simple printer performance benchmark:
|
||||
// gotest -bench=BenchmarkPrint
|
||||
|
||||
package printer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
||||
var testfile *ast.File
|
||||
|
||||
|
||||
func testprint(out io.Writer, file *ast.File) {
|
||||
if _, err := (&Config{TabIndent | UseSpaces, 8}).Fprint(out, fset, file); err != nil {
|
||||
log.Fatalf("print error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// cannot initialize in init because (printer) Fprint launches goroutines.
|
||||
func initialize() {
|
||||
const filename = "testdata/parser.go"
|
||||
|
||||
src, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
log.Fatalf("%s", err)
|
||||
}
|
||||
|
||||
file, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
|
||||
if err != nil {
|
||||
log.Fatalf("%s", err)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
testprint(&buf, file)
|
||||
if !bytes.Equal(buf.Bytes(), src) {
|
||||
log.Fatalf("print error: %s not idempotent", filename)
|
||||
}
|
||||
|
||||
testfile = file
|
||||
}
|
||||
|
||||
|
||||
func BenchmarkPrint(b *testing.B) {
|
||||
if testfile == nil {
|
||||
initialize()
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
testprint(ioutil.Discard, testfile)
|
||||
}
|
||||
}
|
2252
src/pkg/go/printer/testdata/parser.go
vendored
Normal file
2252
src/pkg/go/printer/testdata/parser.go
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user