Merge pull request #418 from elichai/2020-03-benchmarks

Add benchmarks
This commit is contained in:
Andrew Poelstra 2020-10-08 14:05:04 +00:00 committed by GitHub
commit 845fe5ae4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 135 additions and 0 deletions

File diff suppressed because one or more lines are too long

View File

@ -1244,3 +1244,60 @@ mod tests {
} }
} }
} }
#[cfg(all(test, feature = "unstable"))]
mod benches {
use super::Transaction;
use EmptyWrite;
use consensus::{deserialize, Encodable};
use hashes::hex::FromHex;
use test::{black_box, Bencher};
const SOME_TX: &'static str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
#[bench]
pub fn bench_block_get_size(bh: &mut Bencher) {
let raw_tx = Vec::from_hex(SOME_TX).unwrap();
let mut tx: Transaction = deserialize(&raw_tx).unwrap();
bh.iter(|| {
black_box(black_box(&mut tx).get_size());
});
}
#[bench]
pub fn bench_block_serialize(bh: &mut Bencher) {
let raw_tx = Vec::from_hex(SOME_TX).unwrap();
let tx: Transaction = deserialize(&raw_tx).unwrap();
let mut data = Vec::with_capacity(raw_tx.len());
bh.iter(|| {
let result = tx.consensus_encode(&mut data);
black_box(&result);
data.clear();
});
}
#[bench]
pub fn bench_block_serialize_logic(bh: &mut Bencher) {
let raw_tx = Vec::from_hex(SOME_TX).unwrap();
let tx: Transaction = deserialize(&raw_tx).unwrap();
bh.iter(|| {
let size = tx.consensus_encode(&mut EmptyWrite);
black_box(&size);
});
}
#[bench]
pub fn bench_block_deserialize(bh: &mut Bencher) {
let raw_tx = Vec::from_hex(SOME_TX).unwrap();
bh.iter(|| {
let tx: Transaction = deserialize(&raw_tx).unwrap();
black_box(&tx);
});
}
}

View File

@ -99,3 +99,33 @@ pub use util::amount::SignedAmount;
pub use util::key::PrivateKey; pub use util::key::PrivateKey;
pub use util::key::PublicKey; pub use util::key::PublicKey;
pub use util::merkleblock::MerkleBlock; pub use util::merkleblock::MerkleBlock;
#[cfg(all(test, feature = "unstable"))] use tests::EmptyWrite;
#[cfg(all(test, feature = "unstable"))]
mod tests {
use hashes::core::fmt::Arguments;
use std::io::{IoSlice, Result, Write};
#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct EmptyWrite;
impl Write for EmptyWrite {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
Ok(buf.len())
}
fn write_vectored(&mut self, bufs: &[IoSlice]) -> Result<usize> {
Ok(bufs.iter().map(|s| s.len()).sum())
}
fn flush(&mut self) -> Result<()> {
Ok(())
}
fn write_all(&mut self, _: &[u8]) -> Result<()> {
Ok(())
}
fn write_fmt(&mut self, _: Arguments) -> Result<()> {
Ok(())
}
}
}