|
| 1 | +use criterion::{criterion_group, criterion_main, BatchSize}; |
| 2 | + |
| 3 | +use criterion::{BenchmarkId, Criterion, SamplingMode}; |
| 4 | +use ergo_lib::chain::ergo_box::box_builder::ErgoBoxCandidateBuilder; |
| 5 | +use ergo_lib::{ |
| 6 | + chain::transaction::{unsigned::UnsignedTransaction, UnsignedInput}, |
| 7 | + wallet::{secret_key::SecretKey, signing::TransactionContext, Wallet}, |
| 8 | +}; |
| 9 | +use ergotree_ir::chain::ergo_box::box_value::BoxValue; |
| 10 | +use ergotree_ir::chain::tx_id::TxId; |
| 11 | +use ergotree_ir::mir::expr::Expr; |
| 12 | +use ergotree_ir::{ |
| 13 | + chain::{context_extension::ContextExtension, ergo_box::ErgoBox}, |
| 14 | + ergo_tree::{ErgoTree, ErgoTreeHeader}, |
| 15 | + mir::constant::Constant, |
| 16 | +}; |
| 17 | +use sigma_test_util::force_any_val; |
| 18 | + |
| 19 | +pub fn bench_tx_context(c: &mut Criterion) { |
| 20 | + let mut group = c.benchmark_group("TransactionContext::new scaling with inputs"); |
| 21 | + group.sample_size(10); |
| 22 | + let true_tree = ErgoTree::new(ErgoTreeHeader::v0(true), &Expr::Const(true.into())).unwrap(); |
| 23 | + |
| 24 | + for range in (1..=100).step_by(10) { |
| 25 | + let inputs = (0..range) |
| 26 | + .map(|i| { |
| 27 | + ErgoBox::from_box_candidate( |
| 28 | + &ErgoBoxCandidateBuilder::new(BoxValue::SAFE_USER_MIN, true_tree.clone(), 1000) |
| 29 | + .build() |
| 30 | + .unwrap(), |
| 31 | + TxId::zero(), |
| 32 | + i as u16, |
| 33 | + ) |
| 34 | + .unwrap() |
| 35 | + }) |
| 36 | + .collect::<Vec<ErgoBox>>(); |
| 37 | + let input_ids = inputs |
| 38 | + .iter() |
| 39 | + .map(|input| UnsignedInput::new(input.box_id(), ContextExtension::empty())) |
| 40 | + .collect(); |
| 41 | + let unsigned_tx = UnsignedTransaction::new_from_vec( |
| 42 | + input_ids, |
| 43 | + vec![], |
| 44 | + vec![ |
| 45 | + ErgoBoxCandidateBuilder::new(BoxValue::SAFE_USER_MIN, true_tree.clone(), 1000) |
| 46 | + .build() |
| 47 | + .unwrap(), |
| 48 | + ], |
| 49 | + ) |
| 50 | + .unwrap(); |
| 51 | + group.bench_with_input( |
| 52 | + BenchmarkId::from_parameter(format!("{range:4}")), |
| 53 | + &unsigned_tx, |
| 54 | + |b, tx| { |
| 55 | + b.iter_batched( |
| 56 | + || (tx.clone(), inputs.clone(), vec![]), |
| 57 | + |(tx, inputs, data_inputs)| { |
| 58 | + TransactionContext::new(tx, inputs, data_inputs).unwrap() |
| 59 | + }, |
| 60 | + BatchSize::SmallInput, |
| 61 | + ) |
| 62 | + }, |
| 63 | + ); |
| 64 | + } |
| 65 | + group.finish(); |
| 66 | +} |
| 67 | + |
| 68 | +pub fn bench_tx_sign_wallet(c: &mut Criterion) { |
| 69 | + let mut sign_group = c.benchmark_group("sign ProveDlog inputs"); |
| 70 | + sign_group |
| 71 | + .sample_size(10) |
| 72 | + .sampling_mode(SamplingMode::Flat) |
| 73 | + .warm_up_time(std::time::Duration::from_millis(50)); |
| 74 | + |
| 75 | + const MAX_INPUTS: usize = 100; |
| 76 | + let secrets = vec![SecretKey::random_dlog(); MAX_INPUTS]; |
| 77 | + let inputs = (0..MAX_INPUTS) |
| 78 | + .map(|i| { |
| 79 | + ErgoBox::from_box_candidate( |
| 80 | + &ErgoBoxCandidateBuilder::new( |
| 81 | + BoxValue::SAFE_USER_MIN, |
| 82 | + secrets[i].get_address_from_public_image().script().unwrap(), |
| 83 | + 1000, |
| 84 | + ) |
| 85 | + .build() |
| 86 | + .unwrap(), |
| 87 | + TxId::zero(), |
| 88 | + i as u16, |
| 89 | + ) |
| 90 | + .unwrap() |
| 91 | + }) |
| 92 | + .collect::<Vec<ErgoBox>>(); |
| 93 | + let wallet = Wallet::from_secrets(secrets); |
| 94 | + |
| 95 | + for range in (1..=MAX_INPUTS).step_by(20) { |
| 96 | + let input_ids = inputs |
| 97 | + .iter() |
| 98 | + .take(range) |
| 99 | + .map(|input| UnsignedInput::new(input.box_id(), ContextExtension::empty())) |
| 100 | + .collect(); |
| 101 | + let unsigned_tx = UnsignedTransaction::new_from_vec( |
| 102 | + input_ids, |
| 103 | + vec![], |
| 104 | + vec![ErgoBoxCandidateBuilder::new( |
| 105 | + BoxValue::SAFE_USER_MIN, |
| 106 | + ErgoTree::new( |
| 107 | + ErgoTreeHeader::new(0).unwrap(), |
| 108 | + &Constant::from(true).into(), |
| 109 | + ) |
| 110 | + .unwrap(), |
| 111 | + 1000, |
| 112 | + ) |
| 113 | + .build() |
| 114 | + .unwrap()], |
| 115 | + ) |
| 116 | + .unwrap(); |
| 117 | + let tx_context = |
| 118 | + TransactionContext::new(unsigned_tx, inputs[0..range].to_owned(), vec![]).unwrap(); |
| 119 | + sign_group.bench_with_input( |
| 120 | + BenchmarkId::from_parameter(format!("{range:4}")), |
| 121 | + &tx_context, |
| 122 | + |b, tx_context| { |
| 123 | + b.iter_batched( |
| 124 | + || (tx_context.clone(), force_any_val()), |
| 125 | + |(tx_context, state_context)| { |
| 126 | + wallet |
| 127 | + .sign_transaction(tx_context, &state_context, None) |
| 128 | + .unwrap() |
| 129 | + }, |
| 130 | + BatchSize::SmallInput, |
| 131 | + ) |
| 132 | + }, |
| 133 | + ); |
| 134 | + } |
| 135 | + sign_group.finish(); |
| 136 | +} |
| 137 | + |
| 138 | +criterion_group!(benches, bench_tx_context, bench_tx_sign_wallet); |
| 139 | +criterion_main!(benches); |
0 commit comments