Skip to content

Commit 3ec1d65

Browse files
committed
Add benchmark for encoding
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
1 parent 7a69337 commit 3ec1d65

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

Cargo.toml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
[package]
22
name = "multihash"
3-
description = "Implementation of the multihash format"
4-
repository = "https://github.com/multiformats/rust-multihash"
5-
6-
keywords = ["multihash", "ipfs"]
7-
83
version = "0.9.4"
9-
104
authors = ["dignifiedquire <dignifiedquire@gmail.com>"]
11-
5+
edition = "2018"
126
license = "MIT"
13-
147
readme = "README.md"
15-
8+
description = "Implementation of the multihash format"
169
documentation = "https://docs.rs/multihash/"
17-
18-
edition = "2018"
10+
repository = "https://github.com/multiformats/rust-multihash"
11+
keywords = ["multihash", "ipfs"]
1912

2013
[dependencies]
2114
blake2b_simd = { version = "0.5.9", default-features = false }
@@ -26,4 +19,10 @@ tiny-keccak = "1.4"
2619
unsigned-varint = "0.3"
2720

2821
[dev-dependencies]
22+
criterion = "0.3"
23+
rand = "0.7"
2924
quickcheck = "0.9.2"
25+
26+
[[bench]]
27+
name = "multihash"
28+
harness = false

benches/multihash.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2+
use rand::Rng;
3+
4+
use multihash::{encode, Hash};
5+
6+
macro_rules! group_encode {
7+
($criterion:ident, $( $id:expr => $hash:expr, $input:expr)* ) => {{
8+
let mut group = $criterion.benchmark_group("encode");
9+
$(
10+
group.bench_function($id, |b| {
11+
b.iter(|| {
12+
let _ = black_box(encode($hash, $input).unwrap());
13+
})
14+
});
15+
)*
16+
group.finish();
17+
}};
18+
}
19+
20+
fn bench_encode(c: &mut Criterion) {
21+
let mut rng = rand::thread_rng();
22+
let data: Vec<u8> = (0..1024).map(|_| rng.gen()).collect();
23+
group_encode!(c,
24+
"identity" => Hash::Identity, &data
25+
"sha1" => Hash::SHA1, &data
26+
"sha2_256" => Hash::SHA2256, &data
27+
"sha2_512" => Hash::SHA2512, &data
28+
"sha3_224" => Hash::SHA3224, &data
29+
"sha3_256" => Hash::SHA3256, &data
30+
"sha3_384" => Hash::SHA3384, &data
31+
"keccak_224" => Hash::Keccak224, &data
32+
"keccak_256" => Hash::Keccak256, &data
33+
"keccak_384" => Hash::Keccak384, &data
34+
"keccak_512" => Hash::Keccak512, &data
35+
"blake2b_256" => Hash::Blake2b256, &data
36+
"blake2b_512" => Hash::Blake2b512, &data
37+
"blake2s_128" => Hash::Blake2s128, &data
38+
"blake2s_256" => Hash::Blake2s256, &data
39+
);
40+
}
41+
42+
criterion_group!(benches, bench_encode);
43+
criterion_main!(benches);

0 commit comments

Comments
 (0)