diff --git a/resources/weight_analyzer/Cargo.toml b/resources/weight_analyzer/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..1711ec159e131049d4ff5642918b626a99b8e92a --- /dev/null +++ b/resources/weight_analyzer/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "weight-analyzer" +authors = ['Axiom-Team Developers <https://axiom-team.fr>'] +description = 'Crypto-currency software (based on Substrate framework) to operate Äž1 libre currency' +edition = "2021" +homepage = 'https://duniter.org' +license = 'AGPL-3.0' +repository = 'https://git.duniter.org/nodes/rust/duniter-v2s' +version = '0.0.0' + +[workspace] + +[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 new file mode 100644 index 0000000000000000000000000000000000000000..38b476d97e93f984a2be79de2370be570547c504 --- /dev/null +++ b/resources/weight_analyzer/src/main.rs @@ -0,0 +1,70 @@ +use glob::glob; +use std::path::Path; +use subweight_core::parse::overhead::Weight; +use subweight_core::parse::pallet::ChromaticExtrinsic; +use subweight_core::parse::storage::Weights; +use subweight_core::scope::Scope; +use subweight_core::term::Term; + +fn main() { + let pallet_weights = read_pallet_weight(); + let db_weight = read_db_weight(); + let _overhead_weight = read_overhead_weight(); + + // Initialize scope + let mut scope = Scope::from_substrate(); + scope = scope.with_storage_weights(db_weight.weights.read, db_weight.weights.write); + + // Test + // TODO Fix subweight-core parser not working with parameters + for i in pallet_weights { + for j in i { + let (pallet, extrinsic, weight) = evaluate_weight(j, &scope).unwrap(); + println!( + "Pallet: {:?} ; Extrinsic: {:?} ; Weight: {:?}", + pallet, extrinsic, weight + ); + } + } +} + +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: &Scope<Term<u128>>, +) -> Result<(String, String, u128), String> { + let weight = extrinsic + .term + .simplify(subweight_core::Dimension::Time) + .expect("Can't evaluate") + .eval(scope) + .unwrap(); + Ok((extrinsic.pallet, extrinsic.name, weight)) +}