-
Notifications
You must be signed in to change notification settings - Fork 10
/
detector_bechmark_test.go
66 lines (59 loc) · 1.27 KB
/
detector_bechmark_test.go
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
package botdetector
import (
"net/http"
"testing"
)
func BenchmarkDetector(b *testing.B) {
d, err := New(Config{
Denylist: []string{"a", "b"},
Allowlist: []string{"c", "Pingdom.com_bot_version_1.1"},
Patterns: []string{
`(Pingdom.com_bot_version_)(\d+)\.(\d+)`,
`(facebookexternalhit)/(\d+)\.(\d+)`,
},
})
if err != nil {
b.Error(err)
return
}
becnhDetection(b, d)
}
func BenchmarkLRUDetector(b *testing.B) {
d, err := New(Config{
Denylist: []string{"a", "b"},
Allowlist: []string{"c", "Pingdom.com_bot_version_1.1"},
Patterns: []string{
`(Pingdom.com_bot_version_)(\d+)\.(\d+)`,
`(facebookexternalhit)/(\d+)\.(\d+)`,
},
CacheSize: 10000,
})
if err != nil {
b.Error(err)
return
}
becnhDetection(b, d)
}
func becnhDetection(b *testing.B, f DetectorFunc) {
for _, tc := range []struct {
name string
ua string
}{
{"ok_1", "abcd"},
{"ok_2", ""},
{"ok_3", "c"},
{"ok_4", "Pingdom.com_bot_version_1.1"},
{"ko_1", "a"},
{"ko_2", "b"},
{"ko_3", "facebookexternalhit/1.1"},
{"ko_4", "Pingdom.com_bot_version_1.2"},
} {
req, _ := http.NewRequest("GET", "https://example.com", http.NoBody)
req.Header.Add("User-Agent", tc.ua)
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
f(req)
}
})
}
}