referrerpolicy=no-referrer-when-downgrade

frame_benchmarking_cli/shared/
weight_params.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//! Calculates a weight from the [`super::Stats`] of a benchmark result.
19
20use sc_cli::Result;
21
22use clap::Args;
23use serde::Serialize;
24use std::path::PathBuf;
25
26use super::{StatSelect, Stats};
27
28/// Configures the weight generation.
29#[derive(Debug, Default, Serialize, Clone, PartialEq, Args)]
30pub struct WeightParams {
31	/// File or directory to write the *weight* files to.
32	///
33	/// For Substrate this should be `frame/support/src/weights`.
34	#[arg(long)]
35	pub weight_path: Option<PathBuf>,
36
37	/// Select a specific metric to calculate the final weight output.
38	#[arg(long = "metric", default_value = "average")]
39	pub weight_metric: StatSelect,
40
41	/// Multiply the resulting weight with the given factor. Must be positive.
42	///
43	/// Is applied before `weight_add`.
44	#[arg(long = "mul", default_value_t = 1.0)]
45	pub weight_mul: f64,
46
47	/// Add the given offset to the resulting weight.
48	///
49	/// Is applied after `weight_mul`.
50	#[arg(long = "add", default_value_t = 0)]
51	pub weight_add: u64,
52}
53
54/// Calculates the final weight by multiplying the selected metric with
55/// `weight_mul` and adding `weight_add`.
56/// Does not use safe casts and can overflow.
57impl WeightParams {
58	pub fn calc_weight(&self, stat: &Stats) -> Result<u64> {
59		if self.weight_mul.is_sign_negative() || !self.weight_mul.is_normal() {
60			return Err("invalid floating number for `weight_mul`".into())
61		}
62		let s = stat.select(self.weight_metric) as f64;
63		let w = s.mul_add(self.weight_mul, self.weight_add as f64).ceil();
64		Ok(w as u64) // No safe cast here since there is no `From<f64>` for `u64`.
65	}
66}
67
68#[cfg(test)]
69mod test_weight_params {
70	use super::WeightParams;
71	use crate::shared::{StatSelect, Stats};
72
73	#[test]
74	fn calc_weight_works() {
75		let stats = Stats { avg: 113, ..Default::default() };
76		let params = WeightParams {
77			weight_metric: StatSelect::Average,
78			weight_mul: 0.75,
79			weight_add: 3,
80			..Default::default()
81		};
82
83		let want = (113.0f64 * 0.75 + 3.0).ceil() as u64; // Ceil for overestimation.
84		let got = params.calc_weight(&stats).unwrap();
85		assert_eq!(want, got);
86	}
87
88	#[test]
89	fn calc_weight_detects_negative_mul() {
90		let stats = Stats::default();
91		let params = WeightParams { weight_mul: -0.75, ..Default::default() };
92
93		assert!(params.calc_weight(&stats).is_err());
94	}
95}