...
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "github.com/gobwas/glob"
7 "os"
8 "strings"
9 "testing"
10 "unicode/utf8"
11 )
12
13 func benchString(r testing.BenchmarkResult) string {
14 nsop := r.NsPerOp()
15 ns := fmt.Sprintf("%10d ns/op", nsop)
16 allocs := "0"
17 if r.N > 0 {
18 if nsop < 100 {
19
20
21 if nsop < 10 {
22 ns = fmt.Sprintf("%13.2f ns/op", float64(r.T.Nanoseconds())/float64(r.N))
23 } else {
24 ns = fmt.Sprintf("%12.1f ns/op", float64(r.T.Nanoseconds())/float64(r.N))
25 }
26 }
27
28 allocs = fmt.Sprintf("%d", r.MemAllocs/uint64(r.N))
29 }
30
31 return fmt.Sprintf("%8d\t%s\t%s allocs", r.N, ns, allocs)
32 }
33
34 func main() {
35 pattern := flag.String("p", "", "pattern to draw")
36 sep := flag.String("s", "", "comma separated list of separators")
37 fixture := flag.String("f", "", "fixture")
38 verbose := flag.Bool("v", false, "verbose")
39 flag.Parse()
40
41 if *pattern == "" {
42 flag.Usage()
43 os.Exit(1)
44 }
45
46 var separators []rune
47 for _, c := range strings.Split(*sep, ",") {
48 if r, w := utf8.DecodeRuneInString(c); len(c) > w {
49 fmt.Println("only single charactered separators are allowed")
50 os.Exit(1)
51 } else {
52 separators = append(separators, r)
53 }
54 }
55
56 g, err := glob.Compile(*pattern, separators...)
57 if err != nil {
58 fmt.Println("could not compile pattern:", err)
59 os.Exit(1)
60 }
61
62 if !*verbose {
63 fmt.Println(g.Match(*fixture))
64 return
65 }
66
67 fmt.Printf("result: %t\n", g.Match(*fixture))
68
69 cb := testing.Benchmark(func(b *testing.B) {
70 for i := 0; i < b.N; i++ {
71 glob.Compile(*pattern, separators...)
72 }
73 })
74 fmt.Println("compile:", benchString(cb))
75
76 mb := testing.Benchmark(func(b *testing.B) {
77 for i := 0; i < b.N; i++ {
78 g.Match(*fixture)
79 }
80 })
81 fmt.Println("match: ", benchString(mb))
82 }
83
View as plain text