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

add relative weight

parent b9da1a76
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !222. Comments created here will be created in the context of that merge request.
use glob::glob; use glob::glob;
use std::collections::HashMap;
use std::path::Path; use std::path::Path;
use subweight_core::parse::overhead::Weight; use subweight_core::parse::overhead::Weight;
use subweight_core::parse::pallet::ChromaticExtrinsic; use subweight_core::parse::pallet::ChromaticExtrinsic;
...@@ -7,25 +8,26 @@ use subweight_core::parse::storage::Weights; ...@@ -7,25 +8,26 @@ use subweight_core::parse::storage::Weights;
use subweight_core::scope::Scope; use subweight_core::scope::Scope;
use subweight_core::term::Term; 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() { fn main() {
let pallet_weights = read_pallet_weight(); let pallet_weights = read_pallet_weight();
let db_weight = read_db_weight(); let db_weight = read_db_weight();
let _overhead_weight = read_overhead_weight(); let _overhead_weights = read_overhead_weight();
// Initialize scope // Initialize scope with db weights
let mut scope = Scope::from_substrate(); let mut scope = Scope::from_substrate();
scope = scope.with_storage_weights(db_weight.weights.read, db_weight.weights.write); scope = scope.with_storage_weights(db_weight.weights.read, db_weight.weights.write);
// Test let weight_by_pallet = process(pallet_weights, scope);
for i in pallet_weights { println!("{:?}", weight_by_pallet);
for j in i {
let (pallet, extrinsic, weight) = evaluate_weight(j, &mut scope).unwrap();
println!(
"Pallet: {:?} ; Extrinsic: {:?} ; Weight: {:?}",
pallet, extrinsic, weight
);
}
}
} }
fn read_pallet_weight() -> Vec<Vec<ChromaticExtrinsic>> { fn read_pallet_weight() -> Vec<Vec<ChromaticExtrinsic>> {
...@@ -59,7 +61,7 @@ fn read_overhead_weight() -> Weight { ...@@ -59,7 +61,7 @@ fn read_overhead_weight() -> Weight {
fn evaluate_weight( fn evaluate_weight(
extrinsic: ChromaticExtrinsic, extrinsic: ChromaticExtrinsic,
scope: &mut Scope<Term<u128>>, scope: &mut Scope<Term<u128>>,
) -> Result<(String, String, u128), String> { ) -> Result<(String, String, WeightInfo), String> {
// Extend the scope with the maximum value of the complexity parameter. // Extend the scope with the maximum value of the complexity parameter.
if let Some(params) = extrinsic.comp_ranges { if let Some(params) = extrinsic.comp_ranges {
params params
...@@ -76,5 +78,27 @@ fn evaluate_weight( ...@@ -76,5 +78,27 @@ fn evaluate_weight(
.expect("Can't evaluate") .expect("Can't evaluate")
.eval(scope) .eval(scope)
.unwrap(); .unwrap();
Ok((extrinsic.pallet, extrinsic.name, weight)) 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
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment