Skip to content

Commit c7113bc

Browse files
committed
Add benchmark of deserializing a NetworkGraph.
NetworkGraph is one of the largest structures we generally deserialize, so it makes for a good benchmark, even if it isn't the most complicated one. As of this commit, on an Intel 2687W v3, these benchmarks take: test routing::network_graph::benches::read_network_graph ... bench: 2,101,420,078 ns/iter (+/- 6,649,020) test routing::network_graph::benches::write_network_graph ... bench: 344,696,835 ns/iter (+/- 229,061)
1 parent 4cc320b commit c7113bc

File tree

2 files changed

+63
-35
lines changed

2 files changed

+63
-35
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,3 +2424,30 @@ mod tests {
24242424
assert!(result.is_err());
24252425
}
24262426
}
2427+
2428+
#[cfg(all(test, feature = "unstable"))]
2429+
mod benches {
2430+
use super::*;
2431+
2432+
use test::Bencher;
2433+
use std::io::Read;
2434+
2435+
#[bench]
2436+
fn read_network_graph(bench: &mut Bencher) {
2437+
let mut d = ::routing::router::test_utils::get_route_file().unwrap();
2438+
let mut v = Vec::new();
2439+
d.read_to_end(&mut v).unwrap();
2440+
bench.iter(|| {
2441+
let _ = NetworkGraph::read(&mut std::io::Cursor::new(&v)).unwrap();
2442+
});
2443+
}
2444+
2445+
#[bench]
2446+
fn write_network_graph(bench: &mut Bencher) {
2447+
let mut d = ::routing::router::test_utils::get_route_file().unwrap();
2448+
let net_graph = NetworkGraph::read(&mut d).unwrap();
2449+
bench.iter(|| {
2450+
let _ = net_graph.encode();
2451+
});
2452+
}
2453+
}

lightning/src/routing/router.rs

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3856,44 +3856,21 @@ mod tests {
38563856
}
38573857
}
38583858

3859-
use std::fs::File;
3860-
use util::ser::Readable;
3861-
/// Tries to open a network graph file, or panics with a URL to fetch it.
3862-
pub(super) fn get_route_file() -> Result<std::fs::File, std::io::Error> {
3863-
let res = File::open("net_graph-2021-05-27.bin") // By default we're run in RL/lightning
3864-
.or_else(|_| File::open("lightning/net_graph-2021-05-27.bin")) // We may be run manually in RL/
3865-
.or_else(|_| { // Fall back to guessing based on the binary location
3866-
// path is likely something like .../rust-lightning/target/debug/deps/lightning-...
3867-
let mut path = std::env::current_exe().unwrap();
3868-
path.pop(); // lightning-...
3869-
path.pop(); // deps
3870-
path.pop(); // debug
3871-
path.pop(); // target
3872-
path.push("lightning");
3873-
path.push("net_graph-2021-05-27.bin");
3874-
eprintln!("{}", path.to_str().unwrap());
3875-
File::open(path)
3876-
});
3877-
#[cfg(require_route_graph_test)]
3878-
return Ok(res.expect("Didn't have route graph and was configured to require it"));
3879-
#[cfg(not(require_route_graph_test))]
3880-
return res;
3881-
}
3882-
38833859
pub(super) fn random_init_seed() -> u64 {
38843860
// Because the default HashMap in std pulls OS randomness, we can use it as a (bad) RNG.
38853861
use core::hash::{BuildHasher, Hasher};
38863862
let seed = std::collections::hash_map::RandomState::new().build_hasher().finish();
38873863
println!("Using seed of {}", seed);
38883864
seed
38893865
}
3866+
use util::ser::Readable;
38903867

38913868
#[test]
38923869
fn generate_routes() {
3893-
let mut d = match get_route_file() {
3870+
let mut d = match super::test_utils::get_route_file() {
38943871
Ok(f) => f,
3895-
Err(_) => {
3896-
eprintln!("Please fetch https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin and place it at lightning/net_graph-2021-05-27.bin");
3872+
Err(e) => {
3873+
eprintln!("{}", e);
38973874
return;
38983875
},
38993876
};
@@ -3917,10 +3894,10 @@ mod tests {
39173894

39183895
#[test]
39193896
fn generate_routes_mpp() {
3920-
let mut d = match get_route_file() {
3897+
let mut d = match super::test_utils::get_route_file() {
39213898
Ok(f) => f,
3922-
Err(_) => {
3923-
eprintln!("Please fetch https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin and place it at lightning/net_graph-2021-05-27.bin");
3899+
Err(e) => {
3900+
eprintln!("{}", e);
39243901
return;
39253902
},
39263903
};
@@ -3943,12 +3920,38 @@ mod tests {
39433920
}
39443921
}
39453922

3923+
#[cfg(test)]
3924+
pub(crate) mod test_utils {
3925+
use std::fs::File;
3926+
/// Tries to open a network graph file, or panics with a URL to fetch it.
3927+
pub(crate) fn get_route_file() -> Result<std::fs::File, &'static str> {
3928+
let res = File::open("net_graph-2021-05-27.bin") // By default we're run in RL/lightning
3929+
.or_else(|_| File::open("lightning/net_graph-2021-05-27.bin")) // We may be run manually in RL/
3930+
.or_else(|_| { // Fall back to guessing based on the binary location
3931+
// path is likely something like .../rust-lightning/target/debug/deps/lightning-...
3932+
let mut path = std::env::current_exe().unwrap();
3933+
path.pop(); // lightning-...
3934+
path.pop(); // deps
3935+
path.pop(); // debug
3936+
path.pop(); // target
3937+
path.push("lightning");
3938+
path.push("net_graph-2021-05-27.bin");
3939+
eprintln!("{}", path.to_str().unwrap());
3940+
File::open(path)
3941+
})
3942+
.map_err(|_| "Please fetch https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin and place it at lightning/net_graph-2021-05-27.bin");
3943+
#[cfg(require_route_graph_test)]
3944+
return Ok(res.unwrap());
3945+
#[cfg(not(require_route_graph_test))]
3946+
return res;
3947+
}
3948+
}
3949+
39463950
#[cfg(all(test, feature = "unstable"))]
39473951
mod benches {
39483952
use super::*;
39493953
use util::logger::{Logger, Record};
39503954

3951-
use prelude::*;
39523955
use test::Bencher;
39533956

39543957
struct DummyLogger {}
@@ -3958,8 +3961,7 @@ mod benches {
39583961

39593962
#[bench]
39603963
fn generate_routes(bench: &mut Bencher) {
3961-
let mut d = tests::get_route_file()
3962-
.expect("Please fetch https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin and place it at lightning/net_graph-2021-05-27.bin");
3964+
let mut d = test_utils::get_route_file().unwrap();
39633965
let graph = NetworkGraph::read(&mut d).unwrap();
39643966

39653967
// First, get 100 (source, destination) pairs for which route-getting actually succeeds...
@@ -3990,8 +3992,7 @@ mod benches {
39903992

39913993
#[bench]
39923994
fn generate_mpp_routes(bench: &mut Bencher) {
3993-
let mut d = tests::get_route_file()
3994-
.expect("Please fetch https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin and place it at lightning/net_graph-2021-05-27.bin");
3995+
let mut d = test_utils::get_route_file().unwrap();
39953996
let graph = NetworkGraph::read(&mut d).unwrap();
39963997

39973998
// First, get 100 (source, destination) pairs for which route-getting actually succeeds...

0 commit comments

Comments
 (0)