referrerpolicy=no-referrer-when-downgrade

frame_benchmarking_cli/shared/
mod.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// 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.
17
18//! Code that is shared among all benchmarking sub-commands.
19
20pub 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/// 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;
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
56/// Add an underscore after every 3rd character, i.e. a separator for large numbers.
57fn 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
73/// 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) {
77	let seed = seed.unwrap_or(rand::thread_rng().gen::<u64>());
78	(rand_pcg::Pcg64::seed_from_u64(seed), seed)
79}
80
81/// 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> {
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/// 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)]
103	pub hostname_override: Option<String>,
104
105	/// 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>")]
109	pub hostname_fallback: String,
110
111	/// 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>")]
115	pub cpuname_fallback: String,
116}
117
118impl HostInfoParams {
119	/// Returns the hostname of the machine.
120	///
121	/// Can be used to track on which machine a benchmark was run.
122	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	/// Returns the CPU name of the current machine.
130	///
131	/// Can be used to track on which machine a benchmark was run.
132	pub fn cpuname(&self) -> String {
133		gather_sysinfo().cpu.unwrap_or(self.cpuname_fallback.clone())
134	}
135}