diff options
Diffstat (limited to 'zkvms_host_io/src')
| -rw-r--r-- | zkvms_host_io/src/lib.rs | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/zkvms_host_io/src/lib.rs b/zkvms_host_io/src/lib.rs index b2049f5..da1e2df 100644 --- a/zkvms_host_io/src/lib.rs +++ b/zkvms_host_io/src/lib.rs @@ -5,7 +5,15 @@ pub use input_macros::{ }; use num_traits::NumCast; use serde::{Deserialize, Serialize}; -use std::{collections::*, env, fs::read_to_string, option::Option}; +use std::{ + collections::*, + env, + fs::{read_to_string, OpenOptions}, + option::Option, + time::{Duration, Instant}, + io::Write, + path::Path +}; static DEFAULT_PUBLIC_INPUT: &str = include_str!(concat!(env!("INPUTS_DIR"), "/default_public_input.toml")); @@ -143,3 +151,60 @@ pub fn read_args() -> RunWith { default_env, } } + +/// Used by the "benchmarkable" macro. Takes run_info and two vectors of start and +/// end instants for each benchmark iteration. +pub fn emit_benchmark_results(run_info: RunWith, starts: Vec<Instant>, ends: Vec<Instant>) { + let info_row = format!("name,guest,total duration,repeats,average\n"); + let mut output = format!("{},{},", env!("ZKVM"), env!("GUEST")); + + let duration = *ends.last().unwrap() - *starts.first().unwrap(); + if run_info.millis { + output += &format!("{},", duration.as_millis()); + } else { + output += &format!("{:.3},", duration.as_secs_f32()); + } + + let durations = starts + .into_iter() + .zip(ends.into_iter()) + .map(|(s,e)| e - s ) + .collect::<Vec<Duration>>(); + let average = durations.iter().sum::<Duration>() / durations.len() as u32; + + if run_info.millis { + output += &format!("{},{}\n", run_info.repeats, average.as_millis()); + } else { + output += &format!("{},{:.3}\n", run_info.repeats, average.as_secs_f32()); + } + + if let Some(file) = run_info.output_file { + + let file_exists = Path::new(&file).exists(); + + let mut outfile = match OpenOptions::new() + .write(true) + .create(true) + .append(run_info.append) + .open(&file) + { + Ok(file) => file, + Err(e) => { + panic!("Failed to open file: {}", e); + } + }; + + if !file_exists { + if let Err(e) = write!(outfile, "{}", info_row) { + panic!("Failed to write info_row: {}", e); + } + } + + if let Err(e) = write!(outfile, "{}", output) { + panic!("Failed to write output: {}", e); + } + } + else { + print!("{}", output); + } +} |
