Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance Improvements #125

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
084f1cd
Defer errors that only occur during initialization to user
jmeggitt Sep 6, 2023
d7a9b74
Update RecordIterator and ElemIterator to propogate errors to the user
jmeggitt Sep 6, 2023
71f6f2a
Replace ParserError::Unsupported with UnsupportedMrtType and Unsuppor…
jmeggitt Sep 6, 2023
4fad940
Replace IoNotEnoughBytes and TrunatedError with TruncatedField
jmeggitt Sep 8, 2023
721b50a
Add new benchmark based on MRT type
jmeggitt Sep 9, 2023
8217a73
Replace ParseError variant of ParserError
jmeggitt Sep 9, 2023
b35bdf2
Add integration test to check no errors occur while parsing
jmeggitt Sep 9, 2023
35bee4f
Error if attribute does not consume full attribute length
jmeggitt Sep 9, 2023
fea6797
Convert some log warnings to errors
jmeggitt Sep 9, 2023
62f2e40
Remove BgpModelsError
jmeggitt Sep 9, 2023
3cb0d7f
Refactor ParserBmpError to use thiserror
jmeggitt Sep 9, 2023
c052183
Refactor ParserRisliveError to use thiserror
jmeggitt Sep 9, 2023
a4c8147
Replace Bytes with &[u8]
jmeggitt Sep 9, 2023
4eb74b3
Update tests to use &[u8] instead of bytes
jmeggitt Sep 9, 2023
f8638ea
Merge branch 'new-benchmarks' of github.com:jmeggitt/bgpkit-parser in…
jmeggitt Sep 9, 2023
f0b67e9
Fix issue preventing mrt_type benchmark from compiling
jmeggitt Sep 9, 2023
00ac20b
Reorganize aspath module
jmeggitt Sep 9, 2023
5bb1bb6
Allow for borrowed AsPathSegments with Cow
jmeggitt Sep 9, 2023
ba4cf75
Switch to using new storage
jmeggitt Sep 9, 2023
7da91a1
Implement AsPathBuilder
jmeggitt Sep 9, 2023
d0782fe
Use smallvec optimization for AttributeValue variants
jmeggitt Sep 9, 2023
020af58
Store buffer for parsing records in iterator
jmeggitt Sep 9, 2023
c1abd23
Stop creating temporary Vec for prefix when parsing RIB AFI entries
jmeggitt Sep 9, 2023
7df8719
Add optimizations to NLRI reading from old branch
jmeggitt Sep 11, 2023
ca924b8
Remove unused functions from utils and clean up parse_attributes
jmeggitt Sep 11, 2023
7cc9db7
Merge branch 'main' of github.com:jmeggitt/bgpkit-parser into as-path…
jmeggitt Sep 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ serde={version="1.0", features=["derive"], optional = true}
itertools = {version = "0.11.0", optional = true}
ipnet = {version="2.7", optional = true}
bitflags = {version="2.3.3", features = ["serde"], optional = true}
thiserror = {version = "1.0.44", optional = true}
smallvec = {version = "1.11.0", features = ["union"], optional = true}

