frame_benchmarking_cli/shared/
mod.rs1pub mod genesis_state;
21pub mod record;
22pub mod stats;
23pub mod weight_params;
24
25pub use record::BenchRecord;
26pub use stats::{StatSelect, Stats};
27pub use weight_params::WeightParams;
28
29use clap::Args;
30use rand::prelude::*;
31use sc_sysinfo::gather_sysinfo;
32use serde::Serialize;
33
34#[derive(Clone, Copy)]
37pub struct UnderscoreHelper;
38
39impl handlebars::HelperDef for UnderscoreHelper {
40 fn call<'reg: 'rc, 'rc>(
41 &self,
42 h: &handlebars::Helper,
43 _: &handlebars::Handlebars,
44 _: &handlebars::Context,
45 _rc: &mut handlebars::RenderContext,
46 out: &mut dyn handlebars::Output,
47 ) -> handlebars::HelperResult {
48 use handlebars::JsonRender;
49 let param = h.param(0).unwrap();
50 let underscore_param = underscore(param.value().render());
51 out.write(&underscore_param)?;
52 Ok(())
53 }
54}
55
56fn underscore<Number>(i: Number) -> String
58where
59 Number: std::string::ToString,
60{
61 let mut s = String::new();
62 let i_str = i.to_string();
63 let a = i_str.chars().rev().enumerate();
64 for (idx, val) in a {
65 if idx != 0 && idx % 3 == 0 {
66 s.insert(0, '_');
67 }
68 s.insert(0, val);
69 }
70 s
71}
72
73pub fn new_rng(seed: Option<u64>) -> (impl rand::Rng, u64) {
77 let seed = seed.unwrap_or(rand::thread_rng().gen::<u64>());
78 (rand_pcg::Pcg64::seed_from_u64(seed), seed)
79}
80
81pub fn check_build_profile() -> Result<(), String> {
88 if cfg!(build_profile = "debug") {
89 Err("Detected a `debug` profile".into())
90 } else if !cfg!(build_opt_level = "3") {
91 Err("The optimization level is not set to 3".into())
92 } else {
93 Ok(())
94 }
95}
96
97#[derive(Debug, Default, Serialize, Clone, PartialEq, Args)]
99#[command(rename_all = "kebab-case")]
100pub struct HostInfoParams {
101 #[arg(long)]
103 pub hostname_override: Option<String>,
104
105 #[arg(long, default_value = "<UNKNOWN>")]
109 pub hostname_fallback: String,
110
111 #[arg(long, default_value = "<UNKNOWN>")]
115 pub cpuname_fallback: String,
116}
117
118impl HostInfoParams {
119 pub fn hostname(&self) -> String {
123 self.hostname_override
124 .clone()
125 .or(gethostname::gethostname().into_string().ok())
126 .unwrap_or(self.hostname_fallback.clone())
127 }
128
129 pub fn cpuname(&self) -> String {
133 gather_sysinfo().cpu.unwrap_or(self.cpuname_fallback.clone())
134 }
135}