Skip to content

Commit

Permalink
benches: add a Block insertion bench
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed Jan 28, 2022
1 parent a1a1968 commit 019a4b7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ edition = "2018"
name = "lookups"
harness = false

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

[dependencies]
snarkvm = { git = "https://github.com/AleoHQ/snarkVM.git", rev = "e0462bf" }
#snarkvm = { path = "../../snarkVM" }
Expand Down
58 changes: 58 additions & 0 deletions storage/benches/insertion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the snarkOS library.

// The snarkOS library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The snarkOS library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.

use snarkos_storage::{storage::rocksdb::RocksDB, LedgerState};
use snarkvm::{dpc::testnet2::Testnet2, prelude::Block};

use criterion::{criterion_group, criterion_main, Criterion};

use std::{fs, time::Duration};

const NUM_BLOCKS: usize = 1_000;

fn insertion(c: &mut Criterion) {
// Read the test blocks.
// note: the `blocks_100` and `blocks_1000` files were generated on a testnet2 storage using `LedgerState::dump_blocks`.
let mut test_blocks = fs::read(format!("benches/blocks_{}", NUM_BLOCKS)).expect(&format!("Missing the test blocks file"));
let blocks: Vec<Block<Testnet2>> = bincode::deserialize(&mut test_blocks).expect("Failed to deserialize a block dump");
assert_eq!(blocks.len(), NUM_BLOCKS - 1);

// Prepare a test ledger and an iterator of blocks to insert.
let temp_dir = tempfile::tempdir().expect("Failed to open temporary directory").into_path();
let ledger = LedgerState::open_writer::<RocksDB, _>(temp_dir).expect("Failed to initialize ledger");
let mut block_iter = blocks.iter();

c.bench_function("add_block", |b| {
b.iter(|| {
let next_block = if let Some(block) = block_iter.next() {
block
} else {
let _ = ledger.revert_to_block_height(0);
block_iter = blocks.iter();
block_iter.next().unwrap()
};
ledger.add_next_block(next_block).expect("Failed to add a test block");
})
});
}

criterion_group!(
name = benches;
// This benchmark needs a bit more time than the default 5s.
config = Criterion::default().measurement_time(Duration::from_secs(10));
targets = insertion
);
criterion_main!(benches);

0 comments on commit 019a4b7

Please sign in to comment.