Debian Codegraph @

syntax/bench_test.go

raw
1 // Copyright (c) 2016, Daniel Martí <[email protected]>
2 // See LICENSE for licensing information
3
4 package syntax
5
6 import (
7 "io"
8 "strings"
9 "testing"
10 )
11
12 func BenchmarkParse(b *testing.B) {
13 b.ReportAllocs()
14 src := "" +
15 strings.Repeat("\n\n\t\t \n", 10) +
16 "# " + strings.Repeat("foo bar ", 10) + "\n" +
17 strings.Repeat("longlit_", 10) + "\n" +
18 "'" + strings.Repeat("foo bar ", 10) + "'\n" +
19 `"` + strings.Repeat("foo bar ", 10) + `"` + "\n" +
20 strings.Repeat("aa bb cc dd; ", 6) +
21 "a() { (b); { c; }; }; $(d; `e`)\n" +
22 "foo=bar; a=b; c=d$foo${bar}e $simple ${complex:-default}\n" +
23 "if a; then while b; do for c in d e; do f; done; done; fi\n" +
24 "a | b && c || d | e && g || f\n" +
25 "foo >a <b <<<c 2>&1 <<EOF\n" +
26 strings.Repeat("somewhat long heredoc line\n", 10) +
27 "EOF" +
28 ""
29 p := NewParser(KeepComments(true))
30 in := strings.NewReader(src)
31 for b.Loop() {
32 if _, err := p.Parse(in, ""); err != nil {
33 b.Fatal(err)
34 }
35 in.Reset(src)
36 }
37 }
38
39 func BenchmarkPrint(b *testing.B) {
40 b.ReportAllocs()
41 prog := parsePath(b, canonicalPath)
42 printer := NewPrinter()
43 for b.Loop() {
44 if err := printer.Print(io.Discard, prog); err != nil {
45 b.Fatal(err)
46 }
47 }
48 }