diff --git a/Cargo.toml b/Cargo.toml index 227b2a5..2db5f75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ futures = "0.3" clap = { version = "4.2.7", features = ["derive"] } color-eyre = "0.6.2" config = "0.13.3" -dotenv = "0.15.0" +dotenvy = "0.15.7" serde = "1.0.163" serde_derive = "1.0.163" serde_json = { version = "1.0.96", features = ["preserve_order"] } @@ -33,4 +33,3 @@ rand = { version = "0.8.5", features = ["rand_chacha"] } lazy_static = "1.4.0" colored = "2.0.4" sysinfo = "0.29.8" -statrs = "0.16.0" diff --git a/src/actions/shoot.rs b/src/actions/shoot.rs index 91b9c3e..b6d17b5 100644 --- a/src/actions/shoot.rs +++ b/src/actions/shoot.rs @@ -207,7 +207,7 @@ impl GatlingShooter { std::fs::create_dir_all(&self.config.report.reports_dir)?; let writer = std::fs::File::create(report_path)?; - serde_json::to_writer(writer, &report.to_json()?)?; + serde_json::to_writer(writer, &report.to_json())?; } Ok(()) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 4b549d3..04d2f5e 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -3,7 +3,7 @@ // Imports use clap::{Args, Parser, Subcommand}; -const VERSION_STRING: &str = concat!(env!("CARGO_PKG_VERSION")); +const VERSION_STRING: &str = env!("CARGO_PKG_VERSION"); /// Main CLI struct #[derive(Parser, Debug)] diff --git a/src/main.rs b/src/main.rs index 9ef2672..56e7d06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ extern crate log; use clap::Parser; use color_eyre::eyre::Result; -use dotenv::dotenv; +use dotenvy::dotenv; use gatling::{ actions::shoot::shoot, cli::{Cli, Command}, diff --git a/src/metrics.rs b/src/metrics.rs index 2a04fdf..f2c6a36 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -1,11 +1,10 @@ -use crate::utils::{get_num_tx_per_block, SYSINFO}; +use crate::utils::{get_num_tx_per_block, SysInfo, SYSINFO}; use color_eyre::Result; use serde_json::{json, Value}; use starknet::providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider}; -use statrs::statistics::Statistics; -use std::{fmt, sync::Arc}; +use std::{fmt, ops::Deref, sync::Arc}; pub const BLOCK_TIME: u64 = 6; @@ -51,12 +50,9 @@ impl BenchmarkReport { pub async fn from_block_range<'a>( starknet_rpc: Arc>, name: String, - start_block: u64, - end_block: u64, + mut start_block: u64, + mut end_block: u64, ) -> Result { - let mut start_block = start_block; - let mut end_block = end_block; - // Whenever possible, skip the first and last blocks from the metrics // to make sure all the blocks used for calculating metrics are full if end_block - start_block > 2 { @@ -85,53 +81,59 @@ impl BenchmarkReport { Ok(BenchmarkReport { name, metrics }) } - pub fn to_json(&self) -> Result { - let sysinfo_string = format!( - "CPU Count: {}\n\ - CPU Model: {}\n\ - CPU Speed (MHz): {}\n\ - Total Memory: {} GB\n\ - Platform: {}\n\ - Release: {}\n\ - Architecture: {}", - SYSINFO.cpu_count, - SYSINFO.cpu_frequency, - SYSINFO.cpu_brand, - SYSINFO.memory / (1024 * 1024 * 1024), - SYSINFO.os_name, - SYSINFO.kernel_version, - SYSINFO.arch - ); + pub fn to_json(&self) -> Value { + let SysInfo { + os_name, + kernel_version, + arch, + cpu_count, + cpu_frequency, + cpu_brand, + memory, + } = SYSINFO.deref(); - let mut report = vec![]; - - for metric in self.metrics.iter() { - report.push(json!({ - "name": metric.name, - "unit": metric.unit, - "value": metric.value, - "extra": sysinfo_string - })); - } + let gigabyte_memory = memory / (1024 * 1024 * 1024); - let report_json = serde_json::to_value(report)?; + let sysinfo_string = format!( + "CPU Count: {cpu_count}\n\ + CPU Model: {cpu_brand}\n\ + CPU Speed (MHz): {cpu_frequency}\n\ + Total Memory: {gigabyte_memory} GB\n\ + Platform: {os_name}\n\ + Release: {kernel_version}\n\ + Architecture: {arch}", + ); - Ok(report_json) + self.metrics + .iter() + .map(|metric| { + json!({ + "name": metric.name, + "unit": metric.unit, + "value": metric.value, + "extra": sysinfo_string + }) + }) + .collect::() } } impl fmt::Display for MetricResult { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}: {} {}", self.name, self.value, self.unit) + let Self { name, value, unit } = self; + + write!(f, "{name}: {value} {unit}") } } impl fmt::Display for BenchmarkReport { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - writeln!(f, "Benchmark Report: {}", self.name)?; + let Self { name, metrics } = self; + + writeln!(f, "Benchmark Report: {name}")?; - for metric in &self.metrics { - writeln!(f, "{}", metric)?; + for metric in metrics { + writeln!(f, "{metric}")?; } Ok(()) @@ -143,7 +145,7 @@ fn average_tps(num_tx_per_block: &[u64]) -> f64 { } fn average_tpb(num_tx_per_block: &[u64]) -> f64 { - num_tx_per_block.iter().map(|x| *x as f64).mean() + num_tx_per_block.iter().sum::() as f64 / num_tx_per_block.len() as f64 } pub fn compute_all_metrics(num_tx_per_block: Vec) -> Vec { diff --git a/src/utils.rs b/src/utils.rs index 5b30b27..51904e3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -92,16 +92,26 @@ impl Default for SysInfo { impl fmt::Display for SysInfo { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let Self { + os_name, + kernel_version, + arch, + cpu_count, + cpu_frequency, + cpu_brand, + memory, + } = self; + + let cpu_ghz_freq = *cpu_frequency as f64 / 1000.0; + let gigabyte_memory = memory / (1024 * 1024 * 1024); + writeln!( f, - "System Information:\nSystem : {} Kernel Version {}\nArch : {}\nCPU : {} {:.2}GHz {} cores\nMemory : {} GB", - self.os_name, - self.kernel_version, - self.arch, - self.cpu_brand, - format!("{:.2} GHz", self.cpu_frequency as f64 / 1000.0), - self.cpu_count, - self.memory / (1024 * 1024 * 1024) + "System Information:\n\ + System : {os_name} Kernel Version {kernel_version}\n\ + Arch : {arch}\n\ + CPU : {cpu_brand} {cpu_ghz_freq:.2} GHz {cpu_count} cores\n\ + Memory : {gigabyte_memory} GB" ) } }