-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy_protocol_bench_test.go
97 lines (90 loc) · 3.08 KB
/
proxy_protocol_bench_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
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
package proxyprotocol
import "testing"
func BenchmarkHeaderV1Parser(b *testing.B) {
cc := &proxyProtocolConn{
Conn: nil,
}
tests := [][]byte{
[]byte("PROXY TCP4 192.168.1.100 192.168.1.50 5678 3306\r\nOther Data"),
[]byte("PROXY TCP 192.168.1.100 192.168.1.50 5678 3306 3307\r\n"),
[]byte("PROXY TCP6 2001:0db8:85a3:0000:0000:8a2e:0370:7334 2001:0db8:85a3:0000:0000:8a2e:0390:7334 5678 3306\r\n"),
[]byte("PROXY UNKNOWN\r\n"),
}
nt := len(tests)
for i := 0; i < b.N; i++ {
cc.extractClientIPV1(tests[i%nt], nil)
}
}
func BenchmarkHeaderV2Parser(b *testing.B) {
cc := &proxyProtocolConn{
Conn: nil,
}
tests := [][]byte{
encodeProxyProtocolV2Header("tcp4", "192.168.1.100:5678", "192.168.1.5:4000"),
encodeProxyProtocolV2Header("tcp6", "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:5678", "[2001:db8:85a3::8a2e:370:8000]:4000"),
}
nt := len(tests)
for i := 0; i < b.N; i++ {
cc.extraceClientIPV2(tests[i%nt], nil)
}
}
func BenchmarkProtocolVersionTest(b *testing.B) {
tests := [][]byte{
[]byte("PROXY TCP4 192.168.1.100 192.168.1.50 5678 3306\r\nOther Data"),
[]byte("PROXY TCP6 2001:0db8:85a3:0000:0000:8a2e:0370:7334 2001:0db8:85a3:0000:0000:8a2e:0390:7334 5678 3306\r\n"),
encodeProxyProtocolV2Header("tcp4", "192.168.1.100:5678", "192.168.1.5:4000"),
encodeProxyProtocolV2Header("tcp6", "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:5678", "[2001:db8:85a3::8a2e:370:8000]:4000"),
}
nt := len(tests)
for i := 0; i < b.N; i++ {
data := tests[i%nt]
cc := &proxyProtocolConn{
Conn: newMockBufferConnBytes(data, nil),
}
cc.readHeader()
}
}
func BenchmarkParseHeaderMix(b *testing.B) {
tests := [][]byte{
[]byte("PROXY TCP4 192.168.1.100 192.168.1.50 5678 3306\r\nOther Data"),
[]byte("PROXY TCP6 2001:0db8:85a3:0000:0000:8a2e:0370:7334 2001:0db8:85a3:0000:0000:8a2e:0390:7334 5678 3306\r\n"),
encodeProxyProtocolV2Header("tcp4", "192.168.1.100:5678", "192.168.1.5:4000"),
encodeProxyProtocolV2Header("tcp6", "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:5678", "[2001:db8:85a3::8a2e:370:8000]:4000"),
}
nt := len(tests)
for i := 0; i < b.N; i++ {
data := tests[i%nt]
cc := &proxyProtocolConn{
Conn: newMockBufferConnBytes(data, nil),
}
cc.readClientAddrBehindProxy(nil)
}
}
func BenchmarkParseHeaderV1(b *testing.B) {
tests := [][]byte{
[]byte("PROXY TCP4 192.168.1.100 192.168.1.50 5678 3306\r\nOther Data"),
[]byte("PROXY TCP6 2001:0db8:85a3:0000:0000:8a2e:0370:7334 2001:0db8:85a3:0000:0000:8a2e:0390:7334 5678 3306\r\n"),
}
nt := len(tests)
for i := 0; i < b.N; i++ {
data := tests[i%nt]
cc := &proxyProtocolConn{
Conn: newMockBufferConnBytes(data, nil),
}
cc.readClientAddrBehindProxy(nil)
}
}
func BenchmarkParseHeaderV2(b *testing.B) {
tests := [][]byte{
encodeProxyProtocolV2Header("tcp4", "192.168.1.100:5678", "192.168.1.5:4000"),
encodeProxyProtocolV2Header("tcp6", "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:5678", "[2001:db8:85a3::8a2e:370:8000]:4000"),
}
nt := len(tests)
for i := 0; i < b.N; i++ {
data := tests[i%nt]
cc := &proxyProtocolConn{
Conn: newMockBufferConnBytes(data, nil),
}
cc.readClientAddrBehindProxy(nil)
}
}