github.com/TeaOSLab/EdgeNode@v1.3.8/internal/re/rune_tree_test.go (about) 1 // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package re_test 4 5 import ( 6 "github.com/TeaOSLab/EdgeNode/internal/re" 7 "github.com/iwind/TeaGo/assert" 8 "regexp" 9 "testing" 10 ) 11 12 func TestNewRuneTree(t *testing.T) { 13 var a = assert.NewAssertion(t) 14 15 var tree = re.NewRuneTree([]string{"abc", "abd", "def", "GHI", "中国", "@"}) 16 a.IsTrue(tree.Lookup("ABC", true)) 17 a.IsTrue(tree.Lookup("ABC1", true)) 18 a.IsTrue(tree.Lookup("1ABC", true)) 19 a.IsTrue(tree.Lookup("def", true)) 20 a.IsTrue(tree.Lookup("ghI", true)) 21 a.IsFalse(tree.Lookup("d ef", true)) 22 a.IsFalse(tree.Lookup("de", true)) 23 a.IsFalse(tree.Lookup("de f", true)) 24 a.IsTrue(tree.Lookup("我是中国人", true)) 25 a.IsTrue(tree.Lookup("iwind.liu@gmail.com", true)) 26 } 27 28 func TestNewRuneTree2(t *testing.T) { 29 var tree = re.NewRuneTree([]string{"abc", "abd", "def", "GHI", "中国", "@"}) 30 tree.Lookup("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36", true) 31 } 32 33 func BenchmarkRuneMap_Lookup(b *testing.B) { 34 var tree = re.NewRuneTree([]string{"abc", "abd", "def", "ghi", "中国"}) 35 for i := 0; i < b.N; i++ { 36 tree.Lookup("我来自中国", true) 37 } 38 } 39 40 func BenchmarkRuneMap_Lookup2_NOT_FOUND(b *testing.B) { 41 var tree = re.NewRuneTree([]string{"abc", "abd", "cde", "GHI"}) 42 for i := 0; i < b.N; i++ { 43 tree.Lookup("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36", true) 44 } 45 } 46 47 func BenchmarkRune_Regexp_FOUND(b *testing.B) { 48 var reg = regexp.MustCompile("(?i)abc|abd|cde|GHI") 49 for i := 0; i < b.N; i++ { 50 reg.MatchString("HELLO WORLD ABC 123 456 abc HELLO WORLD HELLO WORLD ABC 123 456 abc HELLO WORLD HELLO WORLD ABC 123 456 abc HELLO WORLD") 51 } 52 }