Skip to content

Commit

Permalink
feat: add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
cernicc committed Jun 11, 2024
1 parent 30bd7b1 commit 6440ee5
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 2 deletions.
95 changes: 95 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ cid = { version = "0.11.1" }
clap = { version = "4.5.3" }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
color-print = "0.3.4"
criterion = "0.5.1"
digest = "0.10.7"
futures = "0.3.28"
hex-literal = { version = "0.4.1" }
Expand Down
7 changes: 6 additions & 1 deletion storage/mater/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async-stream.workspace = true
bitflags.workspace = true
byteorder = { workspace = true, features = ["i128"] }
bytes.workspace = true
criterion = { workspace = true, features = ["async_tokio", "html_reports"] }
digest.workspace = true
futures.workspace = true
indexmap.workspace = true
Expand All @@ -23,7 +24,7 @@ serde = { workspace = true, features = ["derive"] }
serde_ipld_dagcbor.workspace = true
sha2.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["fs", "macros", "rt"] }
tokio = { workspace = true, features = ["fs", "macros", "rt", "rt-multi-thread"] }
tokio-stream.workspace = true
tokio-util = { workspace = true, features = ["io"] }

Expand All @@ -33,3 +34,7 @@ tempfile.workspace = true

[lints]
workspace = true

[[bench]]
harness = false
name = "benchmark"
70 changes: 70 additions & 0 deletions storage/mater/benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::io::Cursor;

use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use mater::Blockstore;
use tokio::{fs::File, runtime::Runtime as TokioExecutor};

// Read content to a Blockstore. This function is benchmarked.
async fn read_content(content: &[u8], mut store: Blockstore) {
let cursor = Cursor::new(content);
store.read(cursor).await.unwrap()
}

fn read_file(c: &mut Criterion) {
let paths = [
"tests/fixtures/original/lorem.txt",
"tests/fixtures/original/lorem_1024.txt",
"tests/fixtures/original/lorem_4096_dup.txt",
"tests/fixtures/original/spaceglenda.jpg",
];

for path in paths.iter() {
let content = std::fs::read(path).unwrap();

c.bench_with_input(
BenchmarkId::new("read_file", path),
&content,
|b, contents| {
b.to_async(TokioExecutor::new().unwrap())
.iter(|| read_content(&contents, Blockstore::new()));
},
);
}
}

// Write content from a Blockstore. This function is benchmarked.
async fn write_contents(buffer: Vec<u8>, store: Blockstore) {
store.write(buffer).await.unwrap();
}

fn write_file(c: &mut Criterion) {
let paths = [
"tests/fixtures/original/lorem.txt",
"tests/fixtures/original/lorem_1024.txt",
"tests/fixtures/original/lorem_4096_dup.txt",
"tests/fixtures/original/spaceglenda.jpg",
];

let runtime = TokioExecutor::new().unwrap();
for path in paths.iter() {
let mut blockstore = Blockstore::new();

// Read file contents to the blockstore
runtime.block_on(async {
let file = File::open(path).await.unwrap();
blockstore.read(file).await.unwrap()
});

c.bench_with_input(BenchmarkId::new("write_file", path), &(), |b, _: &()| {
b.to_async(TokioExecutor::new().unwrap()).iter_batched(
|| (blockstore.clone(), Vec::with_capacity(1024 * 1000)),
|(blockstore, buffer)| write_contents(buffer, blockstore),
BatchSize::SmallInput,
);
});
}
}

criterion_group!(bench_reading, read_file);
criterion_group!(bench_writing, write_file);
criterion_main!(bench_reading, bench_writing);
2 changes: 1 addition & 1 deletion storage/mater/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! Both version 1 and version 2 are supported.
//!
//! You can make use of the lower-level utilities such as [`CarV2Reader`] to read a CARv2 file,
//! though these utilies were designed to be used in higher-level abstractions, like the [`Blockstore`].
//! though these utilities were designed to be used in higher-level abstractions, like the [`Blockstore`].
#![warn(unused_crate_dependencies)]
#![warn(missing_docs)]
Expand Down
1 change: 1 addition & 0 deletions storage/mater/src/stores/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
/// The store keeps track of ([`Cid`], [`Bytes`]) pairs, performing de-duplication based on the [`Cid`].
///
/// **Important note: currently, the blockstore only supports a single file!**
#[derive(Debug, Clone)]
pub struct Blockstore {
root: Option<Cid>,
blocks: IndexMap<Cid, Bytes>,
Expand Down

0 comments on commit 6440ee5

Please sign in to comment.