From 9fa3fe54f67859789d37b1e2bf722f76401ca46a Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Mon, 20 Nov 2023 14:35:03 +0530 Subject: [PATCH] passing cli argument added and run.sh modified accordingly --- hash/risc0/bench/host/Cargo.toml | 9 ++-- hash/risc0/bench/host/benches/bench_main.rs | 44 +++++++++++++++++++ .../bench/host/benches/benchmarks/mod.rs | 1 + .../{ => benchmarks}/sha256_benchmarks.rs | 36 +++++++-------- hash/risc0/bench/host/src/lib.rs | 7 +-- hash/risc0/bench/host/src/main.rs | 4 ++ hash/risc0/bench/run.sh | 15 ++++++- 7 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 hash/risc0/bench/host/benches/bench_main.rs create mode 100644 hash/risc0/bench/host/benches/benchmarks/mod.rs rename hash/risc0/bench/host/benches/{ => benchmarks}/sha256_benchmarks.rs (53%) create mode 100644 hash/risc0/bench/host/src/main.rs diff --git a/hash/risc0/bench/host/Cargo.toml b/hash/risc0/bench/host/Cargo.toml index c28882c..1747bba 100644 --- a/hash/risc0/bench/host/Cargo.toml +++ b/hash/risc0/bench/host/Cargo.toml @@ -8,14 +8,11 @@ methods = { path = "../methods" } risc0-zkvm = { version = "0.19.0" } serde = "1.0" rand = "0.8.5" +clap = {version = "4.4.8", features = [ "derive" ]} [dev-dependencies] -criterion = "0.5.1" +criterion = {version ="0.5.1", default-features = false} [[bench]] -name = "sha256_benchmarks" +name = "bench_main" harness = false - -[[bin]] -name = "host" -path = "src/lib.rs" \ No newline at end of file diff --git a/hash/risc0/bench/host/benches/bench_main.rs b/hash/risc0/bench/host/benches/bench_main.rs new file mode 100644 index 0000000..f6b5634 --- /dev/null +++ b/hash/risc0/bench/host/benches/bench_main.rs @@ -0,0 +1,44 @@ +use criterion::Criterion; +// use clap::Parser; +// use std::env; +mod benchmarks; + +fn main() { + let mut criterion: criterion::Criterion<_> = (Criterion::default().sample_size(10)).configure_from_args(); + match std::env::args().skip(1).next() { + Some(arg) => { + match arg.as_str() { + "1" => benchmarks::sha256_benchmarks::sha256_benchmarks_1kb(&mut criterion), + "2" => benchmarks::sha256_benchmarks::sha256_benchmarks_2kb(&mut criterion), + "10" => benchmarks::sha256_benchmarks::sha256_benchmarks_10kb(&mut criterion), + _ => eprintln!("Invalid benchmark argument: {}", arg), + } + } + None => {eprintln!("No benchmark")} + } + + criterion::Criterion::default().configure_from_args().final_summary(); + } + + +// #[derive(Parser)] +// #[command(author, version, about, long_about = None)] +// struct Args { +// #[clap(long, short)] +// run_benchmark_function_one: bool, +// #[clap(long, short)] +// run_benchmark_function_two: bool, +// } + +// fn main(){ +// let args: Args = Args::parse(); +// let mut criterion: criterion::Criterion<_> = (Criterion::default().sample_size(10)).configure_from_args(); + +// if args.run_benchmark_function_one { +// benchmarks::sha256_benchmarks::sha256_benchmarks1(&mut criterion); +// } +// if args.run_benchmark_function_two { +// benchmarks::sha256_benchmarks22::sha256_benchmarks123(&mut criterion); +// } +// criterion::Criterion::default().configure_from_args().final_summary(); +// } diff --git a/hash/risc0/bench/host/benches/benchmarks/mod.rs b/hash/risc0/bench/host/benches/benchmarks/mod.rs new file mode 100644 index 0000000..006d0a4 --- /dev/null +++ b/hash/risc0/bench/host/benches/benchmarks/mod.rs @@ -0,0 +1 @@ +pub mod sha256_benchmarks; \ No newline at end of file diff --git a/hash/risc0/bench/host/benches/sha256_benchmarks.rs b/hash/risc0/bench/host/benches/benchmarks/sha256_benchmarks.rs similarity index 53% rename from hash/risc0/bench/host/benches/sha256_benchmarks.rs rename to hash/risc0/bench/host/benches/benchmarks/sha256_benchmarks.rs index c53e015..0bd640b 100644 --- a/hash/risc0/bench/host/benches/sha256_benchmarks.rs +++ b/hash/risc0/bench/host/benches/benchmarks/sha256_benchmarks.rs @@ -1,9 +1,9 @@ - use host::sha_bench; -use criterion::{criterion_group, criterion_main, Criterion}; +use criterion::Criterion; use rand::RngCore; -fn sha256_benchmarks(c: &mut Criterion) { - c.bench_function("Benchmarking sha256 on 1KB of random data:", |b| { + +pub fn sha256_benchmarks_1kb(c: &mut Criterion) { + c.bench_function(" sha256 on 1KB of random data:", |b| { //generating 1kb of random data in a vector of u8 let mut data = [0u8; 64]; rand::thread_rng().fill_bytes(&mut data); @@ -14,8 +14,11 @@ fn sha256_benchmarks(c: &mut Criterion) { sha_bench(input.clone()); }); }); +} - c.bench_function("Benchmarking sha256 on 2KB of random data:", |b| { +pub fn sha256_benchmarks_2kb(c: &mut Criterion) { + + c.bench_function(" sha256 on 2KB of random data:", |b| { //generating 2kb of random data in a vector of u8 let mut data = [0u8; 128]; rand::thread_rng().fill_bytes(&mut data); @@ -27,26 +30,19 @@ fn sha256_benchmarks(c: &mut Criterion) { }); }); +} - c.bench_function("Benchmarking sha256 on 10KB of random data:", |b| { - //generating 10kb of random data in a vector of u8 - let mut data = [0u8; 1280]; +pub fn sha256_benchmarks_10kb(c: &mut Criterion) { + + //generating 10kb of random data in a vector of u8 + let mut data = [0u8; 1280]; rand::thread_rng().fill_bytes(&mut data); let input: Vec = data.to_vec(); + c.bench_function(" sha256 on 10KB of random data:", |b| { + b.iter(|| { sha_bench(input.clone()); }); }); -} - -criterion_group!( - name = benches; - - // Setting the sample size to 10 for quick benchmarks - // Becuase running default number of samples(100) takes a lot of time - config = Criterion::default().sample_size(10); - targets = sha256_benchmarks -); - -criterion_main!(benches); +} \ No newline at end of file diff --git a/hash/risc0/bench/host/src/lib.rs b/hash/risc0/bench/host/src/lib.rs index 3db39b1..5a25047 100644 --- a/hash/risc0/bench/host/src/lib.rs +++ b/hash/risc0/bench/host/src/lib.rs @@ -21,9 +21,4 @@ pub fn sha_bench(input: Vec) { receipt.verify(METHOD_ID).unwrap(); } -#[allow(dead_code)] -fn main() { - // This is a dummy call for the sha256 execution. - // Benchmarking does not depend on this. - sha_bench(vec![97, 98, 99, 100, 101, 102, 103, 104]); -} + diff --git a/hash/risc0/bench/host/src/main.rs b/hash/risc0/bench/host/src/main.rs new file mode 100644 index 0000000..3033f62 --- /dev/null +++ b/hash/risc0/bench/host/src/main.rs @@ -0,0 +1,4 @@ + +fn main() { + +} diff --git a/hash/risc0/bench/run.sh b/hash/risc0/bench/run.sh index fb14795..b9fc453 100755 --- a/hash/risc0/bench/run.sh +++ b/hash/risc0/bench/run.sh @@ -1,4 +1,15 @@ #!/bin/bash -# Run the 'cargo bench' command. -cargo bench +# Check if ZKBENCH_INPUT_SIZE_KB is set, otherwise set a default value +ZKBENCH_INPUT_SIZE_KB=${ZKBENCH_INPUT_SIZE_KB:-"1"} # Default to 1 if not set + +# Run benchmarks with the specified input size +if [ "$ZKBENCH_INPUT_SIZE_KB" = "1" ]; then + cargo bench --bench bench_main -- 1 +elif [ "$ZKBENCH_INPUT_SIZE_KB" = "2" ]; then + cargo bench --bench bench_main -- 2 +elif [ "$ZKBENCH_INPUT_SIZE_KB" = "10" ]; then + cargo bench --bench bench_main -- 10 +else + echo "Invalid input size: $ZKBENCH_INPUT_SIZE_KB" +fi