github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/domainset/matcher_domain_test.go (about) 1 package domainset 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 const testMissDomain = "aur.archlinux.org" 9 10 var testDomains = [...]string{ 11 "example.com", 12 "github.com", 13 "cube64128.xyz", 14 "www.cube64128.xyz", 15 "api.ipify.org", 16 "api6.ipify.org", 17 "archlinux.org", 18 "aur.archlinux.org", 19 "wiki.archlinux.org", 20 "dash.cloudflare.com", 21 "api.cloudflare.com", 22 "google.com", 23 "www.google.com", 24 "youtube.com", 25 "www.youtube.com", 26 "localdomain", 27 } 28 29 func testDomainMatcher(t *testing.T, m Matcher) { 30 testMatcher(t, m, "com", false) 31 testMatcher(t, m, "example.com", true) 32 testMatcher(t, m, "www.example.com", false) 33 testMatcher(t, m, "gobyexample.com", false) 34 testMatcher(t, m, "example.org", false) 35 testMatcher(t, m, "github.com", true) 36 testMatcher(t, m, "api.github.com", false) 37 testMatcher(t, m, "raw.githubusercontent.com", false) 38 testMatcher(t, m, "github.blog", false) 39 testMatcher(t, m, "cube64128.xyz", true) 40 testMatcher(t, m, "www.cube64128.xyz", true) 41 testMatcher(t, m, "nonexistent.cube64128.xyz", false) 42 testMatcher(t, m, "notcube64128.xyz", false) 43 testMatcher(t, m, "org", false) 44 testMatcher(t, m, "ipify.org", false) 45 testMatcher(t, m, "api.ipify.org", true) 46 testMatcher(t, m, "api6.ipify.org", true) 47 testMatcher(t, m, "api64.ipify.org", false) 48 testMatcher(t, m, "www.ipify.org", false) 49 testMatcher(t, m, "archlinux.org", true) 50 testMatcher(t, m, "aur.archlinux.org", true) 51 testMatcher(t, m, "bugs.archlinux.org", false) 52 testMatcher(t, m, "wiki.archlinux.org", true) 53 testMatcher(t, m, "cloudflare", false) 54 testMatcher(t, m, "cloudflare.com", false) 55 testMatcher(t, m, "dash.cloudflare.com", true) 56 testMatcher(t, m, "api.cloudflare.com", true) 57 testMatcher(t, m, "google.com", true) 58 testMatcher(t, m, "www.google.com", true) 59 testMatcher(t, m, "amervice.google.com", false) 60 testMatcher(t, m, "youtube.com", true) 61 testMatcher(t, m, "www.youtube.com", true) 62 testMatcher(t, m, "m.youtube.com", false) 63 testMatcher(t, m, "localdomain", true) 64 testMatcher(t, m, "www.localdomain", false) 65 } 66 67 func TestDomainLinearMatcher(t *testing.T) { 68 dlm := DomainLinearMatcher(testDomains[:]) 69 testDomainMatcher(t, &dlm) 70 } 71 72 func TestDomainBinarySearchMatcher(t *testing.T) { 73 dbsm := DomainBinarySearchMatcherFromSlice(testDomains[:]) 74 testDomainMatcher(t, &dbsm) 75 } 76 77 func TestDomainMapMatcher(t *testing.T) { 78 dmm := DomainMapMatcherFromSlice(testDomains[:]) 79 testDomainMatcher(t, &dmm) 80 } 81 82 func benchmarkDomainMatcher(b *testing.B, count int, name string, m Matcher) { 83 b.Run(fmt.Sprintf("%d/%s/Hit", count, name), func(b *testing.B) { 84 for i := range b.N { 85 m.Match(testDomains[i%count]) 86 } 87 }) 88 b.Run(fmt.Sprintf("%d/%s/Miss", count, name), func(b *testing.B) { 89 for range b.N { 90 m.Match(testMissDomain) 91 } 92 }) 93 } 94 95 func BenchmarkDomainMatchers(b *testing.B) { 96 for i := len(testDomains) / 4; i <= len(testDomains); i += 3 { 97 dlm := DomainLinearMatcher(testDomains[:i]) 98 dbsm := DomainBinarySearchMatcherFromSlice(testDomains[:i]) 99 dmm := DomainMapMatcherFromSlice(testDomains[:i]) 100 benchmarkDomainMatcher(b, i, "DomainLinearMatcher", &dlm) 101 benchmarkDomainMatcher(b, i, "DomainBinarySearchMatcher", &dbsm) 102 benchmarkDomainMatcher(b, i, "DomainMapMatcher", &dmm) 103 } 104 }