Skip to content
Snippets Groups Projects
Unverified Commit 722e570b authored by bgallois's avatar bgallois
Browse files

fix weight analyzer

parent b328921f
No related branches found
No related tags found
No related merge requests found
Pipeline #35096 failed
...@@ -14,4 +14,6 @@ path = "src/lib.rs" ...@@ -14,4 +14,6 @@ path = "src/lib.rs"
[dependencies] [dependencies]
subweight-core = "3.3.1" subweight-core = "3.3.1"
convert_case = "0.6.0"
glob = "0.3.1" glob = "0.3.1"
serde = { version = "1.0.101", features = ["derive"] }
use convert_case::{Case, Casing};
use glob::glob; use glob::glob;
use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::ops::Div; use std::ops::Div;
use std::path::Path; use std::path::Path;
...@@ -22,7 +24,7 @@ impl Div<&MaxBlockWeight> for f64 { ...@@ -22,7 +24,7 @@ impl Div<&MaxBlockWeight> for f64 {
} }
} }
#[derive(Debug)] #[derive(Clone, Debug, Serialize)]
pub struct WeightInfo { pub struct WeightInfo {
pub weight: u128, pub weight: u128,
pub relative_weight: f64, pub relative_weight: f64,
...@@ -64,12 +66,7 @@ pub fn analyze_weight( ...@@ -64,12 +66,7 @@ pub fn analyze_weight(
fn read_pallet_weight(folder_path: &Path) -> Vec<Vec<ChromaticExtrinsic>> { fn read_pallet_weight(folder_path: &Path) -> Vec<Vec<ChromaticExtrinsic>> {
let mut parsed_files = Vec::new(); let mut parsed_files = Vec::new();
for path in glob( for path in glob(folder_path.join("*").to_str().expect("Invalid pallet path"))
folder_path
.join("pallet_*")
.to_str()
.expect("Invalid pallet path"),
)
.expect("Invalid pallet pattern") .expect("Invalid pallet pattern")
.filter_map(Result::ok) .filter_map(Result::ok)
{ {
...@@ -117,7 +114,14 @@ fn evaluate_weight( ...@@ -117,7 +114,14 @@ fn evaluate_weight(
.unwrap(); .unwrap();
let relative_weight = (weight as f64) / max_block_weight * 100.; let relative_weight = (weight as f64) / max_block_weight * 100.;
Ok(( Ok((
extrinsic.pallet, extrinsic
.pallet
.to_case(Case::Title)
.replace("Pallet", "")
.replace(".rs", "")
.chars()
.filter(|c| !c.is_whitespace())
.collect(),
extrinsic.name, extrinsic.name,
WeightInfo { WeightInfo {
weight, weight,
...@@ -131,12 +135,16 @@ fn process( ...@@ -131,12 +135,16 @@ fn process(
mut scope: Scope<Term<u128>>, mut scope: Scope<Term<u128>>,
max_block_weight: &MaxBlockWeight, max_block_weight: &MaxBlockWeight,
) -> HashMap<String, HashMap<String, WeightInfo>> { ) -> HashMap<String, HashMap<String, WeightInfo>> {
let mut weight_by_pallet = HashMap::new(); let mut weight_by_pallet: HashMap<String, HashMap<String, WeightInfo>> = HashMap::new();
for i in pallet_weights { for i in pallet_weights {
for j in i { for j in i {
let (pallet, extrinsic, weight) = let (pallet, extrinsic, weight) =
evaluate_weight(j, &mut scope, max_block_weight).unwrap(); evaluate_weight(j, &mut scope, max_block_weight).unwrap();
weight_by_pallet.insert(pallet.clone(), HashMap::from([(extrinsic.clone(), weight)])); if let Some(i) = weight_by_pallet.get_mut(&pallet) {
i.insert(extrinsic, weight);
} else {
weight_by_pallet.insert(pallet, HashMap::from([(extrinsic, weight)]));
}
} }
} }
weight_by_pallet weight_by_pallet
...@@ -153,6 +161,7 @@ mod tests { ...@@ -153,6 +161,7 @@ mod tests {
Path::new("../../runtime/common/src/weights/"), Path::new("../../runtime/common/src/weights/"),
&MaxBlockWeight::default(), &MaxBlockWeight::default(),
); );
assert!(weight_by_pallet.get("Balances").unwrap().len() == 7); // 7 extrinsics in pallet
println!("{:?}", weight_by_pallet); // cargo test -- --nocapture println!("{:?}", weight_by_pallet); // cargo test -- --nocapture
} }
#[test] #[test]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment