File: parse_bench.rs

package info (click to toggle)
rust-ipnetwork 0.21.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 240 kB
  • sloc: makefile: 2
file content (49 lines) | stat: -rw-r--r-- 1,436 bytes parent folder | download | duplicates (2)
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
use criterion::{criterion_group, criterion_main, Criterion};
use ipnetwork::{Ipv4Network, Ipv6Network};
use std::net::{Ipv4Addr, Ipv6Addr};

fn parse_ipv4_prefix_benchmark(c: &mut Criterion) {
    c.bench_function("parse ipv4 prefix", |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())
    });
}

fn parse_ipv4_netmask_benchmark(c: &mut Criterion) {
    c.bench_function("parse ipv4 netmask", |b| {
        b.iter(|| "127.1.0.0/255.255.255.0".parse::<Ipv4Network>().unwrap())
    });
}

fn contains_ipv4_benchmark(c: &mut Criterion) {
    let cidr = "74.125.227.0/25".parse::<Ipv4Network>().unwrap();
    c.bench_function("contains ipv4", |b| {
        b.iter(|| {
            cidr.contains(Ipv4Addr::new(74, 125, 227, 4))
        })
    });
}

fn contains_ipv6_benchmark(c: &mut Criterion) {
    let cidr = "FF01:0:0:17:0:0:0:2/65".parse::<Ipv6Network>().unwrap();
    c.bench_function("contains ipv6", |b| {
        b.iter(|| {
            cidr.contains(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0x7fff, 0, 0, 0x2))
        })
    });
}

criterion_group!(
    benches,
    parse_ipv4_prefix_benchmark,
    parse_ipv6_benchmark,
    parse_ipv4_netmask_benchmark,
    contains_ipv4_benchmark,
    contains_ipv6_benchmark
);
criterion_main!(benches);