frame_benchmarking_cli/shared/
record.rs1use sc_cli::Result;
21use sc_service::Configuration;
22
23use log::info;
24use serde::Serialize;
25use std::{fs, path::PathBuf, time::Duration};
26
27use super::Stats;
28
29#[derive(Debug, Default, Clone, Serialize)]
31pub struct BenchRecord {
32 ns_per_size: Vec<(u64, u64)>,
34}
35
36impl BenchRecord {
37 pub fn append(&mut self, size: usize, d: Duration) -> Result<()> {
39 let size: u64 = size.try_into().map_err(|e| format!("Size overflow u64: {}", e))?;
40 let ns: u64 = d
41 .as_nanos()
42 .try_into()
43 .map_err(|e| format!("Nanoseconds overflow u64: {}", e))?;
44 self.ns_per_size.push((size, ns));
45 Ok(())
46 }
47
48 pub fn calculate_stats(self) -> Result<(Stats, Stats)> {
50 let (size, time): (Vec<_>, Vec<_>) = self.ns_per_size.into_iter().unzip();
51 let size = Stats::new(&size)?;
52 let time = Stats::new(&time)?;
53 Ok((time, size)) }
55
56 pub fn save_json(&self, cfg: &Configuration, out_path: &PathBuf, suffix: &str) -> Result<()> {
59 let mut path = PathBuf::from(out_path);
60 if path.is_dir() || path.as_os_str().is_empty() {
61 path.push(&format!("{}_{}", cfg.database, suffix).to_lowercase());
62 path.set_extension("json");
63 }
64
65 let json = serde_json::to_string_pretty(&self)
66 .map_err(|e| format!("Serializing as JSON: {:?}", e))?;
67
68 fs::write(&path, json)?;
69 info!("Raw data written to {:?}", fs::canonicalize(&path)?);
70 Ok(())
71 }
72}