main.go 1.8 KB

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