diff --git a/resources/weight_analyzer/Cargo.toml b/resources/weight_analyzer/Cargo.toml index 1711ec159e131049d4ff5642918b626a99b8e92a..37f396f6973e38bd258aa7402ff48ea175ee8c6d 100644 --- a/resources/weight_analyzer/Cargo.toml +++ b/resources/weight_analyzer/Cargo.toml @@ -10,6 +10,10 @@ version = '0.0.0' [workspace] +[lib] +name = "weightanalyzer" +path = "src/lib.rs" + [dependencies] subweight-core = "3.3.1" glob = "0.3.1" diff --git a/resources/weight_analyzer/src/main.rs b/resources/weight_analyzer/src/main.rs deleted file mode 100644 index 2acb7f7762525ad803a7100784ab6c1b789f780a..0000000000000000000000000000000000000000 --- a/resources/weight_analyzer/src/main.rs +++ /dev/null @@ -1,104 +0,0 @@ -use glob::glob; -use std::collections::HashMap; -use std::path::Path; -use subweight_core::parse::overhead::Weight; -use subweight_core::parse::pallet::ChromaticExtrinsic; -use subweight_core::parse::pallet::ComponentRange; -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 - -#[derive(Debug)] -pub struct WeightInfo { - pub weight: u128, - pub relative_weight: f64, -} - -fn main() { - let pallet_weights = read_pallet_weight(); - let db_weight = read_db_weight(); - let _overhead_weights = read_overhead_weight(); - - // Initialize scope with db weights - let mut scope = Scope::from_substrate(); - scope = scope.with_storage_weights(db_weight.weights.read, db_weight.weights.write); - - let weight_by_pallet = process(pallet_weights, scope); - println!("{:?}", weight_by_pallet); -} - -fn read_pallet_weight() -> Vec<Vec<ChromaticExtrinsic>> { - let mut parsed_files = Vec::new(); - for path in glob("../../runtime/common/src/weights/pallet_*") - .expect("No pallet found") - .filter_map(Result::ok) - { - let file = subweight_core::parse::pallet::parse_file(&path); - if let Ok(file) = file { - parsed_files.push(file); - } - } - parsed_files -} - -fn read_db_weight() -> Weights { - subweight_core::parse::storage::parse_file(Path::new( - "../../runtime/common/src/weights/paritydb_weights.rs", - )) - .expect("No DB weights") -} - -fn read_overhead_weight() -> Weight { - subweight_core::parse::overhead::parse_file(Path::new( - "../../runtime/common/src/weights/block_weights.rs", - )) - .expect("No overhead weight") -} - -fn evaluate_weight( - extrinsic: ChromaticExtrinsic, - scope: &mut Scope<Term<u128>>, -) -> Result<(String, String, WeightInfo), String> { - // Extend the scope with the maximum value of the complexity parameter. - if let Some(params) = extrinsic.comp_ranges { - params - .iter() - .for_each(|(key, val): (&String, &ComponentRange)| { - scope.put_var(key.as_str(), Term::Scalar(val.max.into())); - }); - } - - // Evaluate the weight - let weight = extrinsic - .term - .simplify(subweight_core::Dimension::Time) - .expect("Can't evaluate") - .eval(scope) - .unwrap(); - let relative_weight = (weight as f64) / MAX_BLOCK_WEIGHT * 100.; - Ok(( - extrinsic.pallet, - extrinsic.name, - WeightInfo { - weight, - relative_weight, - }, - )) -} - -fn process( - pallet_weights: Vec<Vec<ChromaticExtrinsic>>, - mut scope: Scope<Term<u128>>, -) -> 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(); - weight_by_pallet.insert(pallet.clone(), HashMap::from([(extrinsic.clone(), weight)])); - } - } - weight_by_pallet -}