Skip to content

Commit

Permalink
bench: Add digest overhead benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Nov 21, 2023
1 parent 7c3534a commit 9cf26b7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ name = "agreement"
harness = false
path = "agreement.rs"

[[bench]]
name = "digest"
harness = false
path = "digest.rs"

[[bench]]
name = "ecdsa"
harness = false
Expand Down
49 changes: 49 additions & 0 deletions bench/digest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2023 Brian Smith.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#![allow(missing_docs)]

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use ring::digest;

static ALGORITHMS: &[(&str, &digest::Algorithm)] = &[
("sha256", &digest::SHA256),
("sha384", &digest::SHA384),
("sha512", &digest::SHA512),
];

const INPUT_LENGTHS: &[usize] = &[
// Benchmark that emphasizes overhead.
0,
];

fn oneshot(c: &mut Criterion) {
for &(alg_name, algorithm) in ALGORITHMS {
for input_len in INPUT_LENGTHS {
c.bench_with_input(
BenchmarkId::new(format!("digest::oneshot::{alg_name}"), 0),
input_len,
|b, &input_len| {
let input = vec![0u8; input_len];
b.iter(|| -> usize {
let digest = digest::digest(algorithm, &input);
black_box(digest.as_ref().len())
})
},
);
}
}
}

criterion_group!(digest, oneshot);
criterion_main!(digest);

0 comments on commit 9cf26b7

Please sign in to comment.