Source file
src/go/printer/performance_test.go
1
2
3
4
5
6
7
8 package printer
9
10 import (
11 "bytes"
12 "go/ast"
13 "go/parser"
14 "io"
15 "log"
16 "os"
17 "testing"
18 )
19
20 var testfile *ast.File
21
22 func testprint(out io.Writer, file *ast.File) {
23 if err := (&Config{TabIndent | UseSpaces | normalizeNumbers, 8, 0}).Fprint(out, fset, file); err != nil {
24 log.Fatalf("print error: %s", err)
25 }
26 }
27
28
29 func initialize() {
30 const filename = "testdata/parser.go"
31
32 src, err := os.ReadFile(filename)
33 if err != nil {
34 log.Fatalf("%s", err)
35 }
36
37 file, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
38 if err != nil {
39 log.Fatalf("%s", err)
40 }
41
42 var buf bytes.Buffer
43 testprint(&buf, file)
44 if !bytes.Equal(buf.Bytes(), src) {
45 log.Fatalf("print error: %s not idempotent", filename)
46 }
47
48 testfile = file
49 }
50
51 func BenchmarkPrint(b *testing.B) {
52 if testfile == nil {
53 initialize()
54 }
55 for i := 0; i < b.N; i++ {
56 testprint(io.Discard, testfile)
57 }
58 }
59
View as plain text