Add a benchmark for CIDR parsing

Also optimise the cidr_parts implementation
This commit is contained in:
Abhishek Chanda
2019-02-02 21:43:27 +00:00
parent aff0419a75
commit a19f24a9c6
3 changed files with 41 additions and 8 deletions

View File

@ -17,6 +17,7 @@ serde = ">=0.8.0, <2.0"
[dev-dependencies]
serde_json = "1.0"
serde_derive = ">=0.8.0, <2.0"
criterion = "0.2.9"
[badges]
travis-ci = { repository = "achanda/ipnetwork" }
@ -24,3 +25,7 @@ travis-ci = { repository = "achanda/ipnetwork" }
[features]
default = []
dev = ["clippy"]
[[bench]]
name = "parse_bench"
harness = false

21
benches/parse_bench.rs Normal file
View File

@ -0,0 +1,21 @@
#[macro_use]
extern crate criterion;
extern crate ipnetwork;
use ipnetwork::{Ipv4Network, Ipv6Network};
use criterion::Criterion;
fn parse_ipv4_benchmark(c: &mut Criterion) {
c.bench_function("parse ipv4", |b| b.iter(|| {
"127.1.0.0/24".parse::<Ipv4Network>().unwrap()
}));
}
fn parse_ipv6_benchmark(c: &mut Criterion) {
c.bench_function("parse ipv6", |b| b.iter(|| {
"FF01:0:0:17:0:0:0:2/64".parse::<Ipv6Network>().unwrap()
}));
}
criterion_group!(benches, parse_ipv4_benchmark, parse_ipv6_benchmark);
criterion_main!(benches);

View File

@ -32,16 +32,23 @@ impl Error for IpNetworkError {
}
pub fn cidr_parts(cidr: &str) -> Result<(&str, Option<&str>), IpNetworkError> {
let parts = cidr.split('/').collect::<Vec<&str>>();
if parts.len() == 1 {
Ok((parts[0], None))
} else if parts.len() == 2 {
Ok((parts[0], Some(parts[1])))
} else {
Err(IpNetworkError::InvalidCidrFormat(format!(
// Try to find a single slash
if let Some(sep) = cidr.find('/') {
let (ip, prefix) = cidr.split_at(sep);
// Error if cidr has multiple slashes
if prefix[1..].find('/').is_some() {
return Err(IpNetworkError::InvalidCidrFormat(format!(
"CIDR must contain a single '/': {}",
cidr
)))
)));
}
else {
// Handle the case when cidr has exactly one slash
return Ok((ip, Some(&prefix[1..])));
}
} else {
// Handle the case when cidr does not have a slash
return Ok((cidr, None));
}
}