forked from mikioh/ipaddr
/
benchmark_test.go
250 lines (191 loc) · 5.17 KB
/
benchmark_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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2015 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.
package ipaddr_test
import (
"net"
"testing"
"github.com/mikioh/ipaddr"
)
func BenchmarkAggregateIPv4(b *testing.B) {
in := toPrefixes([]string{"192.168.0.0/24", "192.168.1.0/24", "192.168.2.0/24", "192.168.3.0/24", "192.168.4.0/25", "192.168.101.0/26", "192.168.102.1/27"})
for i := 0; i < b.N; i++ {
ipaddr.Aggregate(in)
}
}
func BenchmarkAggregateIPv6(b *testing.B) {
in := toPrefixes([]string{"2001:db8::/64", "2001:db8:0:1::/64", "2001:db8:0:2::/64", "2001:db8:0:3::/64", "2001:db8:0:4::/64", "2001:db8::/64", "2001:db8::1/64"})
for i := 0; i < b.N; i++ {
ipaddr.Aggregate(in)
}
}
func BenchmarkCompareIPv4(b *testing.B) {
p1 := toPrefix("192.168.1.0/24")
p2 := toPrefix("192.168.2.0/24")
for i := 0; i < b.N; i++ {
ipaddr.Compare(p1, p2)
}
}
func BenchmarkCompareIPv6(b *testing.B) {
p1 := toPrefix("2001:db8:f001:f002::/64")
p2 := toPrefix("2001:db8:f001:f003::/64")
for i := 0; i < b.N; i++ {
ipaddr.Compare(p1, p2)
}
}
func BenchmarkSummarizeIPv4(b *testing.B) {
fip, lip := net.IPv4(172, 16, 1, 1), net.IPv4(172, 16, 255, 255)
for i := 0; i < b.N; i++ {
ipaddr.Summarize(fip, lip)
}
}
func BenchmarkSummarizeIPv6(b *testing.B) {
fip, lip := net.ParseIP("2001:db8::1:1"), net.ParseIP("2001:db8::1:ffff")
for i := 0; i < b.N; i++ {
ipaddr.Summarize(fip, lip)
}
}
func BenchmarkSupernetIPv4(b *testing.B) {
in := toPrefixes([]string{"192.168.0.0/24", "192.168.1.0/24", "192.168.2.0/24", "192.168.3.0/24", "192.168.4.0/25", "192.168.101.0/26", "192.168.102.1/27"})
for i := 0; i < b.N; i++ {
ipaddr.Supernet(in)
}
}
func BenchmarkSupernetIPv6(b *testing.B) {
in := toPrefixes([]string{"2001:db8::/64", "2001:db8:0:1::/64", "2001:db8:0:2::/64", "2001:db8:0:3::/64", "2001:db8:0:4::/64", "2001:db8::/64", "2001:db8::1/64"})
for i := 0; i < b.N; i++ {
ipaddr.Supernet(in)
}
}
func BenchmarkCursorNextIPv4(b *testing.B) {
ps := toPrefixes([]string{"192.168.0.0/24"})
c := ipaddr.NewCursor(ps)
for i := 0; i < b.N; i++ {
for c.Next() != nil {
}
}
}
func BenchmarkCursorNextIPv6(b *testing.B) {
ps := toPrefixes([]string{"2001:db8::/120"})
c := ipaddr.NewCursor(ps)
for i := 0; i < b.N; i++ {
for c.Next() != nil {
}
}
}
func BenchmarkCursorPrevIPv4(b *testing.B) {
ps := toPrefixes([]string{"192.168.0.255/24"})
c := ipaddr.NewCursor(ps)
for i := 0; i < b.N; i++ {
for c.Prev() != nil {
}
}
}
func BenchmarkCursorPrevIPv6(b *testing.B) {
ps := toPrefixes([]string{"2001:db8::ff/120"})
c := ipaddr.NewCursor(ps)
for i := 0; i < b.N; i++ {
for c.Prev() != nil {
}
}
}
func BenchmarkPrefixEqualIPv4(b *testing.B) {
ps := toPrefixes([]string{"192.168.1.0/24", "192.168.2.0/24"})
for i := 0; i < b.N; i++ {
ps[0].Equal(&ps[1])
}
}
func BenchmarkPrefixEqualIPv6(b *testing.B) {
ps := toPrefixes([]string{"2001:db8:f001:f002::/64", "2001:db8:f001:f003::/64"})
for i := 0; i < b.N; i++ {
ps[0].Equal(&ps[1])
}
}
func BenchmarkPrefixExcludeIPv4(b *testing.B) {
p1 := toPrefix("10.1.0.0/16")
p2 := toPrefix("10.1.1.1/32")
for i := 0; i < b.N; i++ {
p1.Exclude(p2)
}
}
func BenchmarkPrefixExcludeIPv6(b *testing.B) {
p1 := toPrefix("2001:db8::/64")
p2 := toPrefix("2001:db8::1:1:1:1/128")
for i := 0; i < b.N; i++ {
p1.Exclude(p2)
}
}
func BenchmarkPrefixMarshalBinaryIPv4(b *testing.B) {
p := toPrefix("192.168.0.0/31")
for i := 0; i < b.N; i++ {
p.MarshalBinary()
}
}
func BenchmarkPrefixMarshalBinaryIPv6(b *testing.B) {
p := toPrefix("2001:db8:cafe:babe::/127")
for i := 0; i < b.N; i++ {
p.MarshalBinary()
}
}
func BenchmarkPrefixMarshalTextIPv4(b *testing.B) {
p := toPrefix("192.168.0.0/31")
for i := 0; i < b.N; i++ {
p.MarshalText()
}
}
func BenchmarkPrefixMarshalTextIPv6(b *testing.B) {
p := toPrefix("2001:db8:cafe:babe::/127")
for i := 0; i < b.N; i++ {
p.MarshalText()
}
}
func BenchmarkPrefixOverlapsIPv4(b *testing.B) {
p1 := toPrefix("192.168.1.0/24")
p2 := toPrefix("192.168.2.0/24")
for i := 0; i < b.N; i++ {
p1.Overlaps(p2)
}
}
func BenchmarkPrefixOverlapsIPv6(b *testing.B) {
p1 := toPrefix("2001:db8:f001:f002::/64")
p2 := toPrefix("2001:db8:f001:f003::/64")
for i := 0; i < b.N; i++ {
p1.Overlaps(p2)
}
}
func BenchmarkPrefixSubnetsIPv4(b *testing.B) {
p := toPrefix("192.168.0.0/16")
for i := 0; i < b.N; i++ {
p.Subnets(3)
}
}
func BenchmarkPrefixSubnetsIPv6(b *testing.B) {
p := toPrefix("2001:db8::/60")
for i := 0; i < b.N; i++ {
p.Subnets(3)
}
}
func BenchmarkPrefixUnmarshalBinaryIPv4(b *testing.B) {
p := toPrefix("0.0.0.0/0")
for i := 0; i < b.N; i++ {
p.UnmarshalBinary([]byte{22, 192, 168, 0})
}
}
func BenchmarkPrefixUnmarshalBinaryIPv6(b *testing.B) {
p := toPrefix("::/0")
for i := 0; i < b.N; i++ {
p.UnmarshalBinary([]byte{66, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0xca, 0xfe, 0x80})
}
}
func BenchmarkPrefixUnmarshalTextIPv4(b *testing.B) {
p := toPrefix("0.0.0.0/0")
for i := 0; i < b.N; i++ {
p.UnmarshalText([]byte("192.168.0.0/31"))
}
}
func BenchmarkPrefixUnmarshalTextIPv6(b *testing.B) {
p := toPrefix("::/0")
for i := 0; i < b.N; i++ {
p.UnmarshalText([]byte("2001:db8:cafe:babe::/127"))
}
}