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 6, 2024
1 parent 5c38bb5 commit 1004254
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 53 deletions.
1 change: 1 addition & 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="254fafdf71b
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 Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ As discussed in the literature above, ideally we want to choose a selection from

## Benchmarks

<<<<<<< HEAD
To run the benchmarks use: `RUSTFLAGS='--cfg=bench' cargo +nightly bench`.
=======
To run the benchmarks use: `cargo bench`.

### performance comparison
Expand All @@ -30,9 +27,8 @@ A basic performance comparison between this current [Rust BnB](https://github.co

|implementation|pool size|ns/iter|
|-------------:|---------|-------|
| Rust BnB| 1,000|823,680|
| Rust BnB| 1,000|695,860|
| C++ Core BnB| 1,000|816,374|
>>>>>>> 4ede8d5 (Add cost of change param)

## Minimum Supported Rust Version (MSRV)

Expand Down
47 changes: 0 additions & 47 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,50 +740,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 1004254

Please sign in to comment.