passing cli argument added and run.sh modified accordingly

This commit is contained in:
Manish Kumar 2023-11-20 14:35:03 +05:30
parent e564b28e8e
commit 9fa3fe54f6
7 changed files with 82 additions and 34 deletions

View File

@ -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"

View File

@ -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();
// }

View File

@ -0,0 +1 @@
pub mod sha256_benchmarks;

View File

@ -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) {
});
});
}
pub fn sha256_benchmarks_10kb(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];
rand::thread_rng().fill_bytes(&mut data);
let input: Vec<u8> = 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);

View File

@ -21,9 +21,4 @@ pub fn sha_bench(input: Vec<u8>) {
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]);
}

View File

@ -0,0 +1,4 @@
fn main() {
}

View File

@ -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