Skip to content

Commit

Permalink
Add a benchmark for decaying a 100k channel scorer's liquidity info
Browse files Browse the repository at this point in the history
This is a good gut-check to ensure we don't end up taking a ton of
time decaying channel liquidity info.

It currently clocks in around 1.25ms on an i7-1360P.
  • Loading branch information
TheBlueMatt committed Dec 13, 2023
1 parent 512f44c commit 40b4094
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion bench/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ criterion_group!(benches,
lightning_persister::fs_store::bench::bench_sends,
lightning_rapid_gossip_sync::bench::bench_reading_full_graph_from_file,
lightning::routing::gossip::benches::read_network_graph,
lightning::routing::gossip::benches::write_network_graph);
lightning::routing::gossip::benches::write_network_graph,
lightning::routing::scoring::benches::decay_100k_channel_bounds);
criterion_main!(benches);
58 changes: 58 additions & 0 deletions lightning/src/routing/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3748,3 +3748,61 @@ mod tests {
Some(0.0));
}
}

#[cfg(ldk_bench)]
pub mod benches {
use super::*;
use criterion::Criterion;
use crate::routing::router::{bench_utils, RouteHop};
use crate::util::test_utils::TestLogger;
use crate::ln::features::{ChannelFeatures, NodeFeatures};

pub fn decay_100k_channel_bounds(bench: &mut Criterion) {
let logger = TestLogger::new();
let network_graph = bench_utils::read_network_graph(&logger).unwrap();
let mut scorer = ProbabilisticScorer::new(Default::default(), &network_graph, &logger);
// Score a number of random channels
let mut seed: u64 = 0xdeadbeef;
for _ in 0..100_000 {
seed = seed.overflowing_mul(6364136223846793005).0.overflowing_add(1).0;
let (victim, victim_dst, amt) = {
let rong = network_graph.read_only();
let channels = rong.channels();
let chan = channels.unordered_iter()
.skip((seed as usize) % channels.len())
.next().unwrap();
seed = seed.overflowing_mul(6364136223846793005).0.overflowing_add(1).0;
let amt = seed % chan.1.capacity_sats.map(|c| c * 1000)
.or(chan.1.one_to_two.as_ref().map(|info| info.htlc_maximum_msat))
.or(chan.1.two_to_one.as_ref().map(|info| info.htlc_maximum_msat))
.unwrap_or(1_000_000_000).saturating_add(1);
(*chan.0, chan.1.node_two, amt)
};
let path = Path {
hops: vec![RouteHop {
pubkey: victim_dst.as_pubkey().unwrap(),
node_features: NodeFeatures::empty(),
short_channel_id: victim,
channel_features: ChannelFeatures::empty(),
fee_msat: amt,
cltv_expiry_delta: 42,
maybe_announced_channel: true,
}],
blinded_tail: None
};
seed = seed.overflowing_mul(6364136223846793005).0.overflowing_add(1).0;
if seed % 1 == 0 {
scorer.probe_failed(&path, victim, Duration::ZERO);
} else {
scorer.probe_successful(&path, Duration::ZERO);
}
}
let mut cur_time = Duration::ZERO;
cur_time += Duration::from_millis(1);
scorer.decay_liquidity_certainty(cur_time);
bench.bench_function("decay_100k_channel_bounds", |b| b.iter(|| {
cur_time += Duration::from_millis(1);
scorer.decay_liquidity_certainty(cur_time);
}));
}
}

0 comments on commit 40b4094

Please sign in to comment.