Skip to content
Snippets Groups Projects

Automatic weights documentation

Merged Benjamin Gallois requested to merge bgallois/duniter-v2s:weight-visualisation into master
2 files
+ 55
20
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -17,45 +17,65 @@ pub struct WeightInfo {
pub relative_weight: f64,
}
fn main() {
let pallet_weights = read_pallet_weight();
let db_weight = read_db_weight();
let _overhead_weights = read_overhead_weight();
/// Returns a HashMap <pallet_name, <extrinsic_name, weigh_info>>
/// of the analyzed weights.
///
/// # Arguments
///
/// * `folder_path` - A Path to a folder where the weight files are stored.
/// `paritydb_weights.rs` is mandatory and pallet weights should start by
/// `pallet_`.
///
/// # Examples
///
/// ```
/// use weightanalyzer::analyze_weight;
/// use std::path::Path;
/// let weight_by_pallet = analyze_weight(Path::new("../../runtime/common/src/weights/"));
/// println!("{:?}", weight_by_pallet);
/// ```
pub fn analyze_weight(folder_path: &Path) -> HashMap<String, HashMap<String, WeightInfo>> {
let pallet_weights = read_pallet_weight(folder_path);
let db_weight = read_db_weight(folder_path);
let _overhead_weights = read_overhead_weight(folder_path);
// 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);
process(pallet_weights, scope)
}
fn read_pallet_weight() -> Vec<Vec<ChromaticExtrinsic>> {
fn read_pallet_weight(folder_path: &Path) -> 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)
for path in glob(
folder_path
.join("pallet_*")
.to_str()
.expect("Invalid pallet path"),
)
.expect("Invalid pallet pattern")
.filter_map(Result::ok)
{
let file = subweight_core::parse::pallet::parse_file(&path);
if let Ok(file) = file {
parsed_files.push(file);
}
}
if parsed_files.is_empty() {
panic!("No pallet found");
}
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_db_weight(folder_path: &Path) -> Weights {
subweight_core::parse::storage::parse_file(folder_path.join("paritydb_weights.rs").as_path())
.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 read_overhead_weight(folder_path: &Path) -> Weight {
subweight_core::parse::overhead::parse_file(folder_path.join("block_weights.rs").as_path())
.expect("No overhead weight")
}
fn evaluate_weight(
@@ -102,3 +122,14 @@ fn process(
}
weight_by_pallet
}
#[cfg(test)]
mod tests {
use crate::analyze_weight;
use std::path::Path;
#[test]
fn should_works() {
let weight_by_pallet = analyze_weight(Path::new("../../runtime/common/src/weights/"));
println!("{:?}", weight_by_pallet); // cargo test -- --nocapture
}
}
Loading