|
| 1 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 2 | +use itertools::Itertools; |
| 3 | +use kaspa_consensus_core::{ |
| 4 | + muhash::MuHashExtensions, |
| 5 | + subnets::SUBNETWORK_ID_NATIVE, |
| 6 | + tx::{ScriptPublicKey, SignableTransaction, Transaction, TransactionInput, TransactionOutpoint, TransactionOutput, UtxoEntry}, |
| 7 | +}; |
| 8 | +use kaspa_hashes::TransactionID; |
| 9 | +use kaspa_muhash::MuHash; |
| 10 | +use kaspa_utils::iter::parallelism_in_power_steps; |
| 11 | +use rayon::prelude::*; |
| 12 | + |
| 13 | +fn generate_transaction(ins: usize, outs: usize, randomness: u64) -> SignableTransaction { |
| 14 | + let mut tx = Transaction::new(0, vec![], vec![], 0, SUBNETWORK_ID_NATIVE, 0, vec![]); |
| 15 | + let mut entries = vec![]; |
| 16 | + for i in 0..ins { |
| 17 | + let mut hasher = TransactionID::new(); |
| 18 | + hasher.write(i.to_le_bytes()); |
| 19 | + hasher.write(randomness.to_le_bytes()); |
| 20 | + let input = TransactionInput::new(TransactionOutpoint::new(hasher.finalize(), 0), vec![10; 66], 0, 1); |
| 21 | + let entry = UtxoEntry::new(22222222, ScriptPublicKey::from_vec(0, vec![99; 34]), 23456, false); |
| 22 | + tx.inputs.push(input); |
| 23 | + entries.push(entry); |
| 24 | + } |
| 25 | + for _ in 0..outs { |
| 26 | + let output = TransactionOutput::new(23456, ScriptPublicKey::from_vec(0, vec![101; 34])); |
| 27 | + tx.outputs.push(output); |
| 28 | + } |
| 29 | + tx.finalize(); |
| 30 | + SignableTransaction::with_entries(tx, entries) |
| 31 | +} |
| 32 | + |
| 33 | +pub fn parallel_muhash_benchmark(c: &mut Criterion) { |
| 34 | + let mut group = c.benchmark_group("muhash txs"); |
| 35 | + let txs = (0..256).map(|i| generate_transaction(2, 2, i)).collect_vec(); |
| 36 | + group.bench_function("seq", |b| { |
| 37 | + b.iter(|| { |
| 38 | + let mut mh = MuHash::new(); |
| 39 | + for tx in txs.iter() { |
| 40 | + mh.add_transaction(&tx.as_verifiable(), 222); |
| 41 | + } |
| 42 | + black_box(mh) |
| 43 | + }) |
| 44 | + }); |
| 45 | + |
| 46 | + for threads in parallelism_in_power_steps() { |
| 47 | + group.bench_function(format!("par {threads}"), |b| { |
| 48 | + let pool = rayon::ThreadPoolBuilder::new().num_threads(threads).build().unwrap(); |
| 49 | + b.iter(|| { |
| 50 | + pool.install(|| { |
| 51 | + let mh = |
| 52 | + txs.par_iter().map(|tx| MuHash::from_transaction(&tx.as_verifiable(), 222)).reduce(MuHash::new, |mut a, b| { |
| 53 | + a.combine(&b); |
| 54 | + a |
| 55 | + }); |
| 56 | + black_box(mh) |
| 57 | + }) |
| 58 | + }) |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + group.finish(); |
| 63 | +} |
| 64 | + |
| 65 | +criterion_group!(benches, parallel_muhash_benchmark); |
| 66 | +criterion_main!(benches); |
0 commit comments