diff --git a/resources/weight_analyzer/src/lib.rs b/resources/weight_analyzer/src/lib.rs index ccb189571632f5c7ee83362e9bc3430bb4f346e3..ac3fd9604bd587444ed9a41cc9f69cc55dcd35ea 100644 --- a/resources/weight_analyzer/src/lib.rs +++ b/resources/weight_analyzer/src/lib.rs @@ -1,5 +1,6 @@ use glob::glob; use std::collections::HashMap; +use std::ops::Div; use std::path::Path; use subweight_core::parse::overhead::Weight; use subweight_core::parse::pallet::ChromaticExtrinsic; @@ -8,8 +9,18 @@ use subweight_core::parse::storage::Weights; use subweight_core::scope::Scope; use subweight_core::term::Term; -const MAX_BLOCK_WEIGHT: f64 = 2_000_000_000_000.; // TODO auto extraction from metadata using - // frame_system blockweights constant +pub struct MaxBlockWeight(f64); +impl Default for MaxBlockWeight { + fn default() -> Self { + MaxBlockWeight(2_000_000_000_000.) + } +} +impl Div<&MaxBlockWeight> for f64 { + type Output = Self; + fn div(self, max_block_weight: &MaxBlockWeight) -> Self::Output { + self / max_block_weight.0 + } +} #[derive(Debug)] pub struct WeightInfo { @@ -25,16 +36,21 @@ pub struct WeightInfo { /// * `folder_path` - A Path to a folder where the weight files are stored. /// `paritydb_weights.rs` is mandatory and pallet weights should start by /// `pallet_`. +/// *`max_block_weight` - The maximal weight of a block. /// /// # Examples /// /// ``` /// use weightanalyzer::analyze_weight; /// use std::path::Path; -/// let weight_by_pallet = analyze_weight(Path::new("../../runtime/common/src/weights/")); +/// use weightanalyzer::MaxBlockWeight; +/// let weight_by_pallet = analyze_weight(Path::new("../../runtime/common/src/weights/"), &MaxBlockWeight::default()); /// println!("{:?}", weight_by_pallet); /// ``` -pub fn analyze_weight(folder_path: &Path) -> HashMap<String, HashMap<String, WeightInfo>> { +pub fn analyze_weight( + folder_path: &Path, + max_block_weight: &MaxBlockWeight, +) -> HashMap<String, HashMap<String, WeightInfo>> { let pallet_weights = read_pallet_weight(folder_path); let db_weight = read_db_weight(folder_path); let _overhead_weights = read_overhead_weight(folder_path); @@ -43,7 +59,7 @@ pub fn analyze_weight(folder_path: &Path) -> HashMap<String, HashMap<String, Wei let mut scope = Scope::from_substrate(); scope = scope.with_storage_weights(db_weight.weights.read, db_weight.weights.write); - process(pallet_weights, scope) + process(pallet_weights, scope, max_block_weight) } fn read_pallet_weight(folder_path: &Path) -> Vec<Vec<ChromaticExtrinsic>> { @@ -81,6 +97,7 @@ fn read_overhead_weight(folder_path: &Path) -> Weight { fn evaluate_weight( extrinsic: ChromaticExtrinsic, scope: &mut Scope<Term<u128>>, + max_block_weight: &MaxBlockWeight, ) -> Result<(String, String, WeightInfo), String> { // Extend the scope with the maximum value of the complexity parameter. if let Some(params) = extrinsic.comp_ranges { @@ -98,7 +115,7 @@ fn evaluate_weight( .expect("Can't evaluate") .eval(scope) .unwrap(); - let relative_weight = (weight as f64) / MAX_BLOCK_WEIGHT * 100.; + let relative_weight = (weight as f64) / max_block_weight * 100.; Ok(( extrinsic.pallet, extrinsic.name, @@ -112,11 +129,13 @@ fn evaluate_weight( fn process( pallet_weights: Vec<Vec<ChromaticExtrinsic>>, mut scope: Scope<Term<u128>>, + max_block_weight: &MaxBlockWeight, ) -> HashMap<String, HashMap<String, WeightInfo>> { let mut weight_by_pallet = HashMap::new(); for i in pallet_weights { for j in i { - let (pallet, extrinsic, weight) = evaluate_weight(j, &mut scope).unwrap(); + let (pallet, extrinsic, weight) = + evaluate_weight(j, &mut scope, max_block_weight).unwrap(); weight_by_pallet.insert(pallet.clone(), HashMap::from([(extrinsic.clone(), weight)])); } } @@ -126,15 +145,19 @@ fn process( #[cfg(test)] mod tests { use crate::analyze_weight; + use crate::MaxBlockWeight; use std::path::Path; #[test] fn should_works() { - let weight_by_pallet = analyze_weight(Path::new("../../runtime/common/src/weights/")); + let weight_by_pallet = analyze_weight( + Path::new("../../runtime/common/src/weights/"), + &MaxBlockWeight::default(), + ); println!("{:?}", weight_by_pallet); // cargo test -- --nocapture } #[test] #[should_panic] fn should_not_works() { - let _ = analyze_weight(Path::new("")); + let _ = analyze_weight(Path::new(""), &MaxBlockWeight::default()); } }