Skip to content

Commit

Permalink
Benchmarking draft 1
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoZ666 committed Oct 6, 2024
1 parent bf812ac commit 32c3876
Show file tree
Hide file tree
Showing 8 changed files with 1,104 additions and 0 deletions.
595 changes: 595 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,33 @@ edition = "2021"
[dependencies]
rand = "0.8.5"

[dev-dependencies]
criterion = "0.3"

#Empty default feature set, (helpful to generalise in github actions)
[features]
default = []

[[bench]]
name = "benches"
harness = false

[[bench]]
name = "benches_srd"
harness = false

[[bench]]
name = "benches_bnb"
harness = false

[[bench]]
name = "benches_knapsack"
harness = false

[[bench]]
name = "benches_lowestlarger"
harness = false

[[bench]]
name = "benches_fifo"
harness = false
90 changes: 90 additions & 0 deletions benches/benches.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rust_coinselect::{
select_coin, CoinSelectionOpt, ExcessStrategy, OutputGroup, SelectionError, SelectionOutput,
};

fn benchmark_select_coin(c: &mut Criterion) {
let inputs = [
OutputGroup {
value: 55000,
weight: 500,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 400,
weight: 200,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 40000,
weight: 300,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 25000,
weight: 100,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 35000,
weight: 150,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 600,
weight: 250,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 30000,
weight: 120,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 5000,
weight: 50,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
];

let options = CoinSelectionOpt {
target_value: 5730,
target_feerate: 0.5, // Simplified feerate
long_term_feerate: None,
min_absolute_fee: 0,
base_weight: 10,
drain_weight: 50,
drain_cost: 10,
cost_per_input: 20,
cost_per_output: 10,
min_drain_value: 500,
excess_strategy: ExcessStrategy::ToDrain,
};

c.bench_function("select_coin", |b| {
b.iter(|| {
let result: Result<SelectionOutput, SelectionError> =
select_coin(black_box(&inputs), black_box(options));
let _ = black_box(result);
})
});
}

criterion_group!(benches, benchmark_select_coin);
criterion_main!(benches);
90 changes: 90 additions & 0 deletions benches/benches_bnb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rust_coinselect::{
select_coin_bnb, CoinSelectionOpt, ExcessStrategy, OutputGroup, SelectionError, SelectionOutput,
};

fn benchmark_select_coin_bnb(c: &mut Criterion) {
let inputs = [
OutputGroup {
value: 55000,
weight: 500,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 400,
weight: 200,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 40000,
weight: 300,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 25000,
weight: 100,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 35000,
weight: 150,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 600,
weight: 250,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 30000,
weight: 120,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 5000,
weight: 50,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
];

let options = CoinSelectionOpt {
target_value: 5730,
target_feerate: 0.5, // Simplified feerate
long_term_feerate: None,
min_absolute_fee: 0,
base_weight: 10,
drain_weight: 50,
drain_cost: 10,
cost_per_input: 20,
cost_per_output: 10,
min_drain_value: 500,
excess_strategy: ExcessStrategy::ToDrain,
};

c.bench_function("select_coin_bnb", |b| {
b.iter(|| {
let result: Result<SelectionOutput, SelectionError> =
select_coin_bnb(black_box(&inputs), black_box(options));
let _ = black_box(result);
})
});
}

criterion_group!(benches, benchmark_select_coin_bnb);
criterion_main!(benches);
56 changes: 56 additions & 0 deletions benches/benches_fifo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rust_coinselect::{
select_coin_fifo, CoinSelectionOpt, ExcessStrategy, OutputGroup, SelectionError,
SelectionOutput,
};

fn benchmark_select_coin_fifo(c: &mut Criterion) {
let inputs = vec![
OutputGroup {
value: 1000,
weight: 100,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 2000,
weight: 200,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 3000,
weight: 300,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
];

let options = CoinSelectionOpt {
target_value: 2500,
target_feerate: 0.4,
long_term_feerate: Some(0.4),
min_absolute_fee: 0,
base_weight: 10,
drain_weight: 50,
drain_cost: 10,
cost_per_input: 20,
cost_per_output: 10,
min_drain_value: 500,
excess_strategy: ExcessStrategy::ToDrain,
};

c.bench_function("select_coin_fifo", |b| {
b.iter(|| {
let result: Result<SelectionOutput, SelectionError> =
select_coin_fifo(black_box(&inputs), black_box(options));
let _ = black_box(result);
})
});
}

criterion_group!(benches, benchmark_select_coin_fifo);
criterion_main!(benches);
73 changes: 73 additions & 0 deletions benches/benches_knapsack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rust_coinselect::{
select_coin_knapsack, CoinSelectionOpt, ExcessStrategy, OutputGroup, SelectionError,
SelectionOutput,
};

const CENT: f64 = 1000000.0;

fn knapsack_setup_output_groups(
value: Vec<u64>,
weights: Vec<u32>,
target_feerate: f32,
) -> Vec<OutputGroup> {
let mut inputs: Vec<OutputGroup> = Vec::new();
for (i, j) in value.into_iter().zip(weights.into_iter()) {
let k = i.saturating_add((j as f32 * target_feerate).ceil() as u64);
inputs.push(OutputGroup {
value: k,
weight: j,
input_count: 1,
is_segwit: false,
creation_sequence: None,
})
}
inputs
}

fn benchmark_select_coin_knapsack(c: &mut Criterion) {
let inputs = knapsack_setup_output_groups(
vec![
(6.0 * CENT).round() as u64,
(7.0 * CENT).round() as u64,
(8.0 * CENT).round() as u64,
(20.0 * CENT).round() as u64,
(30.0 * CENT).round() as u64,
],
vec![100, 200, 100, 10, 5],
0.77,
);

let options = {
let min_drain_value = 500;
let base_weight = 10;
let target_feerate = 0.56;
let adjusted_target = (37.0 * CENT).round() as u64;
let target_value =
adjusted_target - min_drain_value - (base_weight as f32 * target_feerate).ceil() as u64;
CoinSelectionOpt {
target_value,
target_feerate,
long_term_feerate: Some(0.4),
min_absolute_fee: 0,
base_weight,
drain_weight: 50,
drain_cost: 10,
cost_per_input: 20,
cost_per_output: 10,
min_drain_value,
excess_strategy: ExcessStrategy::ToDrain,
}
};

c.bench_function("select_coin_knapsack", |b| {
b.iter(|| {
let result: Result<SelectionOutput, SelectionError> =
select_coin_knapsack(black_box(&inputs), black_box(options));
let _ = black_box(result);
})
});
}

criterion_group!(benches, benchmark_select_coin_knapsack);
criterion_main!(benches);
Loading

0 comments on commit 32c3876

Please sign in to comment.