Benchmark Block struct serialization code (#2018)

* Benchmark Block struct serialization code

* benchmark generated block

* add deserialize benchmark

* Fix block/tests.rs conditional module imports

* add large_single_transaction_block

* Fix name for deserialise benchmark

Co-authored-by: teor <teor@riseup.net>
This commit is contained in:
Kirill Fomichev 2021-04-19 15:15:02 +03:00 committed by GitHub
parent 5f36be7223
commit b636cf8ac1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 237 additions and 4 deletions

171
Cargo.lock generated
View File

@ -402,6 +402,18 @@ dependencies = [
"sha2",
]
[[package]]
name = "bstr"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a40b47ad93e1a5404e6c18dec46b628214fee441c70f4ab5d6942142cc268a3d"
dependencies = [
"lazy_static",
"memchr",
"regex-automata",
"serde",
]
[[package]]
name = "bumpalo"
version = "3.4.0"
@ -450,6 +462,15 @@ version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e9e01327e6c86e92ec72b1c798d4a94810f147209bbe3ffab6a86954937a6f"
[[package]]
name = "cast"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc38c385bfd7e444464011bb24820f40dd1c76bcdfa1b78611cb7c2e5cafab75"
dependencies = [
"rustc_version",
]
[[package]]
name = "cc"
version = "1.0.67"
@ -625,6 +646,42 @@ dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "criterion"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab327ed7354547cc2ef43cbe20ef68b988e70b4b593cbd66a2a61733123a3d23"
dependencies = [
"atty",
"cast",
"clap",
"criterion-plot",
"csv",
"itertools 0.10.0",
"lazy_static",
"num-traits",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_cbor",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e022feadec601fba1649cfa83586381a4ad31c6bf3a9ab7d408118b05dd9889d"
dependencies = [
"cast",
"itertools 0.9.0",
]
[[package]]
name = "crossbeam"
version = "0.7.3"
@ -748,6 +805,28 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "csv"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1"
dependencies = [
"bstr",
"csv-core",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "csv-core"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
dependencies = [
"memchr",
]
[[package]]
name = "ctor"
version = "0.1.16"
@ -1260,6 +1339,12 @@ dependencies = [
"tracing-futures",
]
[[package]]
name = "half"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62aca2aba2d62b4a7f5b33f3712cb1b0692779a56fb510499d5c0aa594daeaf3"
[[package]]
name = "hashbrown"
version = "0.9.1"
@ -1528,6 +1613,15 @@ version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47be2f14c678be2fdcab04ab1171db51b2762ce6f0a8ee87c8dd4a04ed216135"
[[package]]
name = "itertools"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"
dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.10.0"
@ -1941,6 +2035,12 @@ version = "1.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"
[[package]]
name = "oorandom"
version = "11.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
[[package]]
name = "opaque-debug"
version = "0.3.0"
@ -2095,6 +2195,34 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "plotters"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45ca0ae5f169d0917a7c7f5a9c1a3d3d9598f18f529dd2b8373ed988efea307a"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b07fffcddc1cb3a1de753caa4e4df03b79922ba43cf882acc1bdd7e8df9f4590"
[[package]]
name = "plotters-svg"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b38a02e23bd9604b842a812063aec4ef702b57989c37b655254bb61c471ad211"
dependencies = [
"plotters-backend",
]
[[package]]
name = "ppv-lite86"
version = "0.2.10"
@ -2622,6 +2750,15 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scoped-tls"
version = "1.0.0"
@ -2792,6 +2929,16 @@ dependencies = [
"serde_derive",
]
[[package]]
name = "serde_cbor"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622"
dependencies = [
"half",
"serde",
]
[[package]]
name = "serde_derive"
version = "1.0.125"
@ -3142,6 +3289,16 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "tinytemplate"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "tinyvec"
version = "1.0.1"
@ -3724,6 +3881,17 @@ dependencies = [
"libc",
]
[[package]]
name = "walkdir"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi 0.3.9",
"winapi-util",
]
[[package]]
name = "want"
version = "0.3.0"
@ -3982,12 +4150,13 @@ dependencies = [
"byteorder",
"chrono",
"color-eyre",
"criterion",
"displaydoc",
"ed25519-zebra",
"equihash",
"futures 0.3.14",
"hex",
"itertools",
"itertools 0.10.0",
"jubjub",
"lazy_static",
"primitive-types",

View File

@ -11,6 +11,7 @@ edition = "2018"
default = []
proptest-impl = ["proptest", "proptest-derive"]
bench = ["zebra-test"]
[dependencies]
bech32 = "0.8.0"
@ -44,10 +45,13 @@ proptest-derive = { version = "0.3.0", optional = true }
ed25519-zebra = "2.2.0"
redjubjub = "0.4"
zebra-test = { path = "../zebra-test/", optional = true }
[dev-dependencies]
bincode = "1"
color-eyre = "0.5.11"
criterion = { version = "0.3", features = ["html_reports"] }
spandoc = "0.2"
tracing = "0.1.25"
@ -57,3 +61,7 @@ proptest = "0.10"
proptest-derive = "0.3"
zebra-test = { path = "../zebra-test/" }
[[bench]]
name = "block"
harness = false

View File

@ -0,0 +1,52 @@
use std::io::Cursor;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use zebra_chain::{
block::{
tests::generate::{large_multi_transaction_block, large_single_transaction_block},
Block,
},
serialization::{ZcashDeserialize, ZcashSerialize},
};
use zebra_test::vectors::BLOCK_TESTNET_141042_BYTES;
fn block_serialization(c: &mut Criterion) {
// Biggest block from `zebra-test`.
let block141042_bytes: &[u8] = BLOCK_TESTNET_141042_BYTES.as_ref();
let block141042 = Block::zcash_deserialize(Cursor::new(block141042_bytes)).unwrap();
let blocks = vec![
("BLOCK_TESTNET_141042", block141042),
(
"large_multi_transaction_block",
large_multi_transaction_block(),
),
(
"large_single_transaction_block",
large_single_transaction_block(),
),
];
for (name, block) in blocks {
c.bench_with_input(
BenchmarkId::new("zcash_serialize_to_vec", name),
&block,
|b, block| b.iter(|| block.zcash_serialize_to_vec().unwrap()),
);
let block_bytes = block.zcash_serialize_to_vec().unwrap();
c.bench_with_input(
BenchmarkId::new("zcash_deserialize", name),
&block_bytes,
|b, bytes| b.iter(|| Block::zcash_deserialize(Cursor::new(bytes)).unwrap()),
);
}
}
criterion_group!(
name = benches;
config = Criterion::default().noise_threshold(0.05);
targets = block_serialization
);
criterion_main!(benches);

View File

@ -11,8 +11,8 @@ pub mod merkle;
#[cfg(any(test, feature = "proptest-impl"))]
mod arbitrary;
#[cfg(test)]
mod tests;
#[cfg(any(test, feature = "bench"))]
pub mod tests;
use std::fmt;

View File

@ -1,5 +1,9 @@
// XXX generate should be rewritten as strategies
mod generate;
#[cfg(any(test, feature = "bench"))]
pub mod generate;
#[cfg(test)]
mod preallocate;
#[cfg(test)]
mod prop;
#[cfg(test)]
mod vectors;