move identity generation benchmark to a criterion benchmark

`cargo install criterion` for some additional tools to work with this
benchmark, or use `cargo bench` to run it.

Signed-off-by: Erik Hollensbe <git@hollensbe.org>
This commit is contained in:
Erik Hollensbe 2022-06-27 05:33:52 -07:00
parent 6b0e44b2d7
commit 9373116ad7
No known key found for this signature in database
GPG key ID: 4BB0E241A863B389
3 changed files with 18 additions and 22 deletions

View file

@ -28,9 +28,14 @@ serde = { version = "^1", features = ["derive"], default-features = false }
rand = "*" rand = "*"
serde_json = "*" serde_json = "*"
serde_cbor = "*" serde_cbor = "*"
criterion = "0.3"
[target."cfg(not(windows))".dependencies] [target."cfg(not(windows))".dependencies]
libc = "^0" libc = "^0"
[target."cfg(windows)".dependencies] [target."cfg(windows)".dependencies]
winapi = { version = "^0", features = ["ws2tcpip"] } winapi = { version = "^0", features = ["ws2tcpip"] }
[[bench]]
name = "benchmark_identity"
harness = false

View file

@ -0,0 +1,13 @@
use criterion::{criterion_group, criterion_main, Criterion};
use std::time::Duration;
use zerotier_network_hypervisor::vl1::Identity;
pub fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("basic");
group.measurement_time(Duration::new(30, 0));
group.bench_function("identity generation", |b| b.iter(|| Identity::generate()));
group.finish();
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

View file

@ -861,8 +861,6 @@ mod tests {
use crate::util::marshalable::Marshalable; use crate::util::marshalable::Marshalable;
use crate::vl1::identity::*; use crate::vl1::identity::*;
use std::str::FromStr; use std::str::FromStr;
use std::time::{Duration, SystemTime};
#[allow(unused_imports)]
use zerotier_core_crypto::hex; use zerotier_core_crypto::hex;
#[test] #[test]
@ -974,24 +972,4 @@ mod tests {
assert!(Identity::from_str(ids.as_str()).unwrap() == id); assert!(Identity::from_str(ids.as_str()).unwrap() == id);
} }
} }
#[test]
fn benchmark_generate() {
let mut count = 0;
let run_time = Duration::from_secs(5);
let start = SystemTime::now();
let mut end;
let mut duration;
loop {
let _id = Identity::generate();
//println!("{}", _id.to_string());
end = SystemTime::now();
duration = end.duration_since(start).unwrap();
count += 1;
if duration >= run_time {
break;
}
}
println!("benchmark: V1 identity generation: {} ms / identity (average)", (duration.as_millis() as f64) / (count as f64));
}
} }