Skip to content

Commit ad6d026

Browse files
committed
Add channel open operations benchmark
Add a channel-open benchmark that measures the open_channel call while leaving chain confirmation cleanup outside the timed section. AI-assisted-by: OpenAI Codex
1 parent c66ca51 commit ad6d026

1 file changed

Lines changed: 91 additions & 3 deletions

File tree

benches/operations.rs

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ use std::time::{Duration, Instant};
1313

1414
use bitcoin::Amount;
1515
use common::{
16-
expect_event, generate_blocks_and_wait, premine_and_distribute_funds, random_config,
17-
setup_bitcoind_and_electrsd, setup_node, store_bench_configs, wait_for_payment_success,
16+
expect_channel_pending_event, expect_channel_ready_event, expect_event,
17+
generate_blocks_and_wait, open_channel_no_wait, premine_and_distribute_funds, random_config,
18+
setup_bitcoind_and_electrsd, setup_node, setup_two_nodes_with_store, store_bench_configs,
19+
wait_for_payment_success,
1820
};
1921
use criterion::{criterion_group, criterion_main, Criterion};
20-
use electrsd::corepc_node::Node as BitcoinD;
22+
use electrsd::corepc_node::{Client as BitcoindClient, Node as BitcoinD};
2123
use ldk_node::{Event, Node};
2224
use lightning::ln::channelmanager::PaymentId;
2325
use lightning_invoice::{Bolt11InvoiceDescription, Description};
@@ -26,6 +28,7 @@ use crate::common::{open_channel_push_amt, TestChainSource, TestStoreType};
2628

2729
fn operations_benchmark(c: &mut Criterion) {
2830
forwarding_benchmark(c);
31+
channel_open_benchmark(c);
2932
}
3033

3134
fn forwarding_benchmark(c: &mut Criterion) {
@@ -73,6 +76,72 @@ fn benchmark_runtime() -> tokio::runtime::Runtime {
7376
builder.build().unwrap()
7477
}
7578

79+
fn channel_open_benchmark(c: &mut Criterion) {
80+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
81+
let chain_source = TestChainSource::BitcoindRpcSync(&bitcoind);
82+
let runtime = benchmark_runtime();
83+
84+
let mut group = c.benchmark_group("channel_open");
85+
group.sample_size(10);
86+
87+
for store_config in store_bench_configs() {
88+
if !should_register_bench("channel_open", store_config.name) {
89+
continue;
90+
}
91+
let (node_a, node_b) =
92+
setup_two_nodes_with_store(&chain_source, false, true, false, store_config.store_type);
93+
let node_a = Arc::new(node_a);
94+
let node_b = Arc::new(node_b);
95+
96+
// connect nodes
97+
node_a
98+
.connect(
99+
node_b.node_id(),
100+
node_b.listening_addresses().unwrap().first().unwrap().clone(),
101+
true,
102+
)
103+
.unwrap();
104+
105+
runtime.block_on(async {
106+
let address_a = node_a.onchain_payment().new_address().unwrap();
107+
premine_and_distribute_funds(
108+
&bitcoind.client,
109+
&electrsd.client,
110+
vec![address_a],
111+
Amount::from_sat(35_000_000),
112+
)
113+
.await;
114+
node_a.sync_wallets().unwrap();
115+
});
116+
117+
let node_a = Arc::clone(&node_a);
118+
let node_b = Arc::clone(&node_b);
119+
let bitcoind_client = &bitcoind.client;
120+
let electrsd_ref = &electrsd;
121+
122+
group.bench_function(store_config.name, |b| {
123+
b.to_async(&runtime).iter_custom(|iter| {
124+
let node_a = Arc::clone(&node_a);
125+
let node_b = Arc::clone(&node_b);
126+
127+
async move {
128+
let mut total = Duration::ZERO;
129+
for _ in 0..iter {
130+
total += open_channel(
131+
Arc::clone(&node_a),
132+
Arc::clone(&node_b),
133+
bitcoind_client,
134+
electrsd_ref,
135+
)
136+
.await;
137+
}
138+
total
139+
}
140+
});
141+
});
142+
}
143+
}
144+
76145
/// Returns whether the benchmark identified by `group/name` matches the CLI filters.
77146
///
78147
/// Criterion applies its own filters after benchmark registration, but these benches do expensive
@@ -249,6 +318,25 @@ async fn wait_for_forwarding_path(nodes: &[Arc<Node>]) {
249318
panic!("Timed out waiting for forwarding path readiness");
250319
}
251320

321+
async fn open_channel(
322+
node_a: Arc<Node>, node_b: Arc<Node>, bitcoind: &BitcoindClient, electrsd: &electrsd::ElectrsD,
323+
) -> Duration {
324+
let start = Instant::now();
325+
let funding_txo = open_channel_no_wait(&node_a, &node_b, 100_000, None, false).await;
326+
let duration = start.elapsed();
327+
328+
common::wait_for_tx(&electrsd.client, funding_txo.txid).await;
329+
330+
generate_blocks_and_wait(bitcoind, &electrsd.client, 6).await;
331+
node_a.sync_wallets().unwrap();
332+
node_b.sync_wallets().unwrap();
333+
334+
expect_channel_ready_event!(node_b, node_a.node_id());
335+
expect_channel_ready_event!(node_a, node_b.node_id());
336+
337+
duration
338+
}
339+
252340
fn drain_events(node: &Node) {
253341
while node.next_event().is_some() {
254342
node.event_handled().unwrap();

0 commit comments

Comments
 (0)