Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependancy Changes and Minor refactors #36

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"
2 changes: 1 addition & 1 deletion src/actions/shoot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
86 changes: 44 additions & 42 deletions src/metrics.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -51,12 +50,9 @@ impl BenchmarkReport {
pub async fn from_block_range<'a>(
starknet_rpc: Arc<JsonRpcClient<HttpTransport>>,
name: String,
start_block: u64,
end_block: u64,
mut start_block: u64,
mut end_block: u64,
) -> Result<BenchmarkReport> {
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 {
Expand Down Expand Up @@ -85,53 +81,59 @@ impl BenchmarkReport {
Ok(BenchmarkReport { name, metrics })
}

pub fn to_json(&self) -> Result<Value> {
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::<Value>()
}
}

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(())
Expand All @@ -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::<u64>() as f64 / num_tx_per_block.len() as f64
}

pub fn compute_all_metrics(num_tx_per_block: Vec<u64>) -> Vec<MetricResult> {
Expand Down
26 changes: 18 additions & 8 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}
}
Expand Down