-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
166 lines (144 loc) · 3.92 KB
/
benchmark_test.go
File metadata and controls
166 lines (144 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package knownbots
import (
"os"
"path/filepath"
"testing"
)
func init() {
EnableLog = false
}
func setupBenchValidator(b *testing.B, botCount int) *Validator {
tmpDir := b.TempDir()
confDir := filepath.Join(tmpDir, "conf.d")
if err := os.MkdirAll(confDir, 0755); err != nil {
b.Fatal(err)
}
botNames := []string{
"TestBot1", "TestBot2", "TestBot3", "TestBot4", "TestBot5",
"TestBot6", "TestBot7", "TestBot8", "TestBot9", "TestBot10",
"TestBot11", "TestBot12", "TestBot13", "TestBot14", "TestBot15",
"TestBot16", "TestBot17", "TestBot18", "TestBot19", "TestBot20",
"TestBot21", "TestBot22", "TestBot23", "TestBot24", "TestBot25",
"TestBot26", "TestBot27", "TestBot28", "TestBot29", "TestBot30",
"TestBot31", "TestBot32", "TestBot33", "TestBot34", "TestBot35",
"TestBot36", "TestBot37", "TestBot38", "TestBot39", "TestBot40",
}
for i := 0; i < botCount && i < len(botNames); i++ {
botName := botNames[i]
content := "name: " + botName + "\nua: \"" + botName + "\"\ncustom:\n - \"192.168.1.0/24\"\ndomains:\n - \"example.com\"\n"
path := filepath.Join(confDir, botName+".yaml")
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
b.Fatal(err)
}
}
v, err := New(WithRoot(tmpDir))
if err != nil {
b.Fatal(err)
}
return v
}
func BenchmarkFindBotByUA_Hit_First(b *testing.B) {
v := setupBenchValidator(b, 40)
defer v.Close()
ua := "Mozilla/5.0 (compatible; TestBot1/2.1; +http://example.com/bot.html)"
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if v.findBotByUA(ua) == nil {
b.Fatal("expected to find Googlebot")
}
}
})
}
func BenchmarkFindBotByUA_Hit_Middle(b *testing.B) {
v := setupBenchValidator(b, 40)
defer v.Close()
ua := "Mozilla/5.0 (compatible; TestBot2/1.0)"
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if v.findBotByUA(ua) == nil {
b.Fatal("expected to find DuckDuckBot")
}
}
})
}
func BenchmarkFindBotByUA_Hit_Last(b *testing.B) {
v := setupBenchValidator(b, 40)
defer v.Close()
ua := "Mozilla/5.0 (compatible; TestBot40/1.0)"
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if v.findBotByUA(ua) == nil {
b.Fatal("expected to find SiteAuditBot")
}
}
})
}
func BenchmarkFindBotByUA_Miss(b *testing.B) {
v := setupBenchValidator(b, 40)
defer v.Close()
ua := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if v.findBotByUA(ua) != nil {
b.Fatal("expected not to find any bot")
}
}
})
}
func BenchmarkFindBotByUA_CaseSensitive(b *testing.B) {
v := setupBenchValidator(b, 40)
defer v.Close()
ua := "Mozilla/5.0 (compatible; TestBot1/2.1)"
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if v.findBotByUA(ua) == nil {
b.Fatal("expected to find Googlebot")
}
}
})
}
func BenchmarkValidate_KnownBot_IPHit(b *testing.B) {
v := setupBenchValidator(b, 40)
defer v.Close()
ua := "Mozilla/5.0 (compatible; TestBot1/2.1)"
ip := "192.168.1.100"
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
result := v.Validate(ua, ip)
if result.Status != StatusVerified {
b.Fatalf("expected verified, got %d", result.Status)
}
}
})
}
func BenchmarkValidate_Browser(b *testing.B) {
v := setupBenchValidator(b, 40)
defer v.Close()
ua := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
ip := "192.168.1.1"
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
result := v.Validate(ua, ip)
if result.IsBot {
b.Fatal("expected IsBot=false for browser")
}
}
})
}
func BenchmarkContainsWord(b *testing.B) {
text := "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
word := "Googlebot"
b.ResetTimer()
for i := 0; i < b.N; i++ {
if !containsWord(text, word) {
b.Fatal("expected match")
}
}
}