From efc9b31dc62376a7d1f66f3da4d81baca6c2a526 Mon Sep 17 00:00:00 2001 From: "s.kamardin" Date: Wed, 20 Jan 2016 21:05:54 +0300 Subject: [PATCH] add mismtach benchs --- glob_test.go | 342 ++++++++++++++++++++++++++++++++++++--------------- readme.md | 42 ++++--- 2 files changed, 274 insertions(+), 110 deletions(-) diff --git a/glob_test.go b/glob_test.go index 6325a60..76ccf75 100644 --- a/glob_test.go +++ b/glob_test.go @@ -11,34 +11,40 @@ import ( ) const ( - pattern_all = "[a-z][!a-x]*cat*[h][!b]*eyes*" - regexp_all = `^[a-z][^a-x].*cat.*[h][^b].*eyes.*$` - fixture_all = "my cat has very bright eyes" + pattern_all = "[a-z][!a-x]*cat*[h][!b]*eyes*" + regexp_all = `^[a-z][^a-x].*cat.*[h][^b].*eyes.*$` + fixture_all_match = "my cat has very bright eyes" + fixture_all_mismatch = "my dog has very bright eyes" - pattern_plain = "google.com" - regexp_plain = `^google\.com$` - fixture_plain = "google.com" + pattern_plain = "google.com" + regexp_plain = `^google\.com$` + fixture_plain_match = "google.com" + fixture_plain_mismatch = "gobwas.com" - pattern_multiple = "https://*.google.*" - regexp_multiple = `^https:\/\/.*\.google\..*$` - fixture_multiple = "https://account.google.com" + pattern_multiple = "https://*.google.*" + regexp_multiple = `^https:\/\/.*\.google\..*$` + fixture_multiple_match = "https://account.google.com" + fixture_multiple_mismatch = "https://google.com" - pattern_alternatives = "{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}" - regexp_alternatives = `^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$` - fixture_alternatives = "http://yahoo.com" + pattern_alternatives = "{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}" + regexp_alternatives = `^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$` + fixture_alternatives_match = "http://yahoo.com" + fixture_alternatives_mismatch = "http://google.com" - pattern_alternatives_suffix = "{https://*gobwas.com,http://exclude.gobwas.com}" - regexp_alternatives_suffix = `^(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)$` - fixture_alternatives_suffix_first = "https://safe.gobwas.com" - fixture_alternatives_suffix_second = "http://exclude.gobwas.com" + pattern_alternatives_suffix = "{https://*gobwas.com,http://exclude.gobwas.com}" + regexp_alternatives_suffix = `^(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)$` + fixture_alternatives_suffix_first_match = "https://safe.gobwas.com" + fixture_alternatives_suffix_first_mismatch = "http://safe.gobwas.com" + fixture_alternatives_suffix_second = "http://exclude.gobwas.com" - pattern_prefix = "abc*" - regexp_prefix = `^abc.*$` - pattern_suffix = "*def" - regexp_suffix = `^.*def$` - pattern_prefix_suffix = "ab*ef" - regexp_prefix_suffix = `^ab.*ef$` - fixture_prefix_suffix = "abcdef" + pattern_prefix = "abc*" + regexp_prefix = `^abc.*$` + pattern_suffix = "*def" + regexp_suffix = `^.*def$` + pattern_prefix_suffix = "ab*ef" + regexp_prefix_suffix = `^ab.*ef$` + fixture_prefix_suffix_match = "abcdef" + fixture_prefix_suffix_mismatch = "af" pattern_alternatives_combine_lite = "{abc*def,abc?def,abc[zte]def}" regexp_alternatives_combine_lite = `^(abc.*def|abc.def|abc[zte]def)$` @@ -111,22 +117,25 @@ func DrawPatterns(t *testing.T) { pattern string sep string }{ - { - pattern: pattern_all, - }, + // { + // pattern: pattern_all, + // }, + // { + // pattern: pattern_alternatives_suffix, + // sep: separators, + // }, + // { + // pattern: pattern_alternatives_combine_lite, + // }, + // { + // pattern: pattern_alternatives_combine_hard, + // }, { pattern: pattern_alternatives_suffix, - sep: separators, - }, - { - pattern: pattern_alternatives_combine_lite, - }, - { - pattern: pattern_alternatives_combine_hard, - }, - { - pattern: "{https://*.mail.ru,*my.mail.ru,*my.myalpha*.i.mail.ru}", }, + // { + // pattern: "{https://*.mail.ru,*my.mail.ru,*my.myalpha*.i.mail.ru}", + // }, } { glob, err := Compile(test.pattern, test.sep) if err != nil { @@ -236,17 +245,34 @@ func TestGlob(t *testing.T) { glob(true, "{*,**}{a,b}", "ab"), glob(false, "{*,**}{a,b}", "ac"), - glob(true, pattern_all, fixture_all), - glob(true, pattern_plain, fixture_plain), - glob(true, pattern_multiple, fixture_multiple), - glob(true, pattern_alternatives, fixture_alternatives), - glob(true, pattern_alternatives_suffix, fixture_alternatives_suffix_first), + glob(true, pattern_all, fixture_all_match), + glob(false, pattern_all, fixture_all_mismatch), + + glob(true, pattern_plain, fixture_plain_match), + glob(false, pattern_plain, fixture_plain_mismatch), + + glob(true, pattern_multiple, fixture_multiple_match), + glob(false, pattern_multiple, fixture_multiple_mismatch), + + glob(true, pattern_alternatives, fixture_alternatives_match), + glob(false, pattern_alternatives, fixture_alternatives_mismatch), + + glob(true, pattern_alternatives_suffix, fixture_alternatives_suffix_first_match), + glob(false, pattern_alternatives_suffix, fixture_alternatives_suffix_first_mismatch), glob(true, pattern_alternatives_suffix, fixture_alternatives_suffix_second), + glob(true, pattern_alternatives_combine_hard, fixture_alternatives_combine_hard), + glob(true, pattern_alternatives_combine_lite, fixture_alternatives_combine_lite), - glob(true, pattern_prefix, fixture_prefix_suffix), - glob(true, pattern_suffix, fixture_prefix_suffix), - glob(true, pattern_prefix_suffix, fixture_prefix_suffix), + + glob(true, pattern_prefix, fixture_prefix_suffix_match), + glob(false, pattern_prefix, fixture_prefix_suffix_mismatch), + + glob(true, pattern_suffix, fixture_prefix_suffix_match), + glob(false, pattern_suffix, fixture_prefix_suffix_mismatch), + + glob(true, pattern_prefix_suffix, fixture_prefix_suffix_match), + glob(false, pattern_prefix_suffix, fixture_prefix_suffix_mismatch), } { g, err := Compile(test.pattern, test.delimiters...) if err != nil { @@ -258,10 +284,21 @@ func TestGlob(t *testing.T) { if result != test.should { t.Errorf("pattern %q matching %q should be %v but got %v\n%s", test.pattern, test.match, test.should, result, g) } + + // if len(test.delimiters) == 0 || (len(test.delimiters) == 1 && test.delimiters[0] == string(filepath.Separator)) { + // result, err = filepath.Match(test.pattern, test.match) + // if err != nil { + // t.Errorf("[filepath] error matching pattern %q: %s", test.pattern, err) + // continue + // } + // if result != test.should { + // t.Errorf("[filepath] pattern %q matching %q should be %v but got %v\n%s", test.pattern, test.match, test.should, result, g) + // } + // } } } -func BenchmarkParse(b *testing.B) { +func BenchmarkParseGlob(b *testing.B) { for i := 0; i < b.N; i++ { Compile(pattern_all) } @@ -272,90 +309,151 @@ func BenchmarkParseRegexp(b *testing.B) { } } -func BenchmarkAll(b *testing.B) { +func BenchmarkAllGlobMatch(b *testing.B) { m, _ := Compile(pattern_all) for i := 0; i < b.N; i++ { - _ = m.Match(fixture_all) + _ = m.Match(fixture_all_match) } } -func BenchmarkAllRegexp(b *testing.B) { +func BenchmarkAllRegexpMatch(b *testing.B) { m := regexp.MustCompile(regexp_all) - f := []byte(fixture_all) + f := []byte(fixture_all_match) + + for i := 0; i < b.N; i++ { + _ = m.Match(f) + } +} +func BenchmarkAllGlobMismatch(b *testing.B) { + m, _ := Compile(pattern_all) + + for i := 0; i < b.N; i++ { + _ = m.Match(fixture_all_mismatch) + } +} +func BenchmarkAllRegexpMismatch(b *testing.B) { + m := regexp.MustCompile(regexp_all) + f := []byte(fixture_all_mismatch) for i := 0; i < b.N; i++ { _ = m.Match(f) } } -func BenchmarkMultiple(b *testing.B) { +func BenchmarkMultipleGlobMatch(b *testing.B) { m, _ := Compile(pattern_multiple) for i := 0; i < b.N; i++ { - _ = m.Match(fixture_multiple) + _ = m.Match(fixture_multiple_match) } } -func BenchmarkMultipleRegexp(b *testing.B) { +func BenchmarkMultipleRegexpMatch(b *testing.B) { m := regexp.MustCompile(regexp_multiple) - f := []byte(fixture_multiple) + f := []byte(fixture_multiple_match) + + for i := 0; i < b.N; i++ { + _ = m.Match(f) + } +} +func BenchmarkMultipleGlobMismatch(b *testing.B) { + m, _ := Compile(pattern_multiple) + + for i := 0; i < b.N; i++ { + _ = m.Match(fixture_multiple_mismatch) + } +} +func BenchmarkMultipleRegexpMismatch(b *testing.B) { + m := regexp.MustCompile(regexp_multiple) + f := []byte(fixture_multiple_mismatch) for i := 0; i < b.N; i++ { _ = m.Match(f) } } -func BenchmarkAlternatives(b *testing.B) { +func BenchmarkAlternativesGlobMatch(b *testing.B) { m, _ := Compile(pattern_alternatives) for i := 0; i < b.N; i++ { - _ = m.Match(fixture_alternatives) + _ = m.Match(fixture_alternatives_match) } } -func BenchmarkAlternativesSuffixFirst(b *testing.B) { +func BenchmarkAlternativesGlobMismatch(b *testing.B) { + m, _ := Compile(pattern_alternatives) + + for i := 0; i < b.N; i++ { + _ = m.Match(fixture_alternatives_mismatch) + } +} +func BenchmarkAlternativesRegexpMatch(b *testing.B) { + m := regexp.MustCompile(regexp_alternatives) + f := []byte(fixture_alternatives_match) + + for i := 0; i < b.N; i++ { + _ = m.Match(f) + } +} +func BenchmarkAlternativesRegexpMismatch(b *testing.B) { + m := regexp.MustCompile(regexp_alternatives) + f := []byte(fixture_alternatives_mismatch) + + for i := 0; i < b.N; i++ { + _ = m.Match(f) + } +} + +func BenchmarkAlternativesSuffixFirstGlobMatch(b *testing.B) { m, _ := Compile(pattern_alternatives_suffix) for i := 0; i < b.N; i++ { - _ = m.Match(fixture_alternatives_suffix_first) + _ = m.Match(fixture_alternatives_suffix_first_match) } } -func BenchmarkAlternativesSuffixSecond(b *testing.B) { +func BenchmarkAlternativesSuffixFirstGlobMismatch(b *testing.B) { + m, _ := Compile(pattern_alternatives_suffix) + + for i := 0; i < b.N; i++ { + _ = m.Match(fixture_alternatives_suffix_first_mismatch) + } +} +func BenchmarkAlternativesSuffixSecondGlobMatch(b *testing.B) { m, _ := Compile(pattern_alternatives_suffix) for i := 0; i < b.N; i++ { _ = m.Match(fixture_alternatives_suffix_second) } } -func BenchmarkAlternativesCombineLite(b *testing.B) { +func BenchmarkAlternativesCombineLiteGlobMatch(b *testing.B) { m, _ := Compile(pattern_alternatives_combine_lite) for i := 0; i < b.N; i++ { _ = m.Match(fixture_alternatives_combine_lite) } } -func BenchmarkAlternativesCombineHard(b *testing.B) { +func BenchmarkAlternativesCombineHardGlobMatch(b *testing.B) { m, _ := Compile(pattern_alternatives_combine_hard) for i := 0; i < b.N; i++ { _ = m.Match(fixture_alternatives_combine_hard) } } -func BenchmarkAlternativesRegexp(b *testing.B) { - m := regexp.MustCompile(regexp_alternatives) - f := []byte(fixture_alternatives) - - for i := 0; i < b.N; i++ { - _ = m.Match(f) - } -} -func BenchmarkAlternativesSuffixFirstRegexp(b *testing.B) { +func BenchmarkAlternativesSuffixFirstRegexpMatch(b *testing.B) { m := regexp.MustCompile(regexp_alternatives_suffix) - f := []byte(fixture_alternatives_suffix_first) + f := []byte(fixture_alternatives_suffix_first_match) for i := 0; i < b.N; i++ { _ = m.Match(f) } } -func BenchmarkAlternativesSuffixSecondRegexp(b *testing.B) { +func BenchmarkAlternativesSuffixFirstRegexpMismatch(b *testing.B) { + m := regexp.MustCompile(regexp_alternatives_suffix) + f := []byte(fixture_alternatives_suffix_first_mismatch) + + for i := 0; i < b.N; i++ { + _ = m.Match(f) + } +} +func BenchmarkAlternativesSuffixSecondRegexpMatch(b *testing.B) { m := regexp.MustCompile(regexp_alternatives_suffix) f := []byte(fixture_alternatives_suffix_second) @@ -363,7 +461,7 @@ func BenchmarkAlternativesSuffixSecondRegexp(b *testing.B) { _ = m.Match(f) } } -func BenchmarkAlternativesCombineLiteRegexp(b *testing.B) { +func BenchmarkAlternativesCombineLiteRegexpMatch(b *testing.B) { m := regexp.MustCompile(regexp_alternatives_combine_lite) f := []byte(fixture_alternatives_combine_lite) @@ -371,7 +469,7 @@ func BenchmarkAlternativesCombineLiteRegexp(b *testing.B) { _ = m.Match(f) } } -func BenchmarkAlternativesCombineHardRegexp(b *testing.B) { +func BenchmarkAlternativesCombineHardRegexpMatch(b *testing.B) { m := regexp.MustCompile(regexp_alternatives_combine_hard) f := []byte(fixture_alternatives_combine_hard) @@ -380,74 +478,126 @@ func BenchmarkAlternativesCombineHardRegexp(b *testing.B) { } } -func BenchmarkPlain(b *testing.B) { +func BenchmarkPlainGlobMatch(b *testing.B) { m, _ := Compile(pattern_plain) for i := 0; i < b.N; i++ { - _ = m.Match(fixture_plain) + _ = m.Match(fixture_plain_match) } } -func BenchmarkPlainRegexp(b *testing.B) { +func BenchmarkPlainRegexpMatch(b *testing.B) { m := regexp.MustCompile(regexp_plain) - f := []byte(fixture_plain) + f := []byte(fixture_plain_match) + + for i := 0; i < b.N; i++ { + _ = m.Match(f) + } +} +func BenchmarkPlainGlobMismatch(b *testing.B) { + m, _ := Compile(pattern_plain) + + for i := 0; i < b.N; i++ { + _ = m.Match(fixture_plain_mismatch) + } +} +func BenchmarkPlainRegexpMismatch(b *testing.B) { + m := regexp.MustCompile(regexp_plain) + f := []byte(fixture_plain_mismatch) for i := 0; i < b.N; i++ { _ = m.Match(f) } } -func BenchmarkPrefix(b *testing.B) { +func BenchmarkPrefixGlobMatch(b *testing.B) { m, _ := Compile(pattern_prefix) for i := 0; i < b.N; i++ { - _ = m.Match(fixture_prefix_suffix) + _ = m.Match(fixture_prefix_suffix_match) } } -func BenchmarkPrefixRegexp(b *testing.B) { +func BenchmarkPrefixRegexpMatch(b *testing.B) { m := regexp.MustCompile(regexp_prefix) - f := []byte(fixture_prefix_suffix) + f := []byte(fixture_prefix_suffix_match) + + for i := 0; i < b.N; i++ { + _ = m.Match(f) + } +} +func BenchmarkPrefixGlobMismatch(b *testing.B) { + m, _ := Compile(pattern_prefix) + + for i := 0; i < b.N; i++ { + _ = m.Match(fixture_prefix_suffix_mismatch) + } +} +func BenchmarkPrefixRegexpMismatch(b *testing.B) { + m := regexp.MustCompile(regexp_prefix) + f := []byte(fixture_prefix_suffix_mismatch) for i := 0; i < b.N; i++ { _ = m.Match(f) } } -func BenchmarkSuffix(b *testing.B) { +func BenchmarkSuffixGlobMatch(b *testing.B) { m, _ := Compile(pattern_suffix) for i := 0; i < b.N; i++ { - _ = m.Match(fixture_prefix_suffix) + _ = m.Match(fixture_prefix_suffix_match) } } -func BenchmarkSuffixRegexp(b *testing.B) { +func BenchmarkSuffixRegexpMatch(b *testing.B) { m := regexp.MustCompile(regexp_suffix) - f := []byte(fixture_prefix_suffix) + f := []byte(fixture_prefix_suffix_match) + + for i := 0; i < b.N; i++ { + _ = m.Match(f) + } +} +func BenchmarkSuffixGlobMismatch(b *testing.B) { + m, _ := Compile(pattern_suffix) + + for i := 0; i < b.N; i++ { + _ = m.Match(fixture_prefix_suffix_mismatch) + } +} +func BenchmarkSuffixRegexpMismatch(b *testing.B) { + m := regexp.MustCompile(regexp_suffix) + f := []byte(fixture_prefix_suffix_mismatch) for i := 0; i < b.N; i++ { _ = m.Match(f) } } -func BenchmarkPrefixSuffix(b *testing.B) { +func BenchmarkPrefixSuffixGlobMatch(b *testing.B) { m, _ := Compile(pattern_prefix_suffix) for i := 0; i < b.N; i++ { - _ = m.Match(fixture_prefix_suffix) + _ = m.Match(fixture_prefix_suffix_match) } } -func BenchmarkPrefixSuffixRegexp(b *testing.B) { +func BenchmarkPrefixSuffixRegexpMatch(b *testing.B) { m := regexp.MustCompile(regexp_prefix_suffix) - f := []byte(fixture_prefix_suffix) + f := []byte(fixture_prefix_suffix_match) for i := 0; i < b.N; i++ { _ = m.Match(f) } } +func BenchmarkPrefixSuffixGlobMismatch(b *testing.B) { + m, _ := Compile(pattern_prefix_suffix) -//BenchmarkParse-8 500000 2235 ns/op -//BenchmarkAll-8 20000000 73.1 ns/op -//BenchmarkMultiple-8 10000000 130 ns/op -//BenchmarkPlain-8 200000000 6.70 ns/op -//BenchmarkPrefix-8 200000000 8.36 ns/op -//BenchmarkSuffix-8 200000000 8.35 ns/op -//BenchmarkPrefixSuffix-8 100000000 13.6 ns/op + for i := 0; i < b.N; i++ { + _ = m.Match(fixture_prefix_suffix_mismatch) + } +} +func BenchmarkPrefixSuffixRegexpMismatch(b *testing.B) { + m := regexp.MustCompile(regexp_prefix_suffix) + f := []byte(fixture_prefix_suffix_mismatch) + + for i := 0; i < b.N; i++ { + _ = m.Match(f) + } +} diff --git a/readme.md b/readme.md index 0d7fe60..989d1b6 100644 --- a/readme.md +++ b/readme.md @@ -100,25 +100,39 @@ Run `go test -bench=.` from source root to see the benchmarks: Pattern | Fixture | Operations | Speed (ns/op) --------|---------|------------|-------------- -`[a-z][!a-x]*cat*[h][!b]*eyes*` | `my cat has very bright eyes` | 2000000 | 527 -`https://*.google.*` | `https://account.google.com` | 10000000 | 121 -`{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}` | `http://yahoo.com` | 10000000 | 167 -`{https://*gobwas.com,http://exclude.gobwas.com}` | `https://safe.gobwas.com` | 50000000 | 24.7 -`abc*` | `abcdef` | 200000000 | 9.49 -`*def` | `abcdef` | 200000000 | 9.60 -`ab*ef` | `abcdef` | 100000000 | 15.2 +`[a-z][!a-x]*cat*[h][!b]*eyes*` | `my cat has very bright eyes` | ✔ | 2000000 | 527 +`[a-z][!a-x]*cat*[h][!b]*eyes*` | `my dog has very bright eyes` | ✗ | 10000000 | 229 +`https://*.google.*` | `https://account.google.com` | ✔ | 10000000 | 121 +`https://*.google.*` | `https://google.com` | ✗ | 20000000 | 68.6 +`{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}` | `http://yahoo.com` | ✔ | 10000000 | 167 +`{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}` | `http://google.com` | ✗ | 10000000 | 198 +`{https://*gobwas.com,http://exclude.gobwas.com}` | `https://safe.gobwas.com` | ✔ | 100000000 | 23.9 +`{https://*gobwas.com,http://exclude.gobwas.com}` | `http://safe.gobwas.com` | ✗ | 50000000 | 24.7 +`abc*` | `abcdef` | ✔ | 200000000 | 8.86 +`abc*` | `af` | ✗ | 300000000 | 4.99 +`*def` | `abcdef` | ✔ | 200000000 | 9.23 +`*def` | `af` | ✗ | 300000000 | 5.44 +`ab*ef` | `abcdef` | ✔ | 100000000 | 15.2 +`ab*ef` | `af` | ✗ | 100000000 | 10.4 The same things with `regexp` package: Pattern | Fixture | Operations | Speed (ns/op) --------|---------|------------|-------------- -`^[a-z][^a-x].*cat.*[h][^b].*eyes.*$` | `my cat has very bright eyes` | 500000 | 2553 -`^https:\/\/.*\.google\..*$` | `https://account.google.com` | 1000000 | 1205 -`^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$` | `http://yahoo.com` | 1000000 | 1435 -`^(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)$` | `https://safe.gobwas.com` | 1000000 | 1039 -`^abc.*$` | `abcdef` | 3000000 | 275 -`^.*def$` | `abcdef` | 5000000 | 464 -`^ab.*ef$` | `abcdef` | 5000000 | 395 +`^[a-z][^a-x].*cat.*[h][^b].*eyes.*$` | `my cat has very bright eyes` | ✔ | 500000 | 2553 +`^[a-z][^a-x].*cat.*[h][^b].*eyes.*$` | `my dog has very bright eyes` | ✗ | 1000000 | 1383 +`^https:\/\/.*\.google\..*$` | `https://account.google.com` | ✔ | 1000000 | 1205 +`^https:\/\/.*\.google\..*$` | `https://google.com` | ✗ | 2000000 | 767 +`^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$` | `http://yahoo.com` | ✔ | 1000000 | 1435 +`^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$` | `http://google.com` | ✗ | 1000000 | 1674 +`^(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)$` | `https://safe.gobwas.com` | ✔ | 1000000 | 1039 +`^(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)$` | `http://safe.gobwas.com` | ✗ | 5000000 | 272 +`^abc.*$` | `abcdef` | ✔ | 5000000 | 237 +`^abc.*$` | `af` | ✗ | 20000000 | 100 +`^.*def$` | `abcdef` | ✔ | 5000000 | 464 +`^.*def$` | `af` | ✗ | 5000000 | 265 +`^ab.*ef$` | `abcdef` | ✔ | 5000000 | 375 +`^ab.*ef$` | `af` | ✗ | 10000000 | 145 [godoc-image]: https://godoc.org/github.com/gobwas/glob?status.svg [godoc-url]: https://godoc.org/github.com/gobwas/glob