mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 20:27:58 +00:00
Sub-commands for benchmark (#11164)
* Restructure benchmark commands Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Add benchmark block test Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fixup imports Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * CI Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Review fixes Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Extend error message Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Apply suggestions from code review Co-authored-by: Zeke Mostov <z.mostov@gmail.com> * Review fixes Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Add commands to node-template Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Zeke Mostov <z.mostov@gmail.com>
This commit is contained in:
committed by
GitHub
parent
ef5c4b7fc3
commit
a7261180ee
@@ -0,0 +1,95 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Calculates a weight from the [`super::Stats`] of a benchmark result.
|
||||
|
||||
use sc_cli::Result;
|
||||
|
||||
use clap::Args;
|
||||
use serde::Serialize;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use super::{StatSelect, Stats};
|
||||
|
||||
/// Configures the weight generation.
|
||||
#[derive(Debug, Default, Serialize, Clone, PartialEq, Args)]
|
||||
pub struct WeightParams {
|
||||
/// File or directory to write the *weight* files to.
|
||||
///
|
||||
/// For Substrate this should be `frame/support/src/weights`.
|
||||
#[clap(long)]
|
||||
pub weight_path: Option<PathBuf>,
|
||||
|
||||
/// Select a specific metric to calculate the final weight output.
|
||||
#[clap(long = "metric", default_value = "average")]
|
||||
pub weight_metric: StatSelect,
|
||||
|
||||
/// Multiply the resulting weight with the given factor. Must be positive.
|
||||
///
|
||||
/// Is applied before `weight_add`.
|
||||
#[clap(long = "mul", default_value = "1")]
|
||||
pub weight_mul: f64,
|
||||
|
||||
/// Add the given offset to the resulting weight.
|
||||
///
|
||||
/// Is applied after `weight_mul`.
|
||||
#[clap(long = "add", default_value = "0")]
|
||||
pub weight_add: u64,
|
||||
}
|
||||
|
||||
/// Calculates the final weight by multiplying the selected metric with
|
||||
/// `weight_mul` and adding `weight_add`.
|
||||
/// Does not use safe casts and can overflow.
|
||||
impl WeightParams {
|
||||
pub fn calc_weight(&self, stat: &Stats) -> Result<u64> {
|
||||
if self.weight_mul.is_sign_negative() || !self.weight_mul.is_normal() {
|
||||
return Err("invalid floating number for `weight_mul`".into())
|
||||
}
|
||||
let s = stat.select(self.weight_metric) as f64;
|
||||
let w = s.mul_add(self.weight_mul, self.weight_add as f64).ceil();
|
||||
Ok(w as u64) // No safe cast here since there is no `From<f64>` for `u64`.
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_weight_params {
|
||||
use super::WeightParams;
|
||||
use crate::shared::{StatSelect, Stats};
|
||||
|
||||
#[test]
|
||||
fn calc_weight_works() {
|
||||
let stats = Stats { avg: 113, ..Default::default() };
|
||||
let params = WeightParams {
|
||||
weight_metric: StatSelect::Average,
|
||||
weight_mul: 0.75,
|
||||
weight_add: 3,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let want = (113.0f64 * 0.75 + 3.0).ceil() as u64; // Ceil for overestimation.
|
||||
let got = params.calc_weight(&stats).unwrap();
|
||||
assert_eq!(want, got);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn calc_weight_detects_negative_mul() {
|
||||
let stats = Stats::default();
|
||||
let params = WeightParams { weight_mul: -0.75, ..Default::default() };
|
||||
|
||||
assert!(params.calc_weight(&stats).is_err());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user