-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
167 lines (146 loc) · 3.25 KB
/
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
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
package cidrtree_test
import (
"bufio"
"compress/gzip"
"fmt"
"log"
mrand "math/rand"
"net/netip"
"os"
"strings"
"testing"
"github.com/gaissmai/cidrtree"
)
var intMap = map[int]string{
1: "1",
10: "10",
100: "100",
1_000: "1_000",
10_000: "10_000",
100_000: "100_000",
}
func BenchmarkLookup(b *testing.B) {
for k := 1; k <= 100_000; k *= 10 {
rt := new(cidrtree.Table[any])
cidrs := shuffleFullTable(k)
for _, cidr := range cidrs {
rt.Insert(cidr, nil)
}
probe := cidrs[mrand.Intn(k)]
ip := probe.Addr()
name := fmt.Sprintf("In%10s", intMap[k])
b.ResetTimer()
b.Run(name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _, _ = rt.Lookup(ip)
}
})
}
}
func BenchmarkLookupPrefix(b *testing.B) {
for k := 1; k <= 100_000; k *= 10 {
rt := new(cidrtree.Table[any])
cidrs := shuffleFullTable(k)
for _, cidr := range cidrs {
rt.Insert(cidr, nil)
}
probe := cidrs[mrand.Intn(k)]
name := fmt.Sprintf("In%10s", intMap[k])
b.ResetTimer()
b.Run(name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _, _ = rt.LookupPrefix(probe)
}
})
}
}
func BenchmarkClone(b *testing.B) {
for k := 1; k <= 100_000; k *= 10 {
rt := new(cidrtree.Table[any])
for _, cidr := range shuffleFullTable(k) {
rt.Insert(cidr, nil)
}
name := fmt.Sprintf("%10s", intMap[k])
b.ResetTimer()
b.Run(name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = rt.Clone()
}
})
}
}
func BenchmarkInsert(b *testing.B) {
for k := 1; k <= 100_000; k *= 10 {
rt := new(cidrtree.Table[any])
cidrs := shuffleFullTable(k)
for _, cidr := range cidrs {
rt.Insert(cidr, nil)
}
cidr := routes[mrand.Intn(len(routes))].cidr
name := fmt.Sprintf("Into%10s", intMap[k])
b.ResetTimer()
b.Run(name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
rt.Insert(cidr, nil)
}
})
}
}
func BenchmarkDelete(b *testing.B) {
for k := 1; k <= 100_000; k *= 10 {
rt := new(cidrtree.Table[any])
cidrs := shuffleFullTable(k)
for _, cidr := range cidrs {
rt.Insert(cidr, nil)
}
probe := routes[mrand.Intn(len(routes))]
name := fmt.Sprintf("From%10s", intMap[k])
b.ResetTimer()
b.Run(name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = rt.Delete(probe.cidr)
}
})
}
}
// #####################################################
// helpers
// #####################################################
// full internet prefix list, gzipped
var (
prefixFile = "testdata/prefixes.txt.gz"
fullTable = loadFullTable()
)
func shuffleFullTable(n int) []netip.Prefix {
if n > len(fullTable) {
panic("n too big")
}
var clone []netip.Prefix
clone = append(clone, fullTable...)
mrand.Shuffle(len(clone), func(i, j int) {
clone[i], clone[j] = clone[j], clone[i]
})
return clone[:n]
}
func loadFullTable() []netip.Prefix {
var routes []netip.Prefix
file, err := os.Open(prefixFile)
if err != nil {
log.Fatal(err)
}
rgz, err := gzip.NewReader(file)
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(rgz)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
cidr := netip.MustParsePrefix(line)
routes = append(routes, cidr)
}
if err := scanner.Err(); err != nil {
log.Printf("reading from %v, %v", rgz, err)
}
return routes
}