1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
56// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
1718//! Code that is shared among all benchmarking sub-commands.
1920pub mod genesis_state;
21pub mod record;
22pub mod stats;
23pub mod weight_params;
2425pub use record::BenchRecord;
26pub use stats::{StatSelect, Stats};
27pub use weight_params::WeightParams;
2829use clap::Args;
30use rand::prelude::*;
31use sc_sysinfo::gather_sysinfo;
32use serde::Serialize;
3334/// A Handlebars helper to add an underscore after every 3rd character,
35/// i.e. a separator for large numbers.
36#[derive(Clone, Copy)]
37pub struct UnderscoreHelper;
3839impl handlebars::HelperDef for UnderscoreHelper {
40fn 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 {
48use handlebars::JsonRender;
49let param = h.param(0).unwrap();
50let underscore_param = underscore(param.value().render());
51 out.write(&underscore_param)?;
52Ok(())
53 }
54}
5556/// Add an underscore after every 3rd character, i.e. a separator for large numbers.
57fn underscore<Number>(i: Number) -> String
58where
59Number: std::string::ToString,
60{
61let mut s = String::new();
62let i_str = i.to_string();
63let a = i_str.chars().rev().enumerate();
64for (idx, val) in a {
65if idx != 0 && idx % 3 == 0 {
66 s.insert(0, '_');
67 }
68 s.insert(0, val);
69 }
70 s
71}
7273/// Returns an rng and the seed that was used to create it.
74///
75/// Uses a random seed if none is provided.
76pub fn new_rng(seed: Option<u64>) -> (impl rand::Rng, u64) {
77let seed = seed.unwrap_or(rand::thread_rng().gen::<u64>());
78 (rand_pcg::Pcg64::seed_from_u64(seed), seed)
79}
8081/// Returns an error if a debug profile is detected.
82///
83/// The rust compiler only exposes the binary information whether
84/// or not we are in a `debug` build.
85/// This means that `release` and `production` cannot be told apart.
86/// This function additionally checks for OPT-LEVEL = 3.
87pub fn check_build_profile() -> Result<(), String> {
88if cfg!(build_profile = "debug") {
89Err("Detected a `debug` profile".into())
90 } else if !cfg!(build_opt_level = "3") {
91Err("The optimization level is not set to 3".into())
92 } else {
93Ok(())
94 }
95}
9697/// Parameters to configure how the host info will be determined.
98#[derive(Debug, Default, Serialize, Clone, PartialEq, Args)]
99#[command(rename_all = "kebab-case")]
100pub struct HostInfoParams {
101/// Manually override the hostname to use.
102#[arg(long)]
103pub hostname_override: Option<String>,
104105/// Specify a fallback hostname if no-one could be detected automatically.
106 ///
107 /// Note: This only exists to make the `hostname` function infallible.
108#[arg(long, default_value = "<UNKNOWN>")]
109pub hostname_fallback: String,
110111/// Specify a fallback CPU name if no-one could be detected automatically.
112 ///
113 /// Note: This only exists to make the `cpuname` function infallible.
114#[arg(long, default_value = "<UNKNOWN>")]
115pub cpuname_fallback: String,
116}
117118impl HostInfoParams {
119/// Returns the hostname of the machine.
120 ///
121 /// Can be used to track on which machine a benchmark was run.
122pub fn hostname(&self) -> String {
123self.hostname_override
124 .clone()
125 .or(gethostname::gethostname().into_string().ok())
126 .unwrap_or(self.hostname_fallback.clone())
127 }
128129/// Returns the CPU name of the current machine.
130 ///
131 /// Can be used to track on which machine a benchmark was run.
132pub fn cpuname(&self) -> String {
133 gather_sysinfo().cpu.unwrap_or(self.cpuname_fallback.clone())
134 }
135}