Skip to content

Benchmark get_route in CI #798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,39 @@ jobs:
token: f421b687-4dc2-4387-ac3d-dc3b2528af57
fail_ci_if_error: true

benchmark:
runs-on: ubuntu-latest
env:
TOOLCHAIN: nightly
steps:
- name: Checkout source code
uses: actions/checkout@v2
- name: Install Rust ${{ env.TOOLCHAIN }} toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ env.TOOLCHAIN }}
override: true
profile: minimal
- name: Cache routing graph snapshot
id: cache-graph
uses: actions/cache@v2
with:
path: lightning/net_graph-2021-02-12.bin
key: net_graph-2021-02-12
- name: Fetch routing graph snapshot
if: steps.cache-graph.outputs.cache-hit != 'true'
run: |
wget -O lightning/net_graph-2021-02-12.bin https://bitcoin.ninja/ldk-net_graph-879e309c128-2020-02-12.bin
if [ "$(sha256sum lightning/net_graph-2021-02-12.bin | awk '{ print $1 }')" != "890a1f80dfb6ef674a1e4ff0f23cd73d740731c395f99d85abbede0cfbb701ab" ]; then
echo "Bad hash"
exit 1
fi
- name: Run benchmarks on Rust ${{ matrix.toolchain }}
run: |
cd lightning
cargo bench --features unstable
cd ..

check_commits:
runs-on: ubuntu-latest
env:
Expand Down
1 change: 1 addition & 0 deletions lightning/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions lightning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions lightning/src/ln/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,7 @@ fn test_fee_spike_violation_fails_htlc() {
let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes.last().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
(route, payment_hash, payment_preimage)
}}
};
}

let (route, payment_hash, _) = get_route_and_payment_hash!(3460001);
// Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
Expand Down Expand Up @@ -1722,7 +1722,7 @@ fn test_chan_reserve_violation_outbound_htlc_inbound_chan() {
let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.first().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
(route, payment_hash, payment_preimage)
}}
};
}

let (route, our_payment_hash, _) = get_route_and_payment_hash!(1000);
unwrap_send_err!(nodes[1].node.send_payment(&route, our_payment_hash, &None), true, APIError::ChannelUnavailable { ref err },
Expand Down Expand Up @@ -1754,7 +1754,7 @@ fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.first().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
(route, payment_hash, payment_preimage)
}}
};
}

let (route, payment_hash, _) = get_route_and_payment_hash!(1000);
// Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
Expand Down Expand Up @@ -1799,7 +1799,7 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.last().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
(route, payment_hash, payment_preimage)
}}
};
}

let feemsat = 239;
let total_routing_fee_msat = (nodes.len() - 2) as u64 * feemsat;
Expand Down Expand Up @@ -1900,7 +1900,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.last().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
(route, payment_hash, payment_preimage)
}}
};
}

macro_rules! expect_forward {
($node: expr) => {{
Expand Down
45 changes: 45 additions & 0 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
4 changes: 2 additions & 2 deletions lightning/src/util/chacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ mod test {
key: [u8; 32],
nonce: [u8; 8],
keystream: Vec<u8>,
};
}
// taken from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
let test_vectors = vec!(
TestVector{
Expand Down Expand Up @@ -463,7 +463,7 @@ mod test {
key: [u8; 32],
nonce: [u8; 12],
keystream: Vec<u8>,
};
}
// taken from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
let test_vectors = vec!(
TestVector{
Expand Down