From f541985185a8759e13dc5d722136343bc96204e4 Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Mon, 25 Dec 2023 22:14:24 +0100
Subject: [PATCH] add weights computation from weight files

---
 resources/weight_analyzer/Cargo.toml  | 15 ++++++
 resources/weight_analyzer/src/main.rs | 70 +++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
 create mode 100644 resources/weight_analyzer/Cargo.toml
 create mode 100644 resources/weight_analyzer/src/main.rs

diff --git a/resources/weight_analyzer/Cargo.toml b/resources/weight_analyzer/Cargo.toml
new file mode 100644
index 000000000..1711ec159
--- /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 000000000..38b476d97
--- /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))
+}
-- 
GitLab