benchmark_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package strmatcher_test
  2. import (
  3. "strconv"
  4. "testing"
  5. "github.com/v2fly/v2ray-core/v5/common"
  6. . "github.com/v2fly/v2ray-core/v5/common/strmatcher"
  7. )
  8. // Benchmark Domain Matcher Groups
  9. func BenchmarkSimpleMatcherGroupForDomain(b *testing.B) {
  10. g := new(SimpleMatcherGroup)
  11. for i := 1; i <= 1024; i++ {
  12. AddMatcherToGroup(g, DomainMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i))
  13. }
  14. b.ResetTimer()
  15. for i := 0; i < b.N; i++ {
  16. _ = g.Match("0.v2fly.org")
  17. }
  18. }
  19. func BenchmarkDomainMatcherGroup(b *testing.B) {
  20. g := new(DomainMatcherGroup)
  21. for i := 1; i <= 1024; i++ {
  22. AddMatcherToGroup(g, DomainMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i))
  23. }
  24. b.ResetTimer()
  25. for i := 0; i < b.N; i++ {
  26. _ = g.Match("0.v2fly.org")
  27. }
  28. }
  29. func BenchmarkACAutomatonMatcherGroupForDomain(b *testing.B) {
  30. ac := NewACAutomatonMatcherGroup()
  31. for i := 1; i <= 1024; i++ {
  32. AddMatcherToGroup(ac, DomainMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i))
  33. }
  34. ac.Build()
  35. b.ResetTimer()
  36. for i := 0; i < b.N; i++ {
  37. _ = ac.MatchAny("0.v2fly.org")
  38. }
  39. }
  40. func BenchmarkMphMatcherGroupForDomain(b *testing.B) {
  41. mph := NewMphMatcherGroup()
  42. for i := 1; i <= 1024; i++ {
  43. AddMatcherToGroup(mph, DomainMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i))
  44. }
  45. mph.Build()
  46. b.ResetTimer()
  47. for i := 0; i < b.N; i++ {
  48. _ = mph.MatchAny("0.v2fly.org")
  49. }
  50. }
  51. // Benchmark Full Matcher Groups
  52. func BenchmarkSimpleMatcherGroupForFull(b *testing.B) {
  53. g := new(SimpleMatcherGroup)
  54. for i := 1; i <= 1024; i++ {
  55. AddMatcherToGroup(g, FullMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i))
  56. }
  57. b.ResetTimer()
  58. for i := 0; i < b.N; i++ {
  59. _ = g.Match("0.v2fly.org")
  60. }
  61. }
  62. func BenchmarkFullMatcherGroup(b *testing.B) {
  63. g := new(FullMatcherGroup)
  64. for i := 1; i <= 1024; i++ {
  65. AddMatcherToGroup(g, FullMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i))
  66. }
  67. b.ResetTimer()
  68. for i := 0; i < b.N; i++ {
  69. _ = g.Match("0.v2fly.org")
  70. }
  71. }
  72. func BenchmarkACAutomatonMatcherGroupForFull(b *testing.B) {
  73. ac := NewACAutomatonMatcherGroup()
  74. for i := 1; i <= 1024; i++ {
  75. AddMatcherToGroup(ac, FullMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i))
  76. }
  77. ac.Build()
  78. b.ResetTimer()
  79. for i := 0; i < b.N; i++ {
  80. _ = ac.MatchAny("0.v2fly.org")
  81. }
  82. }
  83. func BenchmarkMphMatcherGroupFull(b *testing.B) {
  84. mph := NewMphMatcherGroup()
  85. for i := 1; i <= 1024; i++ {
  86. AddMatcherToGroup(mph, FullMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i))
  87. }
  88. mph.Build()
  89. b.ResetTimer()
  90. for i := 0; i < b.N; i++ {
  91. _ = mph.MatchAny("0.v2fly.org")
  92. }
  93. }
  94. // Benchmark Substr Matcher Groups
  95. func BenchmarkSimpleMatcherGroupForSubstr(b *testing.B) {
  96. g := new(SimpleMatcherGroup)
  97. for i := 1; i <= 1024; i++ {
  98. AddMatcherToGroup(g, SubstrMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i))
  99. }
  100. b.ResetTimer()
  101. for i := 0; i < b.N; i++ {
  102. _ = g.Match("0.v2fly.org")
  103. }
  104. }
  105. func BenchmarkACAutomatonMatcherGroupForSubstr(b *testing.B) {
  106. ac := NewACAutomatonMatcherGroup()
  107. for i := 1; i <= 1024; i++ {
  108. AddMatcherToGroup(ac, SubstrMatcher(strconv.Itoa(i)+".v2fly.org"), uint32(i))
  109. }
  110. ac.Build()
  111. b.ResetTimer()
  112. for i := 0; i < b.N; i++ {
  113. _ = ac.MatchAny("0.v2fly.org")
  114. }
  115. }
  116. // Benchmark Index Matchers
  117. func BenchmarkLinearIndexMatcher(b *testing.B) {
  118. g := new(LinearIndexMatcher)
  119. for i := 1; i <= 1024; i++ {
  120. m, err := Domain.New(strconv.Itoa(i) + ".v2fly.org")
  121. common.Must(err)
  122. g.Add(m)
  123. }
  124. b.ResetTimer()
  125. for i := 0; i < b.N; i++ {
  126. _ = g.Match("0.v2fly.org")
  127. }
  128. }