mirror of https://github.com/gobwas/glob.git
test with regexp
This commit is contained in:
parent
b3c56781e2
commit
ec7fba7e40
117
glob_test.go
117
glob_test.go
|
@ -5,36 +5,47 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gobwas/glob/match"
|
"github.com/gobwas/glob/match"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
pattern_all = "[a-z][!a-x]*cat*[h][!b]*eyes*"
|
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"
|
fixture_all = "my cat has very bright eyes"
|
||||||
|
|
||||||
pattern_plain = "google.com"
|
pattern_plain = "google.com"
|
||||||
|
regexp_plain = `google\.com`
|
||||||
fixture_plain = "google.com"
|
fixture_plain = "google.com"
|
||||||
|
|
||||||
pattern_multiple = "https://*.google.*"
|
pattern_multiple = "https://*.google.*"
|
||||||
|
regexp_multiple = `https:\/\/.*\.google\..*`
|
||||||
fixture_multiple = "https://account.google.com"
|
fixture_multiple = "https://account.google.com"
|
||||||
|
|
||||||
pattern_alternatives = "{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}"
|
pattern_alternatives = "{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}"
|
||||||
|
regexp_alternatives = `(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)`
|
||||||
fixture_alternatives = "http://yahoo.com"
|
fixture_alternatives = "http://yahoo.com"
|
||||||
|
|
||||||
pattern_alternatives_suffix = "{https://*gobwas.com,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 = "https://safe.gobwas.com"
|
fixture_alternatives_suffix_first = "https://safe.gobwas.com"
|
||||||
fixture_alternatives_suffix_second = "http://exclude.gobwas.com"
|
fixture_alternatives_suffix_second = "http://exclude.gobwas.com"
|
||||||
|
|
||||||
pattern_prefix = "abc*"
|
pattern_prefix = "abc*"
|
||||||
|
regexp_prefix = `abc.*`
|
||||||
pattern_suffix = "*def"
|
pattern_suffix = "*def"
|
||||||
|
regexp_suffix = `.*def`
|
||||||
pattern_prefix_suffix = "ab*ef"
|
pattern_prefix_suffix = "ab*ef"
|
||||||
|
regexp_prefix_suffix = `ab.*ef`
|
||||||
fixture_prefix_suffix = "abcdef"
|
fixture_prefix_suffix = "abcdef"
|
||||||
|
|
||||||
pattern_alternatives_combine_lite = "{abc*def,abc?def,abc[zte]def}"
|
pattern_alternatives_combine_lite = "{abc*def,abc?def,abc[zte]def}"
|
||||||
|
regexp_alternatives_combine_lite = `(abc.*def|abc.def|abc[zte]def)`
|
||||||
fixture_alternatives_combine_lite = "abczdef"
|
fixture_alternatives_combine_lite = "abczdef"
|
||||||
|
|
||||||
pattern_alternatives_combine_hard = "{abc*[a-c]def,abc?[d-g]def,abc[zte]?def}"
|
pattern_alternatives_combine_hard = "{abc*[a-c]def,abc?[d-g]def,abc[zte]?def}"
|
||||||
|
regexp_alternatives_combine_hard = `(abc.*[a-c]def|abc.[d-g]def|abc[zte].def)`
|
||||||
fixture_alternatives_combine_hard = "abczqdef"
|
fixture_alternatives_combine_hard = "abczqdef"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -100,6 +111,9 @@ func DrawPatterns(t *testing.T) {
|
||||||
pattern string
|
pattern string
|
||||||
sep string
|
sep string
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
pattern: pattern_all,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
pattern: pattern_alternatives_suffix,
|
pattern: pattern_alternatives_suffix,
|
||||||
sep: separators,
|
sep: separators,
|
||||||
|
@ -110,6 +124,9 @@ func DrawPatterns(t *testing.T) {
|
||||||
{
|
{
|
||||||
pattern: pattern_alternatives_combine_hard,
|
pattern: pattern_alternatives_combine_hard,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
pattern: "{https://*.mail.ru,*my.mail.ru,*my.myalpha*.i.mail.ru}",
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
glob, err := Compile(test.pattern, test.sep)
|
glob, err := Compile(test.pattern, test.sep)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -249,16 +266,27 @@ func BenchmarkParse(b *testing.B) {
|
||||||
Compile(pattern_all)
|
Compile(pattern_all)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func BenchmarkParseRegexp(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
regexp.MustCompile(regexp_all)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkAll(b *testing.B) {
|
func BenchmarkAll(b *testing.B) {
|
||||||
m, _ := Compile(pattern_all)
|
m, _ := Compile(pattern_all)
|
||||||
// fmt.Println("tree all:")
|
|
||||||
// fmt.Println(m)
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
_ = m.Match(fixture_all)
|
_ = m.Match(fixture_all)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func BenchmarkAllRegexp(b *testing.B) {
|
||||||
|
m := regexp.MustCompile(regexp_all)
|
||||||
|
f := []byte(fixture_all)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = m.Match(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkMultiple(b *testing.B) {
|
func BenchmarkMultiple(b *testing.B) {
|
||||||
m, _ := Compile(pattern_multiple)
|
m, _ := Compile(pattern_multiple)
|
||||||
|
@ -267,6 +295,15 @@ func BenchmarkMultiple(b *testing.B) {
|
||||||
_ = m.Match(fixture_multiple)
|
_ = m.Match(fixture_multiple)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func BenchmarkMultipleRegexp(b *testing.B) {
|
||||||
|
m := regexp.MustCompile(regexp_multiple)
|
||||||
|
f := []byte(fixture_multiple)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = m.Match(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkAlternatives(b *testing.B) {
|
func BenchmarkAlternatives(b *testing.B) {
|
||||||
m, _ := Compile(pattern_alternatives)
|
m, _ := Compile(pattern_alternatives)
|
||||||
|
|
||||||
|
@ -302,6 +339,47 @@ func BenchmarkAlternativesCombineHard(b *testing.B) {
|
||||||
_ = m.Match(fixture_alternatives_combine_hard)
|
_ = 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) {
|
||||||
|
m := regexp.MustCompile(regexp_alternatives_suffix)
|
||||||
|
f := []byte(fixture_alternatives_suffix_first)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = m.Match(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func BenchmarkAlternativesSuffixSecondRegexp(b *testing.B) {
|
||||||
|
m := regexp.MustCompile(regexp_alternatives_suffix)
|
||||||
|
f := []byte(fixture_alternatives_suffix_second)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = m.Match(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func BenchmarkAlternativesCombineLiteRegexp(b *testing.B) {
|
||||||
|
m := regexp.MustCompile(regexp_alternatives_combine_lite)
|
||||||
|
f := []byte(fixture_alternatives_combine_lite)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = m.Match(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func BenchmarkAlternativesCombineHardRegexp(b *testing.B) {
|
||||||
|
m := regexp.MustCompile(regexp_alternatives_combine_hard)
|
||||||
|
f := []byte(fixture_alternatives_combine_hard)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = m.Match(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkPlain(b *testing.B) {
|
func BenchmarkPlain(b *testing.B) {
|
||||||
m, _ := Compile(pattern_plain)
|
m, _ := Compile(pattern_plain)
|
||||||
|
|
||||||
|
@ -309,6 +387,15 @@ func BenchmarkPlain(b *testing.B) {
|
||||||
_ = m.Match(fixture_plain)
|
_ = m.Match(fixture_plain)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func BenchmarkPlainRegexp(b *testing.B) {
|
||||||
|
m := regexp.MustCompile(regexp_plain)
|
||||||
|
f := []byte(fixture_plain)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = m.Match(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkPrefix(b *testing.B) {
|
func BenchmarkPrefix(b *testing.B) {
|
||||||
m, _ := Compile(pattern_prefix)
|
m, _ := Compile(pattern_prefix)
|
||||||
|
|
||||||
|
@ -316,6 +403,15 @@ func BenchmarkPrefix(b *testing.B) {
|
||||||
_ = m.Match(fixture_prefix_suffix)
|
_ = m.Match(fixture_prefix_suffix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func BenchmarkPrefixRegexp(b *testing.B) {
|
||||||
|
m := regexp.MustCompile(regexp_prefix)
|
||||||
|
f := []byte(fixture_prefix_suffix)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = m.Match(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkSuffix(b *testing.B) {
|
func BenchmarkSuffix(b *testing.B) {
|
||||||
m, _ := Compile(pattern_suffix)
|
m, _ := Compile(pattern_suffix)
|
||||||
|
|
||||||
|
@ -323,6 +419,15 @@ func BenchmarkSuffix(b *testing.B) {
|
||||||
_ = m.Match(fixture_prefix_suffix)
|
_ = m.Match(fixture_prefix_suffix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func BenchmarkSuffixRegexp(b *testing.B) {
|
||||||
|
m := regexp.MustCompile(regexp_suffix)
|
||||||
|
f := []byte(fixture_prefix_suffix)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = m.Match(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkPrefixSuffix(b *testing.B) {
|
func BenchmarkPrefixSuffix(b *testing.B) {
|
||||||
m, _ := Compile(pattern_prefix_suffix)
|
m, _ := Compile(pattern_prefix_suffix)
|
||||||
|
|
||||||
|
@ -330,6 +435,14 @@ func BenchmarkPrefixSuffix(b *testing.B) {
|
||||||
_ = m.Match(fixture_prefix_suffix)
|
_ = m.Match(fixture_prefix_suffix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func BenchmarkPrefixSuffixRegexp(b *testing.B) {
|
||||||
|
m := regexp.MustCompile(regexp_prefix_suffix)
|
||||||
|
f := []byte(fixture_prefix_suffix)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = m.Match(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//BenchmarkParse-8 500000 2235 ns/op
|
//BenchmarkParse-8 500000 2235 ns/op
|
||||||
//BenchmarkAll-8 20000000 73.1 ns/op
|
//BenchmarkAll-8 20000000 73.1 ns/op
|
||||||
|
|
15
readme.md
15
readme.md
|
@ -100,8 +100,7 @@ Run `go test -bench=.` from source root to see the benchmarks:
|
||||||
|
|
||||||
Pattern | Fixture | Operations | Speed (ns/op)
|
Pattern | Fixture | Operations | Speed (ns/op)
|
||||||
--------|---------|------------|--------------
|
--------|---------|------------|--------------
|
||||||
`[a-z][!a-x]*cat*[h][!b]*eyes*` | - (parsing) | 50000 | 26497
|
`[a-z][!a-x]*cat*[h][!b]*eyes*` | `my cat has very bright eyes` | 2000000 | 549
|
||||||
`[a-z][!a-x]*cat*[h][!b]*eyes*` | `my cat has very bright eyes` | 2000000 | 615
|
|
||||||
`https://*.google.*` | `https://account.google.com` | 10000000 | 121
|
`https://*.google.*` | `https://account.google.com` | 10000000 | 121
|
||||||
`{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}` | `http://yahoo.com` | 10000000 | 167
|
`{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
|
`{https://*gobwas.com,http://exclude.gobwas.com}` | `https://safe.gobwas.com` | 50000000 | 24.7
|
||||||
|
@ -109,6 +108,18 @@ Pattern | Fixture | Operations | Speed (ns/op)
|
||||||
`*def` | `abcdef` | 200000000 | 9.60
|
`*def` | `abcdef` | 200000000 | 9.60
|
||||||
`ab*ef` | `abcdef` | 100000000 | 15.2
|
`ab*ef` | `abcdef` | 100000000 | 15.2
|
||||||
|
|
||||||
|
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 | 2762
|
||||||
|
`https:\/\/.*\.google\..*` | `https://account.google.com` | 1000000 | 1191
|
||||||
|
`(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)` | `http://yahoo.com` | 1000000 | 1444
|
||||||
|
`(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)` | `https://safe.gobwas.com` | 1000000 | 1037
|
||||||
|
`abc.*` | `abcdef` | 3000000 | 414
|
||||||
|
`.*def` | `abcdef` | 5000000 | 276
|
||||||
|
`ab.*ef` | `abcdef` | 5000000 | 352
|
||||||
|
|
||||||
[godoc-image]: https://godoc.org/github.com/gobwas/glob?status.svg
|
[godoc-image]: https://godoc.org/github.com/gobwas/glob?status.svg
|
||||||
[godoc-url]: https://godoc.org/github.com/gobwas/glob
|
[godoc-url]: https://godoc.org/github.com/gobwas/glob
|
||||||
[travis-image]: https://travis-ci.org/gobwas/glob.svg?branch=master
|
[travis-image]: https://travis-ci.org/gobwas/glob.svg?branch=master
|
||||||
|
|
Loading…
Reference in New Issue