Skip to content

Commit d7814b2

Browse files
committed
Add a trivial benchmark of calculating routes on today's graph
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.
1 parent 35bb2d0 commit d7814b2

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

lightning/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ max_level_debug = []
2323
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
2424
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
2525
unsafe_revoked_tx_signing = []
26+
unstable = []
2627

2728
[dependencies]
2829
bitcoin = "0.24"

lightning/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#![allow(bare_trait_objects)]
2828
#![allow(ellipsis_inclusive_range_patterns)]
2929

30+
#![cfg_attr(all(test, feature = "unstable"), feature(test))]
31+
#[cfg(all(test, feature = "unstable"))] extern crate test;
32+
3033
extern crate bitcoin;
3134
#[cfg(any(test, feature = "_test_utils"))] extern crate hex;
3235
#[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] extern crate regex;

lightning/src/routing/router.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,3 +1279,47 @@ mod tests {
12791279
assert_eq!(route.paths[0][1].channel_features.le_flags(), &[0; 0]); // We can't learn any flags from invoices, sadly
12801280
}
12811281
}
1282+
1283+
#[cfg(all(test, feature = "unstable"))]
1284+
mod benches {
1285+
use super::*;
1286+
use util::logger::{Logger, Record};
1287+
1288+
use std::fs::File;
1289+
use test::Bencher;
1290+
1291+
struct DummyLogger {}
1292+
impl Logger for DummyLogger {
1293+
fn log(&self, _record: &Record) {}
1294+
}
1295+
1296+
#[bench]
1297+
fn generate_routes(bench: &mut Bencher) {
1298+
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");
1299+
let graph = NetworkGraph::read(&mut d).unwrap();
1300+
1301+
// First, get 100 (source, destination) pairs for which route-getting actually succeeds...
1302+
let mut path_endpoints = Vec::new();
1303+
let mut seed: usize = 0xdeadbeef;
1304+
'load_endpoints: for _ in 0..100 {
1305+
loop {
1306+
seed *= 0xdeadbeef;
1307+
let src = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap();
1308+
seed *= 0xdeadbeef;
1309+
let dst = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap();
1310+
if get_route(src, &graph, dst, None, &[], seed as u64 % 1_000_000, 42, &DummyLogger{}).is_ok() {
1311+
path_endpoints.push((src, dst));
1312+
continue 'load_endpoints;
1313+
}
1314+
}
1315+
}
1316+
1317+
// ...then benchmark finding paths between the nodes we learned.
1318+
let mut idx = 0;
1319+
bench.iter(|| {
1320+
let (src, dst) = path_endpoints[idx % path_endpoints.len()];
1321+
assert!(get_route(src, &graph, dst, None, &[], seed as u64 % 1_000_000, 42, &DummyLogger{}).is_ok());
1322+
idx += 1;
1323+
});
1324+
}
1325+
}

0 commit comments

Comments
 (0)