#######################
# Parser dependencies #
#######################
bytes = {version = "1.4.0", optional = true}
hex= {version = "0.4.3", optional = true} # bmp/openbmp parsing
log= {version = "0.4", optional = true }
oneio = {version= "0.11.0", features=["lib"], optional = true }
Expand All @@ -54,9 +55,10 @@ models = [
"ipnet",
"itertools",
"bitflags",
"thiserror",
"smallvec",
]
parser = [
"bytes",
"chrono",
"env_logger",
"log",
Expand All @@ -80,16 +82,25 @@ rislive = [
serde = [
"dep:serde",
"ipnet/serde",
"smallvec/serde",
]

[[bench]]
name = "internals"
harness = false

[[bench]]
name = "mrt_type"
harness = false

[[bench]]
name = "bench_main"
harness = false

# Enable debug symbols for benchmarks for easier profiling
[profile.bench]
debug = true

[dev-dependencies]
anyhow = "1"
bgpkit-broker = "0.6.2"
Expand Down
114 changes: 114 additions & 0 deletions benches/mrt_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use bgpkit_parser::models::EntryType;
use bgpkit_parser::mrt_record::parse_common_header;
use bgpkit_parser::BgpkitParser;
use bzip2::bufread::BzDecoder;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use flate2::bufread::GzDecoder;
use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};

mod data_source;

const RECORDS_PER_TYPE: usize = 100;

/// Choose a mix of records with a given MRT type and subtype. The records are chosen to get a
/// uniform distribution of different length records.
fn select_mrt_records<R: Read>(mut input_reader: R, mrt_type: EntryType, subtype: u16) -> Vec<u8> {
let mut included = Vec::new();
let mut buffer = Vec::with_capacity(4096);

while let Ok(header) = parse_common_header(&mut input_reader) {
buffer.clear();
header
.write_header(&mut buffer)
.expect("able to write header to vec");
(&mut input_reader)
.take(header.length as u64)
.read_to_end(&mut buffer)
.expect("able to read message body");

if header.entry_type == mrt_type && header.entry_subtype == subtype {
included.push(std::mem::replace(&mut buffer, Vec::with_capacity(4096)));
}
}

included.sort_by_key(Vec::len);

if included.is_empty() {
println!("No records found for MRT {:?} {:?}", mrt_type, subtype);
return Vec::new();
}

let mut record_output = Vec::new();
for n in 0..RECORDS_PER_TYPE {
let index = (n * included.len()) / RECORDS_PER_TYPE;
record_output.extend_from_slice(&included[index][..]);
}

record_output
}

pub fn criterion_benchmark(c: &mut Criterion) {
let update_data = data_source::test_data_file("update-example.gz");
let rib_data = data_source::test_data_file("rib-example-small.bz2");

let updates_reader = BufReader::new(File::open(update_data).unwrap());
let mut rib_reader = BufReader::new(File::open(rib_data).unwrap());

println!("Decompressing input data and loading into memory...");
let bgp4mp_updates = select_mrt_records(GzDecoder::new(updates_reader), EntryType::BGP4MP, 4);
let rib_ipv4_unicast =
select_mrt_records(BzDecoder::new(&mut rib_reader), EntryType::TABLE_DUMP_V2, 2);
rib_reader.seek(SeekFrom::Start(0)).unwrap();
let rib_ipv6_unicast =
select_mrt_records(BzDecoder::new(&mut rib_reader), EntryType::TABLE_DUMP_V2, 4);

c.bench_function("BGP4MP Update", |b| {
b.iter_with_large_drop(|| {
let mut reader = black_box(&bgp4mp_updates[..]);
let mut holder: [Option<_>; RECORDS_PER_TYPE] = std::array::from_fn(|_| None);

BgpkitParser::from_reader(&mut reader)
.into_record_iter()
.enumerate()
.for_each(|(index, x)| holder[index] = Some(x));

holder
})
});

c.bench_function("TABLE_DUMP_V2 IPv4 Unicast", |b| {
b.iter_with_large_drop(|| {
let mut reader = black_box(&rib_ipv4_unicast[..]);
let mut holder: [Option<_>; RECORDS_PER_TYPE] = std::array::from_fn(|_| None);

BgpkitParser::from_reader(&mut reader)
.into_record_iter()
.enumerate()
.for_each(|(index, x)| holder[index] = Some(x));

holder
})
});

c.bench_function("TABLE_DUMP_V2 IPv6 Unicast", |b| {
b.iter_with_large_drop(|| {
let mut reader = black_box(&rib_ipv6_unicast[..]);
let mut holder: [Option<_>; RECORDS_PER_TYPE] = std::array::from_fn(|_| None);

BgpkitParser::from_reader(&mut reader)
.into_record_iter()
.enumerate()
.for_each(|(index, x)| holder[index] = Some(x));

holder
})
});
}

criterion_group! {
name = benches;
config = Criterion::default();
targets = criterion_benchmark
}
criterion_main!(benches);
5 changes: 4 additions & 1 deletion examples/cache_reading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ fn main() {
let parser =
BgpkitParser::new_cached(item.url.as_str(), "/tmp/bgpkit-cache-example/").unwrap();
// iterating through the parser. the iterator returns `BgpElem` one at a time.
let elems = parser.into_elem_iter().collect::<Vec<BgpElem>>();
let elems = parser
.into_elem_iter()
.filter_map(Result::ok)
.collect::<Vec<BgpElem>>();
log::info!("{} {} {}", item.collector_id, item.url, elems.len());
}
}
1 change: 1 addition & 0 deletions examples/deprecated_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() {
)
.unwrap()
{
let elem = elem.unwrap();
if elem.deprecated.is_some() {
println!(
"{}",
Expand Down
1 change: 1 addition & 0 deletions examples/display_elems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ fn main() {
let url = "http://archive.routeviews.org/bgpdata/\
2021.10/UPDATES/updates.20211001.0000.bz2";
for elem in BgpkitParser::new(url).unwrap() {
let elem = elem.unwrap();
println!(
"{:?}|{:?}|{:?}|{:?}|{:?}",
elem.elem_type, elem.timestamp, elem.prefix, elem.as_path, elem.next_hop,
Expand Down
1 change: 1 addition & 0 deletions examples/extended_communities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn main() {
log::info!("parsing updates file");
// iterating through the parser. the iterator returns `BgpElem` one at a time.
for elem in parser {
let elem = elem.unwrap();
if let Some(cs) = &elem.communities {
for c in cs {
match c {
Expand Down
6 changes: 3 additions & 3 deletions examples/filters.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bgpkit_parser::filter::Filter;
use bgpkit_parser::BgpkitParser;

/// This example shows how to parse a MRT file and filter by prefix.
Expand Down Expand Up @@ -27,13 +28,12 @@ fn main() {
"http://archive.routeviews.org/bgpdata/2021.10/UPDATES/updates.20211001.0000.bz2",
)
.unwrap()
.add_filter("prefix", "211.98.251.0/24")
.unwrap();
.add_filter(Filter::prefix("211.98.251.0/24").unwrap());

log::info!("parsing updates file");
// iterating through the parser. the iterator returns `BgpElem` one at a time.
for elem in parser {
log::info!("{}", &elem);
log::info!("{}", elem.unwrap());
}
log::info!("done");
}
1 change: 1 addition & 0 deletions examples/find_as_set_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() {
let collector = item.collector_id.clone();
let mut origins: HashSet<Asn> = HashSet::new();
for elem in parser {
let elem = elem.unwrap();
if !elem.elem_type.is_announce() {
continue;
}
Expand Down
1 change: 1 addition & 0 deletions examples/only-to-customer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fn main() {
)
.unwrap()
{
let elem = elem.unwrap();
if let Some(otc) = elem.only_to_customer {
println!(
"OTC found: {} for path {}\n{}\n",
Expand Down
1 change: 1 addition & 0 deletions examples/parse-files-from-broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() {
// iterating through the parser. the iterator returns `BgpElem` one at a time.
let elems = parser
.into_elem_iter()
.map(Result::unwrap)
.filter_map(|elem| {
if let Some(origins) = &elem.origin_asns {
if origins.contains(&13335.into()) {
Expand Down
2 changes: 1 addition & 1 deletion examples/parse-single-file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
log::info!("parsing updates file");
// iterating through the parser. the iterator returns `BgpElem` one at a time.
for elem in parser {
log::info!("{}", &elem);
log::info!("{}", elem.unwrap());
}
log::info!("done");
}
2 changes: 1 addition & 1 deletion examples/peer_index_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ fn main() {
let url = "https://data.ris.ripe.net/rrc03/2021.11/bview.20211128.1600.gz";
let parser = bgpkit_parser::BgpkitParser::new(url).unwrap();
for record in parser.into_record_iter().take(1) {
println!("{}", to_string_pretty(&json!(record)).unwrap());
println!("{}", to_string_pretty(&json!(record.unwrap())).unwrap());
}
}
3 changes: 1 addition & 2 deletions examples/real-time-routeviews-kafka-openbmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate core;
use bgpkit_parser::parser::bmp::messages::MessageBody;
use bgpkit_parser::Elementor;
pub use bgpkit_parser::{parse_bmp_msg, parse_openbmp_header};
use bytes::Bytes;
use kafka::consumer::{Consumer, FetchOffset, GroupOffsetStorage};
use kafka::error::Error as KafkaError;
use log::{error, info};
Expand All @@ -30,7 +29,7 @@ fn consume_and_print(group: String, topic: String, brokers: Vec<String>) -> Resu

for ms in mss.iter() {
for m in ms.messages() {
let mut bytes = Bytes::from(m.value.to_vec());
let mut bytes = m.value;
let header = parse_openbmp_header(&mut bytes).unwrap();
let bmp_msg = parse_bmp_msg(&mut bytes);
match bmp_msg {
Expand Down
2 changes: 1 addition & 1 deletion examples/records_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
let url = "http://archive.routeviews.org/route-views.amsix/bgpdata/2023.02/UPDATES/updates.20230222.0430.bz2";
let parser = BgpkitParser::new(url).unwrap();
for record in parser.into_record_iter() {
match record.message {
match record.unwrap().message {
MrtMessage::TableDumpMessage(_) => {}
MrtMessage::TableDumpV2Message(_) => {}
MrtMessage::Bgp4Mp(msg) => match msg {
Expand Down
Loading
Loading