-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathmultiaddr_usage.rs
More file actions
50 lines (44 loc) · 1.44 KB
/
multiaddr_usage.rs
File metadata and controls
50 lines (44 loc) · 1.44 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
use std::net::{Ipv4Addr, Ipv6Addr};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use multiaddr::multiaddr;
use rand::{distributions::Standard, Rng};
fn make_ipv4_tcp_multiaddrs(addr: &[Ipv4Addr]) {
let mut result = Vec::with_capacity(addr.len() * 2);
for addr in addr {
result.push(multiaddr!(Ip4(*addr), Tcp(22u16)))
}
for i in 0..result.len() {
result.push(result[i].clone())
}
black_box(result);
}
fn make_ipv6_tcp_multiaddrs(addr: &[Ipv6Addr]) {
let mut result = Vec::with_capacity(addr.len() * 2);
for addr in addr {
result.push(multiaddr!(Ip6(*addr), Tcp(22u16)))
}
for i in 0..result.len() {
result.push(result[i].clone())
}
black_box(result);
}
fn criterion_benchmark(c: &mut Criterion) {
let random_ipv4: Vec<Ipv4Addr> = rand::thread_rng()
.sample_iter(Standard)
.take(4096)
.map(|x: [u8; 4]| x.into())
.collect();
c.bench_function("4096 ipv4 tcp multiaddrs", |b| {
b.iter(|| make_ipv4_tcp_multiaddrs(black_box(&random_ipv4)))
});
let random_ipv6: Vec<Ipv6Addr> = rand::thread_rng()
.sample_iter(Standard)
.take(4096)
.map(|x: [u8; 16]| x.into())
.collect();
c.bench_function("4096 ipv6 tcp multiaddrs", |b| {
b.iter(|| make_ipv6_tcp_multiaddrs(black_box(&random_ipv6)))
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);