Skip to content

Commit

Permalink
Replace cargo bench with criterion
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Feb 23, 2024
1 parent f725f5c commit a6025a1
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 50 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bitcoin = { git="https://github.com/yancyribbens/rust-bitcoin", rev="2f109442e30
rand = {version = "0.8.5", default-features = false, optional = true}

[dev-dependencies]
criterion = "0.3"
rust-bitcoin-coin-selection = {path = ".", features = ["rand"]}
rand = "0.8.5"

Expand All @@ -27,3 +28,7 @@ bitcoin_hashes = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="2f
bitcoin-io = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="2f109442e30d74fb7502e7fd1ce2075a67262cd5" }
bitcoin-units = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="2f109442e30d74fb7502e7fd1ce2075a67262cd5" }
bitcoin-internals = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="2f109442e30d74fb7502e7fd1ce2075a67262cd5" }

[[bench]]
name = "coin_selection"
harness = false
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ The current interface is provided via `select_coins()` function. The required p
As discussed in the literature above, ideally we want to choose a selection from the existing UTXO set available to the wallet. However, if there is no combination that efficiently matches the target spend amount, then creating a change output by splitting a UTXO is the next best option. Therefore, the algorithm takes into account the current cost of creating a new output (cost_of_change).

## Benchmarks
To run the benchmarks use: `cargo bench`.

To run the benchmarks use: `RUSTFLAGS='--cfg=bench' cargo +nightly bench`.
Note: criterion requires rustc version 1.65 to run the benchmarks.

### performance comparison

A basic performance comparison between this current [Rust BnB](https://github.com/p2pderivatives/rust-bitcoin-coin-selection/pull/28/files#diff-9098d62be93e83524a8371395c973d761a95000d1c295f600a8c808e917c16d9R122) implementation and the [Bitcoin Core](https://github.com/bitcoin/bitcoin/blob/4b1196a9855dcd188a24f393aa2fa21e2d61f061/src/wallet/coinselection.cpp#L76) version using commodity hardware (My rather old laptop).

|implementation|pool size|ns/iter|
|-------------:|---------|-------|
| Rust BnB| 1,000|823,680|
| Rust BnB| 1,000|695,860|
| C++ Core BnB| 1,000|816,374|

Note: The measurements where recorded using rustc 1.75. Expect worse performance with MSRV.

## Minimum Supported Rust Version (MSRV)

This library should always compile with any combination of features on **Rust 1.48**.
49 changes: 49 additions & 0 deletions benches/coin_selection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use bitcoin::Amount;
use bitcoin::FeeRate;
use bitcoin::ScriptBuf;
use bitcoin::TxOut;
use bitcoin::Weight;
use rust_bitcoin_coin_selection::select_coins_bnb;
use rust_bitcoin_coin_selection::WeightedUtxo;

pub fn criterion_benchmark(c: &mut Criterion) {
// https://github.com/bitcoin/bitcoin/blob/f3bc1a72825fe2b51f4bc20e004cef464f05b965/src/wallet/coinselection.h#L18
let cost_of_change = Amount::from_sat(50_000);

let one = WeightedUtxo {
satisfaction_weight: Weight::ZERO,
utxo: TxOut { value: Amount::from_sat(1_000), script_pubkey: ScriptBuf::new() },
};

let two = WeightedUtxo {
satisfaction_weight: Weight::ZERO,
utxo: TxOut { value: Amount::from_sat(3), script_pubkey: ScriptBuf::new() },
};

let target = Amount::from_sat(1_003);
let mut utxo_pool = vec![one; 1000];
utxo_pool.push(two);

c.bench_function("bnb 1000", |b| {
b.iter(|| {
let result: Vec<_> = select_coins_bnb(
black_box(target),
black_box(cost_of_change),
black_box(FeeRate::ZERO),
black_box(FeeRate::ZERO),
black_box(&mut utxo_pool),
)
.unwrap()
.collect();

assert_eq!(2, result.len());
assert_eq!(Amount::from_sat(1_000), result[0].utxo.value);
assert_eq!(Amount::from_sat(3), result[1].utxo.value);
})
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
47 changes: 0 additions & 47 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,50 +793,3 @@ mod tests {
assert!(list.is_none());
}
}

#[cfg(bench)]
#[cfg(test)]
mod benches {
use crate::select_coins_bnb;
use crate::Utxo;
use test::Bencher;

#[derive(Clone, Debug, Eq, PartialEq)]
struct MinimalUtxo {
value: u64,
}

impl Utxo for MinimalUtxo {
fn get_value(&self) -> u64 {
self.value
}
}

#[bench]
/// Creates a UTXO pool of 1,000 coins that do not match and one coin
/// that will be a match when combined with any of the other 1,000 coins.
///
/// Matching benchmark of Bitcoin core coin-selection benchmark.
// https://github.com/bitcoin/bitcoin/blob/f3bc1a72825fe2b51f4bc20e004cef464f05b965/src/bench/coin_selection.cpp#L44
fn bench_select_coins_bnb(bh: &mut Bencher) {
// https://github.com/bitcoin/bitcoin/blob/f3bc1a72825fe2b51f4bc20e004cef464f05b965/src/consensus/amount.h#L15
/// The amount of satoshis in one BTC.
const COIN: u64 = 100_000_000;

// https://github.com/bitcoin/bitcoin/blob/f3bc1a72825fe2b51f4bc20e004cef464f05b965/src/wallet/coinselection.h#L18
/// lower bound for randomly-chosen target change amount
const CHANGE_LOWER: u64 = 50_000;

let u = MinimalUtxo { value: 1000 * COIN };
let mut utxo_pool = vec![u; 1000];
utxo_pool.push(MinimalUtxo { value: 3 * COIN });

bh.iter(|| {
let result =
select_coins_bnb(1003 * COIN, CHANGE_LOWER, &mut utxo_pool.clone()).unwrap();
assert_eq!(2, result.len());
assert_eq!(1000 * COIN, result[0].value);
assert_eq!(3 * COIN, result[1].value);
});
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use bitcoin::SignedAmount;
use bitcoin::TxOut;
use bitcoin::Weight;

use crate::branch_and_bound::select_coins_bnb;
pub use crate::branch_and_bound::select_coins_bnb;
use crate::single_random_draw::select_coins_srd;
use bitcoin::blockdata::transaction::TxIn;
use rand::thread_rng;
Expand Down

0 comments on commit a6025a1

Please sign in to comment.