From 9cf26b7f69e3f66b26756fb77e6ac212c8361e0a Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Tue, 21 Nov 2023 11:13:42 -0800 Subject: [PATCH] bench: Add digest overhead benchmarks. --- bench/Cargo.toml | 5 +++++ bench/digest.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 bench/digest.rs diff --git a/bench/Cargo.toml b/bench/Cargo.toml index c647968da6..c93b09b364 100644 --- a/bench/Cargo.toml +++ b/bench/Cargo.toml @@ -20,6 +20,11 @@ name = "agreement" harness = false path = "agreement.rs" +[[bench]] +name = "digest" +harness = false +path = "digest.rs" + [[bench]] name = "ecdsa" harness = false diff --git a/bench/digest.rs b/bench/digest.rs new file mode 100644 index 0000000000..93038ccc0c --- /dev/null +++ b/bench/digest.rs @@ -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);