github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/strmatcher/benchmark_matchers_test.go (about) 1 package strmatcher_test 2 3 import ( 4 "strconv" 5 "testing" 6 7 "github.com/v2fly/v2ray-core/v5/common" 8 . "github.com/v2fly/v2ray-core/v5/common/strmatcher" 9 ) 10 11 func BenchmarkFullMatcher(b *testing.B) { 12 b.Run("SimpleMatcherGroup------", func(b *testing.B) { 13 benchmarkMatcherType(b, Full, func() MatcherGroup { 14 return new(SimpleMatcherGroup) 15 }) 16 }) 17 b.Run("FullMatcherGroup--------", func(b *testing.B) { 18 benchmarkMatcherType(b, Full, func() MatcherGroup { 19 return NewFullMatcherGroup() 20 }) 21 }) 22 b.Run("ACAutomationMatcherGroup", func(b *testing.B) { 23 benchmarkMatcherType(b, Full, func() MatcherGroup { 24 return NewACAutomatonMatcherGroup() 25 }) 26 }) 27 b.Run("MphMatcherGroup---------", func(b *testing.B) { 28 benchmarkMatcherType(b, Full, func() MatcherGroup { 29 return NewMphMatcherGroup() 30 }) 31 }) 32 } 33 34 func BenchmarkDomainMatcher(b *testing.B) { 35 b.Run("SimpleMatcherGroup------", func(b *testing.B) { 36 benchmarkMatcherType(b, Domain, func() MatcherGroup { 37 return new(SimpleMatcherGroup) 38 }) 39 }) 40 b.Run("DomainMatcherGroup------", func(b *testing.B) { 41 benchmarkMatcherType(b, Domain, func() MatcherGroup { 42 return NewDomainMatcherGroup() 43 }) 44 }) 45 b.Run("ACAutomationMatcherGroup", func(b *testing.B) { 46 benchmarkMatcherType(b, Domain, func() MatcherGroup { 47 return NewACAutomatonMatcherGroup() 48 }) 49 }) 50 b.Run("MphMatcherGroup---------", func(b *testing.B) { 51 benchmarkMatcherType(b, Domain, func() MatcherGroup { 52 return NewMphMatcherGroup() 53 }) 54 }) 55 } 56 57 func BenchmarkSubstrMatcher(b *testing.B) { 58 b.Run("SimpleMatcherGroup------", func(b *testing.B) { 59 benchmarkMatcherType(b, Substr, func() MatcherGroup { 60 return new(SimpleMatcherGroup) 61 }) 62 }) 63 b.Run("SubstrMatcherGroup------", func(b *testing.B) { 64 benchmarkMatcherType(b, Substr, func() MatcherGroup { 65 return new(SubstrMatcherGroup) 66 }) 67 }) 68 b.Run("ACAutomationMatcherGroup", func(b *testing.B) { 69 benchmarkMatcherType(b, Substr, func() MatcherGroup { 70 return NewACAutomatonMatcherGroup() 71 }) 72 }) 73 } 74 75 // Utility functions for benchmark 76 77 func benchmarkMatcherType(b *testing.B, t Type, ctor func() MatcherGroup) { 78 b.Run("Match", func(b *testing.B) { 79 b.Run("Succ", func(b *testing.B) { 80 benchmarkMatch(b, ctor(), map[Type]bool{t: true}) 81 }) 82 b.Run("Fail", func(b *testing.B) { 83 benchmarkMatch(b, ctor(), map[Type]bool{t: false}) 84 }) 85 }) 86 b.Run("MatchAny", func(b *testing.B) { 87 b.Run("Succ", func(b *testing.B) { 88 benchmarkMatchAny(b, ctor(), map[Type]bool{t: true}) 89 }) 90 b.Run("Fail", func(b *testing.B) { 91 benchmarkMatchAny(b, ctor(), map[Type]bool{t: false}) 92 }) 93 }) 94 } 95 96 func benchmarkMatch(b *testing.B, g MatcherGroup, enabledTypes map[Type]bool) { 97 prepareMatchers(g, enabledTypes) 98 b.ResetTimer() 99 for i := 0; i < b.N; i++ { 100 _ = g.Match("0.v2fly.org") 101 } 102 } 103 104 func benchmarkMatchAny(b *testing.B, g MatcherGroup, enabledTypes map[Type]bool) { 105 prepareMatchers(g, enabledTypes) 106 b.ResetTimer() 107 for i := 0; i < b.N; i++ { 108 _ = g.MatchAny("0.v2fly.org") 109 } 110 } 111 112 func prepareMatchers(g MatcherGroup, enabledTypes map[Type]bool) { 113 for matcherType, hasMatch := range enabledTypes { 114 switch matcherType { 115 case Domain: 116 if hasMatch { 117 AddMatcherToGroup(g, DomainMatcher("v2fly.org"), 0) 118 } 119 for i := 1; i < 1024; i++ { 120 AddMatcherToGroup(g, DomainMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i)) 121 } 122 case Full: 123 if hasMatch { 124 AddMatcherToGroup(g, FullMatcher("0.v2fly.org"), 0) 125 } 126 for i := 1; i < 64; i++ { 127 AddMatcherToGroup(g, FullMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i)) 128 } 129 case Substr: 130 if hasMatch { 131 AddMatcherToGroup(g, SubstrMatcher("v2fly.org"), 0) 132 } 133 for i := 1; i < 4; i++ { 134 AddMatcherToGroup(g, SubstrMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i)) 135 } 136 case Regex: 137 matcher, err := Regex.New("^[^.]*$") // Dotless domain matcher automatically inserted in DNS app when "localhost" DNS is used. 138 common.Must(err) 139 AddMatcherToGroup(g, matcher, 0) 140 } 141 } 142 if g, ok := g.(buildable); ok { 143 common.Must(g.Build()) 144 } 145 } 146 147 type buildable interface { 148 Build() error 149 }