From: Matt Corallo Date: Fri, 28 May 2021 00:47:11 +0000 (+0000) Subject: Add benchmark of deserializing a NetworkGraph. X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=32ad9b70aa2a10d795af1166eb4e34ec8224d722;p=rust-lightning Add benchmark of deserializing a NetworkGraph. NetworkGraph is one of the largest structures we generally deserialize, so it makes for a good benchmark, even if it isn't the most complicated one. As of this commit, on an Intel 2687W v3, these benchmarks take: test routing::network_graph::benches::read_network_graph ... bench: 2,101,420,078 ns/iter (+/- 6,649,020) test routing::network_graph::benches::write_network_graph ... bench: 344,696,835 ns/iter (+/- 229,061) --- diff --git a/lightning/src/routing/network_graph.rs b/lightning/src/routing/network_graph.rs index 4b90cad44..def433a07 100644 --- a/lightning/src/routing/network_graph.rs +++ b/lightning/src/routing/network_graph.rs @@ -2424,3 +2424,32 @@ mod tests { assert!(result.is_err()); } } + +#[cfg(all(test, feature = "unstable"))] +mod benches { + use super::*; + + use test::Bencher; + use std::io::Read; + + #[bench] + fn read_network_graph(bench: &mut Bencher) { + let mut d = ::routing::router::test_utils::get_route_file() + .expect("Please fetch https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin and place it at lightning/net_graph-2021-05-27.bin"); + let mut v = Vec::new(); + d.read_to_end(&mut v).unwrap(); + bench.iter(|| { + let _ = NetworkGraph::read(&mut std::io::Cursor::new(&v)).unwrap(); + }); + } + + #[bench] + fn write_network_graph(bench: &mut Bencher) { + let mut d = ::routing::router::test_utils::get_route_file() + .expect("Please fetch https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin and place it at lightning/net_graph-2021-05-27.bin"); + let net_graph = NetworkGraph::read(&mut d).unwrap(); + bench.iter(|| { + let _ = net_graph.encode(); + }); + } +} diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 46fe93e77..c948f0fbd 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -3856,30 +3856,6 @@ mod tests { } } - use std::fs::File; - use util::ser::Readable; - /// Tries to open a network graph file, or panics with a URL to fetch it. - pub(super) fn get_route_file() -> Result { - let res = File::open("net_graph-2021-05-27.bin") // By default we're run in RL/lightning - .or_else(|_| File::open("lightning/net_graph-2021-05-27.bin")) // We may be run manually in RL/ - .or_else(|_| { // Fall back to guessing based on the binary location - // path is likely something like .../rust-lightning/target/debug/deps/lightning-... - let mut path = std::env::current_exe().unwrap(); - path.pop(); // lightning-... - path.pop(); // deps - path.pop(); // debug - path.pop(); // target - path.push("lightning"); - path.push("net_graph-2021-05-27.bin"); - eprintln!("{}", path.to_str().unwrap()); - File::open(path) - }); - #[cfg(require_route_graph_test)] - return Ok(res.expect("Didn't have route graph and was configured to require it")); - #[cfg(not(require_route_graph_test))] - return res; - } - pub(super) fn random_init_seed() -> u64 { // Because the default HashMap in std pulls OS randomness, we can use it as a (bad) RNG. use core::hash::{BuildHasher, Hasher}; @@ -3887,10 +3863,11 @@ mod tests { println!("Using seed of {}", seed); seed } + use util::ser::Readable; #[test] fn generate_routes() { - let mut d = match get_route_file() { + let mut d = match super::test_utils::get_route_file() { Ok(f) => f, Err(_) => { eprintln!("Please fetch https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin and place it at lightning/net_graph-2021-05-27.bin"); @@ -3917,7 +3894,7 @@ mod tests { #[test] fn generate_routes_mpp() { - let mut d = match get_route_file() { + let mut d = match super::test_utils::get_route_file() { Ok(f) => f, Err(_) => { eprintln!("Please fetch https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin and place it at lightning/net_graph-2021-05-27.bin"); @@ -3943,12 +3920,38 @@ mod tests { } } +#[cfg(test)] +pub(crate) mod test_utils { + use std::fs::File; + /// Tries to open a network graph file, or panics with a URL to fetch it. + pub(crate) fn get_route_file() -> Result { + let res = File::open("net_graph-2021-05-27.bin") // By default we're run in RL/lightning + .or_else(|_| File::open("lightning/net_graph-2021-05-27.bin")) // We may be run manually in RL/ + .or_else(|_| { // Fall back to guessing based on the binary location + // path is likely something like .../rust-lightning/target/debug/deps/lightning-... + let mut path = std::env::current_exe().unwrap(); + path.pop(); // lightning-... + path.pop(); // deps + path.pop(); // debug + path.pop(); // target + path.push("lightning"); + path.push("net_graph-2021-05-27.bin"); + eprintln!("{}", path.to_str().unwrap()); + File::open(path) + }); + #[cfg(require_route_graph_test)] + return Ok(res.expect("Didn't have route graph and was configured to require it")); + #[cfg(not(require_route_graph_test))] + return res; + } + +} + #[cfg(all(test, feature = "unstable"))] mod benches { use super::*; use util::logger::{Logger, Record}; - use prelude::*; use test::Bencher; struct DummyLogger {} @@ -3958,7 +3961,7 @@ mod benches { #[bench] fn generate_routes(bench: &mut Bencher) { - let mut d = tests::get_route_file() + let mut d = test_utils::get_route_file() .expect("Please fetch https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin and place it at lightning/net_graph-2021-05-27.bin"); let graph = NetworkGraph::read(&mut d).unwrap(); @@ -3990,7 +3993,7 @@ mod benches { #[bench] fn generate_mpp_routes(bench: &mut Bencher) { - let mut d = tests::get_route_file() + let mut d = test_utils::get_route_file() .expect("Please fetch https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin and place it at lightning/net_graph-2021-05-27.bin"); let graph = NetworkGraph::read(&mut d).unwrap();