From: Jeffrey Czyz Date: Fri, 14 Jan 2022 18:28:30 +0000 (-0600) Subject: Remove duplicate generate_routes benchmark code X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=de6497ee44e35ed9b8869aa0246f3af8e7f87b2c;p=rust-lightning Remove duplicate generate_routes benchmark code Refactor generate_routes and generate_mpp_routes into a single utility for benchmarking. The utility is parameterized with features in order to test both single path and multi-path routing. Additionally, it is parameterized with a Score to be used with other scorers. --- diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 1f6638dae..d5f83faf3 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -4963,6 +4963,8 @@ pub(crate) mod test_utils { #[cfg(all(test, feature = "unstable", not(feature = "no-std")))] mod benches { use super::*; + use bitcoin::secp256k1::PublicKey; + use ln::features::InvoiceFeatures; use routing::scoring::Scorer; use util::logger::{Logger, Record}; @@ -4973,47 +4975,29 @@ mod benches { fn log(&self, _record: &Record) {} } - #[bench] - fn generate_routes(bench: &mut Bencher) { + fn read_network_graph() -> NetworkGraph { let mut d = test_utils::get_route_file().unwrap(); - let graph = NetworkGraph::read(&mut d).unwrap(); - let nodes = graph.read_only().nodes().clone(); - let scorer = Scorer::with_fixed_penalty(0); - - // First, get 100 (source, destination) pairs for which route-getting actually succeeds... - let mut path_endpoints = Vec::new(); - let mut seed: usize = 0xdeadbeef; - 'load_endpoints: for _ in 0..100 { - loop { - seed *= 0xdeadbeef; - let src = PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap(); - seed *= 0xdeadbeef; - let dst = PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap(); - let payee = Payee::from_node_id(dst); - let amt = seed as u64 % 1_000_000; - if get_route(&src, &payee, &graph, None, amt, 42, &DummyLogger{}, &scorer).is_ok() { - path_endpoints.push((src, dst, amt)); - continue 'load_endpoints; - } - } - } + NetworkGraph::read(&mut d).unwrap() + } - // ...then benchmark finding paths between the nodes we learned. - let mut idx = 0; - bench.iter(|| { - let (src, dst, amt) = path_endpoints[idx % path_endpoints.len()]; - let payee = Payee::from_node_id(dst); - assert!(get_route(&src, &payee, &graph, None, amt, 42, &DummyLogger{}, &scorer).is_ok()); - idx += 1; - }); + #[bench] + fn generate_routes_with_default_scorer(bench: &mut Bencher) { + let network_graph = read_network_graph(); + let scorer = Scorer::default(); + generate_routes(bench, &network_graph, scorer, InvoiceFeatures::empty()); } #[bench] - fn generate_mpp_routes(bench: &mut Bencher) { - let mut d = test_utils::get_route_file().unwrap(); - let graph = NetworkGraph::read(&mut d).unwrap(); + fn generate_mpp_routes_with_default_scorer(bench: &mut Bencher) { + let network_graph = read_network_graph(); + let scorer = Scorer::default(); + generate_routes(bench, &network_graph, scorer, InvoiceFeatures::known()); + } + + fn generate_routes( + bench: &mut Bencher, graph: &NetworkGraph, scorer: S, features: InvoiceFeatures + ) { let nodes = graph.read_only().nodes().clone(); - let scorer = Scorer::with_fixed_penalty(0); // First, get 100 (source, destination) pairs for which route-getting actually succeeds... let mut path_endpoints = Vec::new(); @@ -5024,7 +5008,7 @@ mod benches { let src = PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap(); seed *= 0xdeadbeef; let dst = PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap(); - let payee = Payee::from_node_id(dst).with_features(InvoiceFeatures::known()); + let payee = Payee::from_node_id(dst).with_features(features.clone()); let amt = seed as u64 % 1_000_000; if get_route(&src, &payee, &graph, None, amt, 42, &DummyLogger{}, &scorer).is_ok() { path_endpoints.push((src, dst, amt)); @@ -5037,7 +5021,7 @@ mod benches { let mut idx = 0; bench.iter(|| { let (src, dst, amt) = path_endpoints[idx % path_endpoints.len()]; - let payee = Payee::from_node_id(dst).with_features(InvoiceFeatures::known()); + let payee = Payee::from_node_id(dst).with_features(features.clone()); assert!(get_route(&src, &payee, &graph, None, amt, 42, &DummyLogger{}, &scorer).is_ok()); idx += 1; });