Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Change weight template outputs to (x * constants::WEIGH_PER_NANOS) #11233

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .maintain/frame-weight-template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use frame_support::{traits::Get, weights::{Weight, constants::{RocksDbWeight, WEIGHT_PER_NANOS}}};
use sp_std::marker::PhantomData;

/// Weight functions needed for {{pallet}}.
Expand Down Expand Up @@ -60,10 +60,10 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
{{~#each benchmark.components as |c| ~}}
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
) -> Weight {
({{underscore benchmark.base_weight}} as Weight)
({{underscoreFormat benchmark.base_weight}})
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
// Standard Error: {{underscoreFormat cw.error}}
.saturating_add(({{underscoreFormat cw.slope}}).saturating_mul({{cw.name}} as Weight))
{{/each}}
{{#if (ne benchmark.base_reads "0")}}
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight))
Expand Down
8 changes: 4 additions & 4 deletions utils/frame/benchmarking-cli/src/pallet/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::Weight};
use frame_support::{traits::Get, weights::{Weight, constants}};
use sp_std::marker::PhantomData;

/// Weight functions for `{{pallet}}`.
Expand All @@ -29,10 +29,10 @@ impl<T: frame_system::Config> {{pallet}}::WeightInfo for WeightInfo<T> {
{{~#each benchmark.components as |c| ~}}
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
) -> Weight {
({{underscore benchmark.base_weight}} as Weight)
({{underscoreFormat benchmark.base_weight}})
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
// Standard Error: {{underscoreFormat cw.error}}
.saturating_add(({{underscoreFormat cw.slope}}).saturating_mul({{cw.name}} as Weight))
{{/each}}
{{#if (ne benchmark.base_reads "0")}}
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight))
Expand Down
4 changes: 2 additions & 2 deletions utils/frame/benchmarking-cli/src/pallet/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::{
use inflector::Inflector;
use serde::Serialize;

use crate::{shared::UnderscoreHelper, PalletCmd};
use crate::{shared::UnderscoreFormatHelper, PalletCmd};
use frame_benchmarking::{
Analysis, AnalysisChoice, BenchmarkBatchSplitResults, BenchmarkResult, BenchmarkSelector,
RegressionModel,
Expand Down Expand Up @@ -297,7 +297,7 @@ pub fn write_results(

// New Handlebars instance with helpers.
let mut handlebars = handlebars::Handlebars::new();
handlebars.register_helper("underscore", Box::new(UnderscoreHelper));
handlebars.register_helper("underscoreFormat", Box::new(UnderscoreFormatHelper));
handlebars.register_helper("join", Box::new(JoinHelper));
// Don't HTML escape any characters.
handlebars.register_escape_fn(|s| -> String { s.to_string() });
Expand Down
48 changes: 48 additions & 0 deletions utils/frame/benchmarking-cli/src/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub use record::BenchRecord;
pub use stats::{StatSelect, Stats};
pub use weight_params::WeightParams;

use frame_support::weights::constants;

/// A Handlebars helper to add an underscore after every 3rd character,
/// i.e. a separator for large numbers.
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -63,3 +65,49 @@ where
}
s
}

/// A Handlebars helper to add an underscore after every 3rd character,then format it,
/// i.e. a separator for large numbers : 123000000 -> 123_000 * WEIGHT_PER_NANOS .
#[derive(Clone, Copy)]
pub struct UnderscoreFormatHelper;

impl handlebars::HelperDef for UnderscoreFormatHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &handlebars::Helper,
_: &handlebars::Handlebars,
_: &handlebars::Context,
_rc: &mut handlebars::RenderContext,
out: &mut dyn handlebars::Output,
) -> handlebars::HelperResult {
use handlebars::JsonRender;
let param = h.param(0).unwrap();
let underscore_format_param = underscore_format(param.value().render());
out.write(&underscore_format_param)?;
Ok(())
}
}

/// Add an underscore after every 3rd character, then format it, i.e. a separator for large numbers: 123000000 -> 123_000 * WEIGHT_PER_NANOS .
fn underscore_format<Number>(i: Number) -> String
where
Number: std::string::ToString,
{
let mut s = String::new();
let i_str = i.to_string();
let f_u64 = i_str.parse::<f64>().unwrap();
let j_str = (f_u64 / constants::WEIGHT_PER_NANOS as f64 ).to_string();
let a = j_str.chars().rev().enumerate();
for (idx, val) in a {
if idx != 0 && idx % 3 == 0 {
if val=='.' {

}else{
s.insert(0, '_');
}
}
s.insert(0, val);
}
s.push_str(" * constants::WEIGHT_PER_NANOS ");
s
}