-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
77 lines (70 loc) · 1.75 KB
/
Copy pathbenchmark_test.go
File metadata and controls
77 lines (70 loc) · 1.75 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
package ipfilter
import (
"crypto/rand"
"net/netip"
"testing"
)
func BenchmarkWhitelist_ipv4(b *testing.B) {
wl := NewWhitelist()
wl.AllowPrefix(randomIPv4(10000)...)
wl.AllowPrefix(randomIPv6(10000)...)
wl.Allow("255.255.255.255/32")
b.ResetTimer()
for b.Loop() {
_ = wl.Allowed("255.255.255.255") // Always allowed
}
}
func BenchmarkBlacklist_ipv4(b *testing.B) {
wl := NewBlacklist()
wl.DisallowPrefix(randomIPv4(10000)...)
wl.DisallowPrefix(randomIPv6(10000)...)
wl.Disallow("255.255.255.255/32")
b.ResetTimer()
for b.Loop() {
_ = wl.Allowed("255.255.255.255") // Always disallowed
}
}
func BenchmarkWhitelist_ipv6(b *testing.B) {
wl := NewWhitelist()
wl.AllowPrefix(randomIPv4(10000)...)
wl.AllowPrefix(randomIPv6(10000)...)
wl.Allow("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")
b.ResetTimer()
for b.Loop() {
_ = wl.Allowed("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") // Always allowed
}
}
func BenchmarkBlacklist_ipv6(b *testing.B) {
wl := NewBlacklist()
wl.DisallowPrefix(randomIPv4(10000)...)
wl.DisallowPrefix(randomIPv6(10000)...)
wl.Disallow("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")
b.ResetTimer()
for b.Loop() {
_ = wl.Allowed("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") // Always disallowed
}
}
func randomIPv4(n int) []netip.Prefix {
addrs := make([]netip.Prefix, n)
for i := range n {
var b [4]byte
rand.Read(b[:])
ip := netip.AddrFrom4(b)
bits := 32 // 28+mrand.IntN(5)
prefix := netip.PrefixFrom(ip, bits)
addrs[i] = prefix
}
return addrs
}
func randomIPv6(n int) []netip.Prefix {
addrs := make([]netip.Prefix, n)
for i := range n {
var b [16]byte
rand.Read(b[:])
ip := netip.AddrFrom16(b)
bits := 128 // 120+mrand.IntN(9)
prefix := netip.PrefixFrom(ip, bits)
addrs[i] = prefix
}
return addrs
}