]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add a trivial benchmark of calculating routes on today's graph
authorMatt Corallo <git@bluematt.me>
Mon, 15 Feb 2021 21:49:02 +0000 (16:49 -0500)
committerMatt Corallo <git@bluematt.me>
Mon, 15 Feb 2021 21:51:51 +0000 (16:51 -0500)
Sadly rust upstream never really figured out the benchmark story,
and it looks like the API we use here may not be long for this
world. Luckily, we can switch to criterion with largely the same
API if that happens before upstream finishes ongoing work with the
custom test framework stuff.

Sadly, it requires fetching the current network graph, which I did
using Val's route-testing script written to test the MPP router.

lightning/Cargo.toml
lightning/src/lib.rs
lightning/src/routing/router.rs

index 016399f882f438a0fd56eaa095c44dccb0d75847..883aee4a1fc6a0970699a7423855ff981559f7ac 100644 (file)
@@ -23,6 +23,7 @@ max_level_debug = []
 # Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
 # This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
 unsafe_revoked_tx_signing = []
+unstable = []
 
 [dependencies]
 bitcoin = "0.24"
index e466205c97c83a9ad854c6aad71d52db6817848c..7310a49aef53ac6f1432df78c0e04103002836ab 100644 (file)
@@ -27,6 +27,9 @@
 #![allow(bare_trait_objects)]
 #![allow(ellipsis_inclusive_range_patterns)]
 
+#![cfg_attr(all(test, feature = "unstable"), feature(test))]
+#[cfg(all(test, feature = "unstable"))] extern crate test;
+
 extern crate bitcoin;
 #[cfg(any(test, feature = "_test_utils"))] extern crate hex;
 #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] extern crate regex;
index 32084fad581ad55e232d997584251af168a853bc..9f1b60e0d27adf83735dfc02c0f20720e9c67161 100644 (file)
@@ -1279,3 +1279,48 @@ mod tests {
                assert_eq!(route.paths[0][1].channel_features.le_flags(), &[0; 0]); // We can't learn any flags from invoices, sadly
        }
 }
+
+#[cfg(all(test, feature = "unstable"))]
+mod benches {
+       use super::*;
+       use util::logger::{Logger, Record};
+
+       use std::fs::File;
+       use test::Bencher;
+
+       struct DummyLogger {}
+       impl Logger for DummyLogger {
+               fn log(&self, _record: &Record) {}
+       }
+
+       #[bench]
+       fn generate_routes(bench: &mut Bencher) {
+               let mut d = File::open("net_graph-2021-02-12.bin").expect("Please fetch https://bitcoin.ninja/ldk-net_graph-879e309c128-2020-02-12.bin and place it at lightning/net_graph-2021-02-12.bin");
+               let graph = NetworkGraph::read(&mut d).unwrap();
+
+               // 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 = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap();
+                               seed *= 0xdeadbeef;
+                               let dst = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap();
+                               let amt = seed as u64 % 1_000_000;
+                               if get_route(src, &graph, dst, None, &[], amt, 42, &DummyLogger{}).is_ok() {
+                                       path_endpoints.push((src, dst, amt));
+                                       continue 'load_endpoints;
+                               }
+                       }
+               }
+
+               // ...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()];
+                       assert!(get_route(src, &graph, dst, None, &[], amt, 42, &DummyLogger{}).is_ok());
+                       idx += 1;
+               });
+       }
+}