Skip to content

Commit 0646980

Browse files
committed
chore: add crate bench to workspace
chain: add optional dep `criterion`. Ideally this should be conditional on "--cfg=bdk_bench". See LDK for example
1 parent 3bc45b5 commit 0646980

File tree

7 files changed

+137
-1
lines changed

7 files changed

+137
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/target
1+
**/target
22
Cargo.lock
33
/.vscode
44

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ members = [
1818
"example-crates/example_wallet_esplora_async",
1919
"example-crates/example_wallet_rpc",
2020
]
21+
exclude = ["bench"]
2122

2223
[workspace.package]
2324
authors = ["Bitcoin Dev Kit Developers"]

bench/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "bdk_bench"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[[bench]]
7+
name = "bench"
8+
harness = false
9+
10+
[dependencies]
11+
bdk_chain = { path = "../crates/chain" }
12+
criterion = { version = "0.5", default-features = false }

bench/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# To run benches
2+
3+
`cargo bench`

bench/benches/bench.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extern crate bdk_chain;
2+
extern crate criterion;
3+
4+
use criterion::{criterion_group, criterion_main};
5+
6+
criterion_group!(benches, bdk_chain::tx_graph::bench::filter_chain_unspents);
7+
criterion_main!(benches);

crates/chain/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ bitcoin = { version = "0.32.0", default-features = false }
2020
bdk_core = { path = "../core", version = "0.3.0", default-features = false }
2121
serde = { version = "1", optional = true, features = ["derive", "rc"] }
2222
miniscript = { version = "12.0.0", optional = true, default-features = false }
23+
criterion = { version = "0.5", default-features = false }
2324

2425
# Feature dependencies
2526
rusqlite = { version = "0.31.0", features = ["bundled"], optional = true }

crates/chain/src/tx_graph.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,3 +1553,115 @@ where
15531553
fn tx_outpoint_range(txid: Txid) -> RangeInclusive<OutPoint> {
15541554
OutPoint::new(txid, u32::MIN)..=OutPoint::new(txid, u32::MAX)
15551555
}
1556+
1557+
/// Bench
1558+
#[allow(unused)]
1559+
#[allow(missing_docs)]
1560+
pub mod bench {
1561+
use std::str::FromStr;
1562+
1563+
use bdk_core::{CheckPoint, ConfirmationBlockTime};
1564+
use bitcoin::absolute;
1565+
use bitcoin::hashes::Hash;
1566+
use bitcoin::transaction;
1567+
use bitcoin::{Address, BlockHash, Network, TxIn};
1568+
use criterion::Criterion;
1569+
use miniscript::Descriptor;
1570+
use miniscript::DescriptorPublicKey;
1571+
1572+
use super::*;
1573+
use crate::keychain_txout::KeychainTxOutIndex;
1574+
use crate::local_chain::LocalChain;
1575+
use crate::IndexedTxGraph;
1576+
1577+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
1578+
enum Keychain {
1579+
External,
1580+
}
1581+
1582+
const EXTERNAL: &str = "tr([ab28dc00/86h/1h/0h]tpubDCdDtzAMZZrkwKBxwNcGCqe4FRydeD9rfMisoi7qLdraG79YohRfPW4YgdKQhpgASdvh612xXNY5xYzoqnyCgPbkpK4LSVcH5Xv4cK7johH/0/*)";
1583+
1584+
fn get_params() -> (
1585+
IndexedTxGraph<ConfirmationBlockTime, KeychainTxOutIndex<Keychain>>,
1586+
LocalChain,
1587+
) {
1588+
let genesis = bitcoin::constants::genesis_block(Network::Regtest).block_hash();
1589+
let block_0 = BlockId {
1590+
height: 0,
1591+
hash: genesis,
1592+
};
1593+
let mut cp = CheckPoint::new(block_0);
1594+
let block_100 = BlockId {
1595+
height: 100,
1596+
hash: BlockHash::all_zeros(),
1597+
};
1598+
cp = cp.push(block_100).unwrap();
1599+
let chain = LocalChain::from_tip(cp).unwrap();
1600+
1601+
let mut graph = IndexedTxGraph::new({
1602+
let mut index = KeychainTxOutIndex::new(10);
1603+
index
1604+
.insert_descriptor(Keychain::External, parse_descriptor(EXTERNAL))
1605+
.unwrap();
1606+
index
1607+
});
1608+
1609+
// insert funding tx (coinbase)
1610+
let addr_0 =
1611+
Address::from_str("bcrt1plhmjhj75nut38qwwm5w7xqysy25xhd4ckuv7zu5tey3nkmcwh3cqvan5mz")
1612+
.unwrap()
1613+
.assume_checked();
1614+
let tx_0 = Transaction {
1615+
output: vec![TxOut {
1616+
script_pubkey: addr_0.script_pubkey(),
1617+
value: Amount::ONE_BTC,
1618+
}],
1619+
..new_tx(0)
1620+
};
1621+
let txid_0 = tx_0.compute_txid();
1622+
let _ = graph.insert_tx(tx_0);
1623+
let _ = graph.insert_anchor(
1624+
txid_0,
1625+
ConfirmationBlockTime {
1626+
block_id: block_100,
1627+
confirmation_time: 100,
1628+
},
1629+
);
1630+
1631+
(graph, chain)
1632+
}
1633+
1634+
fn parse_descriptor(s: &str) -> miniscript::Descriptor<DescriptorPublicKey> {
1635+
<Descriptor<DescriptorPublicKey>>::parse_descriptor(
1636+
&bitcoin::secp256k1::Secp256k1::new(),
1637+
s,
1638+
)
1639+
.unwrap()
1640+
.0
1641+
}
1642+
1643+
fn new_tx(lt: u32) -> Transaction {
1644+
Transaction {
1645+
version: transaction::Version::TWO,
1646+
lock_time: absolute::LockTime::from_consensus(lt),
1647+
input: vec![],
1648+
output: vec![],
1649+
}
1650+
}
1651+
1652+
pub fn filter_chain_unspents(bench: &mut Criterion) {
1653+
let (graph, chain) = get_params();
1654+
// TODO: insert conflicts
1655+
let outpoints = graph.index.outpoints().clone();
1656+
bench.bench_function("filter_chain_unspents", |b| {
1657+
b.iter(|| {
1658+
TxGraph::filter_chain_unspents(
1659+
graph.graph(),
1660+
&chain,
1661+
chain.tip().block_id(),
1662+
outpoints.clone(),
1663+
)
1664+
})
1665+
});
1666+
}
1667+
}

0 commit comments

Comments
 (0)