Repository navigation
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
311 lines (263 loc) · 7.62 KB
/
Copy pathbenchmark_test.go
File metadata and controls
311 lines (263 loc) · 7.62 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Benchmarks for the package's wire codecs and session layers, organized
// like fuzz_test.go: whole messages, attributes, the marshal paths, the
// framed read path, and full-table ingest at the Peer and FSM layers. Most
// are driven by the route collector corpus; see corpus_test.go.
package bgp
import (
"context"
"net/netip"
"sync/atomic"
"testing"
"time"
)
// BenchmarkParseMessage sweeps the raw parse path over every message of an
// entire route collector updates file: the initial convergence hot path.
func BenchmarkParseMessage(b *testing.B) {
msgs := corpusMessages(b)
var total int64
for _, m := range msgs {
total += int64(len(m))
}
b.SetBytes(total)
b.ReportAllocs()
for b.Loop() {
for _, m := range msgs {
if _, err := ParseMessage(m); err != nil {
b.Fatalf("failed to parse message: %v", err)
}
}
}
}
// BenchmarkParseUpdate parses one representative eBGP UPDATE: a long
// AS_PATH, communities, and an MP_REACH_NLRI, the shape a transit feed is
// mostly made of. It measures the UPDATE body parser alone, including the
// RFC 7606 validation every received UPDATE pays for.
func BenchmarkParseUpdate(b *testing.B) {
u := &Update{
Attributes: mustAttributes(
b,
OriginIGP,
ASPath{{ASNs: []uint32{
64512, 64513, 64514, 64515, 64516,
64517, 64518, 64519, 64520, 64521,
4242423610, 65536, 65537, 65538, 65539,
}}},
MED(100),
Communities{
NewCommunity(64512, 100),
NewCommunity(64512, 200),
NewCommunity(64513, 300),
NewCommunity(65535, 65281),
},
LargeCommunities{{
Global: 4242423610,
Local1: 1,
Local2: 2,
}},
MPReachNLRI{
Family: Family{
AFI: AFIIPv6,
SAFI: SAFIUnicast,
},
NextHop: netip.MustParseAddr("2001:db8::1"),
LinkLocal: netip.MustParseAddr("fe80::1"),
NLRI: Prefixes{
netip.MustParsePrefix("2001:db8:1::/48"),
netip.MustParsePrefix("2001:db8:2::/48"),
netip.MustParsePrefix("2001:db8:3::/48"),
},
},
),
}
wire, err := u.AppendBinary(nil)
if err != nil {
b.Fatalf("failed to marshal UPDATE: %v", err)
}
// The body alone: parseUpdate's input is the message minus its header.
body := wire[headerLen:]
b.SetBytes(int64(len(body)))
b.ReportAllocs()
for b.Loop() {
if _, _, err := parseUpdate(body, nil); err != nil {
b.Fatalf("failed to parse UPDATE: %v", err)
}
}
}
// BenchmarkRawAttributeParse sweeps the opt-in typed attribute parse path
// over every path attribute in the corpus.
func BenchmarkRawAttributeParse(b *testing.B) {
// Gather every attribute which parses in typed form, dropping the
// unknown types which would only measure error construction.
var attrs []RawAttribute
for _, m := range corpusMessages(b) {
r, err := ParseMessage(m)
if err != nil {
b.Fatalf("failed to parse message: %v", err)
}
u, ok := r.Message.(*Update)
if !ok {
continue
}
for _, a := range u.Attributes {
if _, err := a.Parse(); err == nil {
attrs = append(attrs, a)
}
}
}
b.ReportAllocs()
for b.Loop() {
for _, a := range attrs {
if _, err := a.Parse(); err != nil {
b.Fatalf("failed to parse attribute: %v", err)
}
}
}
}
// BenchmarkUpdateAppendBinary marshals a full eBGP UPDATE into a recycled
// buffer, the send path equivalent of buffer reuse in Conn.
func BenchmarkUpdateAppendBinary(b *testing.B) {
u := &Update{
Attributes: mustAttributes(
b,
OriginIGP,
ASPath{{ASNs: []uint32{64496, 65536, 64497}}},
NextHop(netip.MustParseAddr("192.0.2.1")),
MED(100),
Communities{NewCommunity(64496, 100), NewCommunity(64496, 200)},
),
NLRI: []netip.Prefix{
netip.MustParsePrefix("203.0.113.0/24"),
netip.MustParsePrefix("192.0.2.128/25"),
netip.MustParsePrefix("198.51.100.0/24"),
},
}
b.ReportAllocs()
var buf []byte
for b.Loop() {
var err error
if buf, err = u.AppendBinary(buf[:0]); err != nil {
b.Fatalf("failed to marshal UPDATE: %v", err)
}
}
}
// BenchmarkCorpusAppendBinary re-marshals every parsed corpus message into a
// recycled buffer: the send path measured against real attribute mixes, a
// full-table push in miniature.
func BenchmarkCorpusAppendBinary(b *testing.B) {
var (
msgs []Message
total int64
)
for _, raw := range corpusMessages(b) {
r, err := ParseMessage(raw)
if err != nil {
b.Fatalf("failed to parse corpus message: %v", err)
}
// The parsed message references the read buffer; clone so the
// benchmark holds the whole corpus at once.
msgs = append(msgs, detachMessage(b, r.Message))
total += int64(len(raw))
}
b.SetBytes(total)
b.ReportAllocs()
var buf []byte
for b.Loop() {
for _, m := range msgs {
var err error
if buf, err = m.AppendBinary(buf[:0]); err != nil {
b.Fatalf("failed to marshal corpus message: %v", err)
}
}
}
}
// BenchmarkConnReadMessage frames and parses an entire route collector
// updates file through a Conn: the receive path of initial convergence. The
// underlying connection replays the stream from memory, so the benchmark
// measures framing and parsing rather than the kernel.
func BenchmarkConnReadMessage(b *testing.B) {
msgs := corpusMessages(b)
var stream []byte
for _, m := range msgs {
stream = append(stream, m...)
}
b.SetBytes(int64(len(stream)))
b.ReportAllocs()
c := NewConn(&replayConn{b: stream})
for b.Loop() {
for range msgs {
if _, err := c.ReadMessage(); err != nil {
b.Fatalf("failed to read message: %v", err)
}
}
}
}
// BenchmarkPeerFullTableIngest replays a full internet table through a live
// Peer session: one wire UPDATE per unique IPv4 route of a fetched RIB dump,
// streamed through an in-memory connection into an OnUpdate handler. One
// iteration ingests the whole table, so ns/op is the convergence time of a
// full-table push at the mainstream Peer layer, deep copies included.
func BenchmarkPeerFullTableIngest(b *testing.B) {
wire, routes := tableReplay(b)
var remaining atomic.Int64
doneC := make(chan struct{}, 1)
r := newPipeRig(b, PeerConfig{
OnUpdate: func(_ context.Context, _ *Peer, u *Update, _ *UpdateDiagnostics) error {
if remaining.Add(-int64(len(u.NLRI))) == 0 {
doneC <- struct{}{}
}
return nil
},
})
s := r.acceptScript()
s.establish(&Open{
ASN: 64497,
HoldTime: 90 * time.Second,
ID: MustParseIdentifier("192.0.2.2"),
})
recv(b, r.estC, "session establishment")
b.SetBytes(int64(len(wire)))
b.ReportAllocs()
for b.Loop() {
remaining.Store(int64(routes))
if _, err := s.nc.Write(wire); err != nil {
b.Fatalf("failed to write table: %v", err)
}
<-doneC
}
b.ReportMetric(float64(routes), "routes/op")
}
// BenchmarkFSMFullTableIngest is BenchmarkPeerFullTableIngest one layer
// down: the same replay through a bare FSM, whose zero-copy handlers borrow
// the read buffer instead of receiving deep copies. The difference between
// the two benchmarks is the price of the Peer layer's ownership contract.
func BenchmarkFSMFullTableIngest(b *testing.B) {
wire, routes := tableReplay(b)
var remaining atomic.Int64
doneC := make(chan struct{}, 1)
r := newFSMRig(b, FSMConfig{
OnUpdate: func(_ context.Context, _ *FSM, u *Update, _ *UpdateDiagnostics) error {
if remaining.Add(-int64(len(u.NLRI))) == 0 {
doneC <- struct{}{}
}
return nil
},
})
defer r.cancel()
s := r.nextDial()
s.establish(&Open{
ASN: 64497,
HoldTime: 90 * time.Second,
ID: MustParseIdentifier("192.0.2.2"),
})
recv(b, r.estC, "session establishment")
b.SetBytes(int64(len(wire)))
b.ReportAllocs()
for b.Loop() {
remaining.Store(int64(routes))
if _, err := s.nc.Write(wire); err != nil {
b.Fatalf("failed to write table: %v", err)
}
<-doneC
}
b.ReportMetric(float64(routes), "routes/op")
}