From 1ba441622ea3dc0738374d4d0fe4a0a8815380de Mon Sep 17 00:00:00 2001 From: hzy1919 Date: Mon, 18 Apr 2022 19:27:23 +0800 Subject: [PATCH 01/13] Change the weight template currently outputs to (x * constants::WEIGHT_PER_NANOS) which is semantically more clear --- .maintain/frame-weight-template.hbs | 8 ++-- .../benchmarking-cli/src/pallet/template.hbs | 8 ++-- .../benchmarking-cli/src/pallet/writer.rs | 4 +- .../frame/benchmarking-cli/src/shared/mod.rs | 48 +++++++++++++++++++ 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/.maintain/frame-weight-template.hbs b/.maintain/frame-weight-template.hbs index 34852daf7d47b..d5e0a224f624c 100644 --- a/.maintain/frame-weight-template.hbs +++ b/.maintain/frame-weight-template.hbs @@ -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}}. @@ -60,10 +60,10 @@ impl WeightInfo for SubstrateWeight { {{~#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)) diff --git a/utils/frame/benchmarking-cli/src/pallet/template.hbs b/utils/frame/benchmarking-cli/src/pallet/template.hbs index ea734e165919a..aa4fe4491592d 100644 --- a/utils/frame/benchmarking-cli/src/pallet/template.hbs +++ b/utils/frame/benchmarking-cli/src/pallet/template.hbs @@ -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}}`. @@ -29,10 +29,10 @@ impl {{pallet}}::WeightInfo for WeightInfo { {{~#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)) diff --git a/utils/frame/benchmarking-cli/src/pallet/writer.rs b/utils/frame/benchmarking-cli/src/pallet/writer.rs index cd97b3efbd9db..e5efa971ea656 100644 --- a/utils/frame/benchmarking-cli/src/pallet/writer.rs +++ b/utils/frame/benchmarking-cli/src/pallet/writer.rs @@ -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, @@ -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() }); diff --git a/utils/frame/benchmarking-cli/src/shared/mod.rs b/utils/frame/benchmarking-cli/src/shared/mod.rs index f08d79b9aafca..a773017dff23d 100644 --- a/utils/frame/benchmarking-cli/src/shared/mod.rs +++ b/utils/frame/benchmarking-cli/src/shared/mod.rs @@ -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)] @@ -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(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::().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 +} From 7745d46a030f0379c5060b6000321c72b9fbde34 Mon Sep 17 00:00:00 2001 From: hzy1919 Date: Tue, 19 Apr 2022 20:24:46 +0800 Subject: [PATCH 02/13] fmt --- utils/frame/benchmarking-cli/src/shared/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/utils/frame/benchmarking-cli/src/shared/mod.rs b/utils/frame/benchmarking-cli/src/shared/mod.rs index a773017dff23d..a23d5657ba958 100644 --- a/utils/frame/benchmarking-cli/src/shared/mod.rs +++ b/utils/frame/benchmarking-cli/src/shared/mod.rs @@ -88,7 +88,8 @@ impl handlebars::HelperDef for UnderscoreFormatHelper { } } -/// Add an underscore after every 3rd character, then format it, i.e. a separator for large numbers: 123000000 -> 123_000 * WEIGHT_PER_NANOS . +/// 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(i: Number) -> String where Number: std::string::ToString, @@ -96,18 +97,17 @@ where let mut s = String::new(); let i_str = i.to_string(); let f_u64 = i_str.parse::().unwrap(); - let j_str = (f_u64 / constants::WEIGHT_PER_NANOS as f64 ).to_string(); + 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{ + if val == '.' { + } else { s.insert(0, '_'); } } s.insert(0, val); } - s.push_str(" * constants::WEIGHT_PER_NANOS "); + s.push_str(" * WEIGHT_PER_NANOS"); s } From f216efb819bb6c6a31d8cfbd99249e38d2055146 Mon Sep 17 00:00:00 2001 From: hzy1919 Date: Tue, 19 Apr 2022 21:49:15 +0800 Subject: [PATCH 03/13] fix --- utils/frame/benchmarking-cli/src/shared/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/frame/benchmarking-cli/src/shared/mod.rs b/utils/frame/benchmarking-cli/src/shared/mod.rs index 21e73ff800d11..2a01607e9942e 100644 --- a/utils/frame/benchmarking-cli/src/shared/mod.rs +++ b/utils/frame/benchmarking-cli/src/shared/mod.rs @@ -111,6 +111,8 @@ where } s.push_str(" * WEIGHT_PER_NANOS"); s +} + /// Returns an rng and the seed that was used to create it. /// /// Uses a random seed if none is provided. From 3914c07e97099c5cee2161396e3f087dec749a49 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Tue, 19 Apr 2022 22:40:46 +0200 Subject: [PATCH 04/13] Final fixes Signed-off-by: Oliver Tale-Yazdi --- .maintain/frame-weight-template.hbs | 6 +++--- .../frame/benchmarking-cli/src/shared/mod.rs | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.maintain/frame-weight-template.hbs b/.maintain/frame-weight-template.hbs index d5e0a224f624c..ab10a2b4ca1c3 100644 --- a/.maintain/frame-weight-template.hbs +++ b/.maintain/frame-weight-template.hbs @@ -92,10 +92,10 @@ impl WeightInfo for () { {{~#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(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight)) diff --git a/utils/frame/benchmarking-cli/src/shared/mod.rs b/utils/frame/benchmarking-cli/src/shared/mod.rs index 2a01607e9942e..90e4275229ca7 100644 --- a/utils/frame/benchmarking-cli/src/shared/mod.rs +++ b/utils/frame/benchmarking-cli/src/shared/mod.rs @@ -68,7 +68,7 @@ where } /// 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 . +/// i.e. a separator for large numbers : 123000000 -> 123_000 * WEIGHT_PER_NANOS. #[derive(Clone, Copy)] pub struct UnderscoreFormatHelper; @@ -90,22 +90,23 @@ impl handlebars::HelperDef for UnderscoreFormatHelper { } /// Add an underscore after every 3rd character, then format it, i.e. a separator for large numbers: -/// 123000000 -> 123_000 * WEIGHT_PER_NANOS . +/// 123000000 -> 123_000 * WEIGHT_PER_NANOS. fn underscore_format(i: Number) -> String where Number: std::string::ToString, { + let mut i = i.to_string().parse::().unwrap(); + if i % constants::WEIGHT_PER_NANOS != 0 { + panic!("Nano seconds is the smallest unit; Weight must be divisible by it; qed"); + } + i = i / constants::WEIGHT_PER_NANOS; + let mut s = String::new(); let i_str = i.to_string(); - let f_u64 = i_str.parse::().unwrap(); - let j_str = (f_u64 / constants::WEIGHT_PER_NANOS as f64).to_string(); - let a = j_str.chars().rev().enumerate(); + let a = i_str.chars().rev().enumerate(); for (idx, val) in a { if idx != 0 && idx % 3 == 0 { - if val == '.' { - } else { - s.insert(0, '_'); - } + s.insert(0, '_'); } s.insert(0, val); } From a48233b72197f9c0dbacfd50899f0d0840dfc87f Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Tue, 19 Apr 2022 23:46:19 +0200 Subject: [PATCH 05/13] Update lottery weights Signed-off-by: Oliver Tale-Yazdi --- frame/lottery/src/weights.rs | 38 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/frame/lottery/src/weights.rs b/frame/lottery/src/weights.rs index 3b6114a73cfaa..baf7c39e01b0f 100644 --- a/frame/lottery/src/weights.rs +++ b/frame/lottery/src/weights.rs @@ -18,12 +18,13 @@ //! Autogenerated weights for pallet_lottery //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-01-31, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-04-19, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: // ./target/production/substrate // benchmark +// pallet // --chain=dev // --steps=50 // --repeat=20 @@ -35,13 +36,12 @@ // --output=./frame/lottery/src/weights.rs // --template=.maintain/frame-weight-template.hbs // --header=HEADER-APACHE2 -// --raw #![cfg_attr(rustfmt, rustfmt_skip)] #![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_lottery. @@ -65,28 +65,28 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Lottery Tickets (r:0 w:1) fn buy_ticket() -> Weight { - (37_257_000 as Weight) + (38_262 * WEIGHT_PER_NANOS) .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } // Storage: Lottery CallIndices (r:0 w:1) fn set_calls(n: u32, ) -> Weight { - (8_966_000 as Weight) - // Standard Error: 5_000 - .saturating_add((302_000 as Weight).saturating_mul(n as Weight)) + (9_092 * WEIGHT_PER_NANOS) + // Standard Error: 4 * WEIGHT_PER_NANOS + .saturating_add((298 * WEIGHT_PER_NANOS).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Lottery Lottery (r:1 w:1) // Storage: Lottery LotteryIndex (r:1 w:1) // Storage: System Account (r:1 w:1) fn start_lottery() -> Weight { - (32_282_000 as Weight) + (32_238 * WEIGHT_PER_NANOS) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Lottery Lottery (r:1 w:1) fn stop_repeat() -> Weight { - (3_903_000 as Weight) + (4_023 * WEIGHT_PER_NANOS) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } @@ -96,7 +96,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Lottery TicketsCount (r:1 w:1) // Storage: Lottery Tickets (r:1 w:0) fn on_initialize_end() -> Weight { - (51_489_000 as Weight) + (52_404 * WEIGHT_PER_NANOS) .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } @@ -107,7 +107,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Lottery Tickets (r:1 w:0) // Storage: Lottery LotteryIndex (r:1 w:1) fn on_initialize_repeat() -> Weight { - (53_046_000 as Weight) + (54_167 * WEIGHT_PER_NANOS) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(5 as Weight)) } @@ -123,28 +123,28 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Lottery Tickets (r:0 w:1) fn buy_ticket() -> Weight { - (37_257_000 as Weight) + (38_262 * WEIGHT_PER_NANOS) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } // Storage: Lottery CallIndices (r:0 w:1) fn set_calls(n: u32, ) -> Weight { - (8_966_000 as Weight) - // Standard Error: 5_000 - .saturating_add((302_000 as Weight).saturating_mul(n as Weight)) + (9_092 * WEIGHT_PER_NANOS) + // Standard Error: 4 * WEIGHT_PER_NANOS + .saturating_add((298 * WEIGHT_PER_NANOS).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } // Storage: Lottery Lottery (r:1 w:1) // Storage: Lottery LotteryIndex (r:1 w:1) // Storage: System Account (r:1 w:1) fn start_lottery() -> Weight { - (32_282_000 as Weight) + (32_238 * WEIGHT_PER_NANOS) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } // Storage: Lottery Lottery (r:1 w:1) fn stop_repeat() -> Weight { - (3_903_000 as Weight) + (4_023 * WEIGHT_PER_NANOS) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } @@ -154,7 +154,7 @@ impl WeightInfo for () { // Storage: Lottery TicketsCount (r:1 w:1) // Storage: Lottery Tickets (r:1 w:0) fn on_initialize_end() -> Weight { - (51_489_000 as Weight) + (52_404 * WEIGHT_PER_NANOS) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } @@ -165,7 +165,7 @@ impl WeightInfo for () { // Storage: Lottery Tickets (r:1 w:0) // Storage: Lottery LotteryIndex (r:1 w:1) fn on_initialize_repeat() -> Weight { - (53_046_000 as Weight) + (54_167 * WEIGHT_PER_NANOS) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(5 as Weight)) } From a8506108e212159cf9053b6ebd2429f83870ee52 Mon Sep 17 00:00:00 2001 From: hzy1919 Date: Wed, 27 Apr 2022 10:41:51 +0800 Subject: [PATCH 06/13] Fix --- utils/frame/benchmarking-cli/src/pallet/writer.rs | 3 ++- utils/frame/benchmarking-cli/src/shared/mod.rs | 12 ++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/utils/frame/benchmarking-cli/src/pallet/writer.rs b/utils/frame/benchmarking-cli/src/pallet/writer.rs index e5efa971ea656..d1a4e62de2ed8 100644 --- a/utils/frame/benchmarking-cli/src/pallet/writer.rs +++ b/utils/frame/benchmarking-cli/src/pallet/writer.rs @@ -26,7 +26,7 @@ use std::{ use inflector::Inflector; use serde::Serialize; -use crate::{shared::UnderscoreFormatHelper, PalletCmd}; +use crate::{shared::{UnderscoreFormatHelper,UnderscoreHelper}, PalletCmd}; use frame_benchmarking::{ Analysis, AnalysisChoice, BenchmarkBatchSplitResults, BenchmarkResult, BenchmarkSelector, RegressionModel, @@ -297,6 +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. diff --git a/utils/frame/benchmarking-cli/src/shared/mod.rs b/utils/frame/benchmarking-cli/src/shared/mod.rs index 90e4275229ca7..77299d8f29b25 100644 --- a/utils/frame/benchmarking-cli/src/shared/mod.rs +++ b/utils/frame/benchmarking-cli/src/shared/mod.rs @@ -81,25 +81,21 @@ impl handlebars::HelperDef for UnderscoreFormatHelper { _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)?; + let weight_as_nanoseconds_param = weight_as_nanoseconds(param.render()); + out.write(&weight_as_nanoseconds_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(i: Number) -> String +fn weight_as_nanoseconds(i: Number) -> String where Number: std::string::ToString, { let mut i = i.to_string().parse::().unwrap(); - if i % constants::WEIGHT_PER_NANOS != 0 { - panic!("Nano seconds is the smallest unit; Weight must be divisible by it; qed"); - } - i = i / constants::WEIGHT_PER_NANOS; + i = i.checked_div(constants::WEIGHT_PER_NANOS).unwrap(); let mut s = String::new(); let i_str = i.to_string(); From 094b59438346be8b7de8e45a0421571327acdfc1 Mon Sep 17 00:00:00 2001 From: hzy1919 Date: Wed, 27 Apr 2022 19:57:03 +0800 Subject: [PATCH 07/13] fmt --- utils/frame/benchmarking-cli/src/pallet/writer.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/utils/frame/benchmarking-cli/src/pallet/writer.rs b/utils/frame/benchmarking-cli/src/pallet/writer.rs index d1a4e62de2ed8..51017847fe3c1 100644 --- a/utils/frame/benchmarking-cli/src/pallet/writer.rs +++ b/utils/frame/benchmarking-cli/src/pallet/writer.rs @@ -26,7 +26,10 @@ use std::{ use inflector::Inflector; use serde::Serialize; -use crate::{shared::{UnderscoreFormatHelper,UnderscoreHelper}, PalletCmd}; +use crate::{ + shared::{UnderscoreFormatHelper, UnderscoreHelper}, + PalletCmd, +}; use frame_benchmarking::{ Analysis, AnalysisChoice, BenchmarkBatchSplitResults, BenchmarkResult, BenchmarkSelector, RegressionModel, From 834a7fefd85c544cc4ca94189e493143dd9681b7 Mon Sep 17 00:00:00 2001 From: ZhiYong Date: Wed, 27 Apr 2022 21:23:32 +0800 Subject: [PATCH 08/13] Update utils/frame/benchmarking-cli/src/shared/mod.rs Co-authored-by: Shawn Tabrizi --- utils/frame/benchmarking-cli/src/shared/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/frame/benchmarking-cli/src/shared/mod.rs b/utils/frame/benchmarking-cli/src/shared/mod.rs index 77299d8f29b25..ddaf14d66ade2 100644 --- a/utils/frame/benchmarking-cli/src/shared/mod.rs +++ b/utils/frame/benchmarking-cli/src/shared/mod.rs @@ -70,9 +70,9 @@ where /// 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; +pub struct WeightAsNanoseconds; -impl handlebars::HelperDef for UnderscoreFormatHelper { +impl handlebars::HelperDef for WeightAsNanoseconds { fn call<'reg: 'rc, 'rc>( &self, h: &handlebars::Helper, From eafa96f20e18918bfba8fb2bc26f91f6cdfb908b Mon Sep 17 00:00:00 2001 From: ZhiYong Date: Wed, 27 Apr 2022 21:23:54 +0800 Subject: [PATCH 09/13] Update .maintain/frame-weight-template.hbs Co-authored-by: Shawn Tabrizi --- .maintain/frame-weight-template.hbs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.maintain/frame-weight-template.hbs b/.maintain/frame-weight-template.hbs index ab10a2b4ca1c3..f5f55e13d821a 100644 --- a/.maintain/frame-weight-template.hbs +++ b/.maintain/frame-weight-template.hbs @@ -62,8 +62,8 @@ impl WeightInfo for SubstrateWeight { ) -> Weight { ({{underscoreFormat benchmark.base_weight}}) {{#each benchmark.component_weight as |cw|}} - // Standard Error: {{underscoreFormat cw.error}} - .saturating_add(({{underscoreFormat cw.slope}}).saturating_mul({{cw.name}} as Weight)) + // Standard Error: {{weightAsNanoseconds cw.error}} + .saturating_add(({{weightAsNanoseconds 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)) From a2a035ed3f2801a19c00ddd78bf0ab181e8e42a3 Mon Sep 17 00:00:00 2001 From: hzy1919 Date: Thu, 28 Apr 2022 22:08:06 +0800 Subject: [PATCH 10/13] Update multiplication to be saturating. --- .maintain/frame-weight-template.hbs | 10 +++++----- utils/frame/benchmarking-cli/src/pallet/template.hbs | 6 +++--- utils/frame/benchmarking-cli/src/pallet/writer.rs | 4 ++-- utils/frame/benchmarking-cli/src/shared/mod.rs | 3 ++- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.maintain/frame-weight-template.hbs b/.maintain/frame-weight-template.hbs index f5f55e13d821a..6c1b3117f5657 100644 --- a/.maintain/frame-weight-template.hbs +++ b/.maintain/frame-weight-template.hbs @@ -60,10 +60,10 @@ impl WeightInfo for SubstrateWeight { {{~#each benchmark.components as |c| ~}} {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} ) -> Weight { - ({{underscoreFormat benchmark.base_weight}}) + {{weightAsNanoseconds benchmark.base_weight}} {{#each benchmark.component_weight as |cw|}} // Standard Error: {{weightAsNanoseconds cw.error}} - .saturating_add(({{weightAsNanoseconds cw.slope}}).saturating_mul({{cw.name}} as Weight)) + .saturating_add({{weightAsNanoseconds 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)) @@ -92,10 +92,10 @@ impl WeightInfo for () { {{~#each benchmark.components as |c| ~}} {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} ) -> Weight { - ({{underscoreFormat benchmark.base_weight}}) + {{weightAsNanoseconds benchmark.base_weight}} {{#each benchmark.component_weight as |cw|}} - // Standard Error: {{underscoreFormat cw.error}} - .saturating_add(({{underscoreFormat cw.slope}}).saturating_mul({{cw.name}} as Weight)) + // Standard Error: {{weightAsNanoseconds cw.error}} + .saturating_add({{weightAsNanoseconds cw.slope}}.saturating_mul({{cw.name}} as Weight)) {{/each}} {{#if (ne benchmark.base_reads "0")}} .saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight)) diff --git a/utils/frame/benchmarking-cli/src/pallet/template.hbs b/utils/frame/benchmarking-cli/src/pallet/template.hbs index aa4fe4491592d..1dc5fd4e1508b 100644 --- a/utils/frame/benchmarking-cli/src/pallet/template.hbs +++ b/utils/frame/benchmarking-cli/src/pallet/template.hbs @@ -29,10 +29,10 @@ impl {{pallet}}::WeightInfo for WeightInfo { {{~#each benchmark.components as |c| ~}} {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} ) -> Weight { - ({{underscoreFormat benchmark.base_weight}}) + {{weightAsNanoseconds benchmark.base_weight}} {{#each benchmark.component_weight as |cw|}} - // Standard Error: {{underscoreFormat cw.error}} - .saturating_add(({{underscoreFormat cw.slope}}).saturating_mul({{cw.name}} as Weight)) + // Standard Error: {{weightAsNanoseconds cw.error}} + .saturating_add({{weightAsNanoseconds 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)) diff --git a/utils/frame/benchmarking-cli/src/pallet/writer.rs b/utils/frame/benchmarking-cli/src/pallet/writer.rs index 51017847fe3c1..2a413444dde12 100644 --- a/utils/frame/benchmarking-cli/src/pallet/writer.rs +++ b/utils/frame/benchmarking-cli/src/pallet/writer.rs @@ -27,7 +27,7 @@ use inflector::Inflector; use serde::Serialize; use crate::{ - shared::{UnderscoreFormatHelper, UnderscoreHelper}, + shared::{WeightAsNanoseconds, UnderscoreHelper}, PalletCmd, }; use frame_benchmarking::{ @@ -301,7 +301,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("weightAsNanoseconds", Box::new(WeightAsNanoseconds)); handlebars.register_helper("join", Box::new(JoinHelper)); // Don't HTML escape any characters. handlebars.register_escape_fn(|s| -> String { s.to_string() }); diff --git a/utils/frame/benchmarking-cli/src/shared/mod.rs b/utils/frame/benchmarking-cli/src/shared/mod.rs index ddaf14d66ade2..79e598709a897 100644 --- a/utils/frame/benchmarking-cli/src/shared/mod.rs +++ b/utils/frame/benchmarking-cli/src/shared/mod.rs @@ -106,7 +106,8 @@ where } s.insert(0, val); } - s.push_str(" * WEIGHT_PER_NANOS"); + s.insert(0, '('); + s.push_str(" as Weight).saturating_mul(WEIGHT_PER_NANOS)"); s } From ab6aeb17adb485b78cde12e0298a1d6252a15e5e Mon Sep 17 00:00:00 2001 From: hzy1919 Date: Thu, 28 Apr 2022 22:14:40 +0800 Subject: [PATCH 11/13] Fix and fmt. --- utils/frame/benchmarking-cli/src/pallet/template.hbs | 2 +- utils/frame/benchmarking-cli/src/pallet/writer.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/frame/benchmarking-cli/src/pallet/template.hbs b/utils/frame/benchmarking-cli/src/pallet/template.hbs index 1dc5fd4e1508b..1bbc6ea09ada8 100644 --- a/utils/frame/benchmarking-cli/src/pallet/template.hbs +++ b/utils/frame/benchmarking-cli/src/pallet/template.hbs @@ -14,7 +14,7 @@ #![allow(unused_parens)] #![allow(unused_imports)] -use frame_support::{traits::Get, weights::{Weight, constants}}; +use frame_support::{traits::Get, weights::{Weight, constants::WEIGHT_PER_NANOS}}; use sp_std::marker::PhantomData; /// Weight functions for `{{pallet}}`. diff --git a/utils/frame/benchmarking-cli/src/pallet/writer.rs b/utils/frame/benchmarking-cli/src/pallet/writer.rs index 2a413444dde12..7588061f33022 100644 --- a/utils/frame/benchmarking-cli/src/pallet/writer.rs +++ b/utils/frame/benchmarking-cli/src/pallet/writer.rs @@ -27,7 +27,7 @@ use inflector::Inflector; use serde::Serialize; use crate::{ - shared::{WeightAsNanoseconds, UnderscoreHelper}, + shared::{UnderscoreHelper, WeightAsNanoseconds}, PalletCmd, }; use frame_benchmarking::{ From b5978b2a2330d90f922472364cdc9d01ee81e26c Mon Sep 17 00:00:00 2001 From: hzy1919 Date: Tue, 10 May 2022 22:41:12 +0800 Subject: [PATCH 12/13] update weight-template and some related comments. --- .maintain/frame-weight-template.hbs | 12 ++++++------ utils/frame/benchmarking-cli/src/pallet/template.hbs | 6 +++--- utils/frame/benchmarking-cli/src/shared/mod.rs | 10 ++++------ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/.maintain/frame-weight-template.hbs b/.maintain/frame-weight-template.hbs index 6c1b3117f5657..63940086d36be 100644 --- a/.maintain/frame-weight-template.hbs +++ b/.maintain/frame-weight-template.hbs @@ -60,10 +60,10 @@ impl WeightInfo for SubstrateWeight { {{~#each benchmark.components as |c| ~}} {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} ) -> Weight { - {{weightAsNanoseconds benchmark.base_weight}} + ({{weightAsNanoseconds benchmark.base_weight}} as Weight).saturating_mul(WEIGHT_PER_NANOS) {{#each benchmark.component_weight as |cw|}} - // Standard Error: {{weightAsNanoseconds cw.error}} - .saturating_add({{weightAsNanoseconds cw.slope}}.saturating_mul({{cw.name}} as Weight)) + // Standard Error: {{weightAsNanoseconds cw.error}} NS + .saturating_add(({{weightAsNanoseconds cw.slope}} as Weight).saturating_mul(WEIGHT_PER_NANOS).saturating_mul({{cw.name}} as Weight)) {{/each}} {{#if (ne benchmark.base_reads "0")}} .saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight)) @@ -92,10 +92,10 @@ impl WeightInfo for () { {{~#each benchmark.components as |c| ~}} {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} ) -> Weight { - {{weightAsNanoseconds benchmark.base_weight}} + ({{weightAsNanoseconds benchmark.base_weight}} as Weight).saturating_mul(WEIGHT_PER_NANOS) {{#each benchmark.component_weight as |cw|}} - // Standard Error: {{weightAsNanoseconds cw.error}} - .saturating_add({{weightAsNanoseconds cw.slope}}.saturating_mul({{cw.name}} as Weight)) + // Standard Error: {{weightAsNanoseconds cw.error}} NS + .saturating_add(({{weightAsNanoseconds cw.slope}} as Weight).saturating_mul(WEIGHT_PER_NANOS).saturating_mul({{cw.name}} as Weight)) {{/each}} {{#if (ne benchmark.base_reads "0")}} .saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight)) diff --git a/utils/frame/benchmarking-cli/src/pallet/template.hbs b/utils/frame/benchmarking-cli/src/pallet/template.hbs index 1bbc6ea09ada8..2bfc32da0cbd5 100644 --- a/utils/frame/benchmarking-cli/src/pallet/template.hbs +++ b/utils/frame/benchmarking-cli/src/pallet/template.hbs @@ -29,10 +29,10 @@ impl {{pallet}}::WeightInfo for WeightInfo { {{~#each benchmark.components as |c| ~}} {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} ) -> Weight { - {{weightAsNanoseconds benchmark.base_weight}} + ({{weightAsNanoseconds benchmark.base_weight}} as Weight).saturating_mul(WEIGHT_PER_NANOS) {{#each benchmark.component_weight as |cw|}} - // Standard Error: {{weightAsNanoseconds cw.error}} - .saturating_add({{weightAsNanoseconds cw.slope}}.saturating_mul({{cw.name}} as Weight)) + // Standard Error: {{weightAsNanoseconds cw.error}} NS + .saturating_add(({{weightAsNanoseconds cw.slope}} as Weight).saturating_mul(WEIGHT_PER_NANOS).saturating_mul({{cw.name}} as Weight)) {{/each}} {{#if (ne benchmark.base_reads "0")}} .saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight)) diff --git a/utils/frame/benchmarking-cli/src/shared/mod.rs b/utils/frame/benchmarking-cli/src/shared/mod.rs index 82beb81b945c4..9efc57d029580 100644 --- a/utils/frame/benchmarking-cli/src/shared/mod.rs +++ b/utils/frame/benchmarking-cli/src/shared/mod.rs @@ -67,8 +67,8 @@ 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. +/// A Handlebars helper to convert to NS as a unit and add an underscore after every 3rd character, +/// i.e. a separator for large numbers : 123000000 -> 123_000 NS. #[derive(Clone, Copy)] pub struct WeightAsNanoseconds; @@ -88,8 +88,8 @@ impl handlebars::HelperDef for WeightAsNanoseconds { } } -/// Add an underscore after every 3rd character, then format it, i.e. a separator for large numbers: -/// 123000000 -> 123_000 * WEIGHT_PER_NANOS. +/// Convert to NS as a unit and add an underscore after every 3rd character, +/// i.e. a separator for large numbers: 123000000 -> 123_000 NS. fn weight_as_nanoseconds(i: Number) -> String where Number: std::string::ToString, @@ -106,8 +106,6 @@ where } s.insert(0, val); } - s.insert(0, '('); - s.push_str(" as Weight).saturating_mul(WEIGHT_PER_NANOS)"); s } From a5b0213c22dfe59ca1555db6b5661e2fb6f86920 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 11 May 2022 17:49:58 +0200 Subject: [PATCH 13/13] Update weights Signed-off-by: Oliver Tale-Yazdi --- frame/lottery/src/weights.rs | 38 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/frame/lottery/src/weights.rs b/frame/lottery/src/weights.rs index baf7c39e01b0f..f8e42e0985dc3 100644 --- a/frame/lottery/src/weights.rs +++ b/frame/lottery/src/weights.rs @@ -18,7 +18,7 @@ //! Autogenerated weights for pallet_lottery //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-04-19, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-05-11, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: @@ -28,8 +28,8 @@ // --chain=dev // --steps=50 // --repeat=20 -// --pallet=pallet_lottery -// --extrinsic=* +// --pallet=pallet-lottery +// --extrinsic= // --execution=wasm // --wasm-execution=compiled // --heap-pages=4096 @@ -65,28 +65,28 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Lottery Tickets (r:0 w:1) fn buy_ticket() -> Weight { - (38_262 * WEIGHT_PER_NANOS) + (38_236 as Weight).saturating_mul(WEIGHT_PER_NANOS) .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } // Storage: Lottery CallIndices (r:0 w:1) fn set_calls(n: u32, ) -> Weight { - (9_092 * WEIGHT_PER_NANOS) - // Standard Error: 4 * WEIGHT_PER_NANOS - .saturating_add((298 * WEIGHT_PER_NANOS).saturating_mul(n as Weight)) + (8_991 as Weight).saturating_mul(WEIGHT_PER_NANOS) + // Standard Error: 6 NS + .saturating_add((294 as Weight).saturating_mul(WEIGHT_PER_NANOS).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Lottery Lottery (r:1 w:1) // Storage: Lottery LotteryIndex (r:1 w:1) // Storage: System Account (r:1 w:1) fn start_lottery() -> Weight { - (32_238 * WEIGHT_PER_NANOS) + (32_071 as Weight).saturating_mul(WEIGHT_PER_NANOS) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Lottery Lottery (r:1 w:1) fn stop_repeat() -> Weight { - (4_023 * WEIGHT_PER_NANOS) + (3_782 as Weight).saturating_mul(WEIGHT_PER_NANOS) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } @@ -96,7 +96,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Lottery TicketsCount (r:1 w:1) // Storage: Lottery Tickets (r:1 w:0) fn on_initialize_end() -> Weight { - (52_404 * WEIGHT_PER_NANOS) + (50_981 as Weight).saturating_mul(WEIGHT_PER_NANOS) .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } @@ -107,7 +107,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Lottery Tickets (r:1 w:0) // Storage: Lottery LotteryIndex (r:1 w:1) fn on_initialize_repeat() -> Weight { - (54_167 * WEIGHT_PER_NANOS) + (55_169 as Weight).saturating_mul(WEIGHT_PER_NANOS) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(5 as Weight)) } @@ -123,28 +123,28 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Lottery Tickets (r:0 w:1) fn buy_ticket() -> Weight { - (38_262 * WEIGHT_PER_NANOS) + (38_236 as Weight).saturating_mul(WEIGHT_PER_NANOS) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } // Storage: Lottery CallIndices (r:0 w:1) fn set_calls(n: u32, ) -> Weight { - (9_092 * WEIGHT_PER_NANOS) - // Standard Error: 4 * WEIGHT_PER_NANOS - .saturating_add((298 * WEIGHT_PER_NANOS).saturating_mul(n as Weight)) + (8_991 as Weight).saturating_mul(WEIGHT_PER_NANOS) + // Standard Error: 6 NS + .saturating_add((294 as Weight).saturating_mul(WEIGHT_PER_NANOS).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } // Storage: Lottery Lottery (r:1 w:1) // Storage: Lottery LotteryIndex (r:1 w:1) // Storage: System Account (r:1 w:1) fn start_lottery() -> Weight { - (32_238 * WEIGHT_PER_NANOS) + (32_071 as Weight).saturating_mul(WEIGHT_PER_NANOS) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } // Storage: Lottery Lottery (r:1 w:1) fn stop_repeat() -> Weight { - (4_023 * WEIGHT_PER_NANOS) + (3_782 as Weight).saturating_mul(WEIGHT_PER_NANOS) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } @@ -154,7 +154,7 @@ impl WeightInfo for () { // Storage: Lottery TicketsCount (r:1 w:1) // Storage: Lottery Tickets (r:1 w:0) fn on_initialize_end() -> Weight { - (52_404 * WEIGHT_PER_NANOS) + (50_981 as Weight).saturating_mul(WEIGHT_PER_NANOS) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } @@ -165,7 +165,7 @@ impl WeightInfo for () { // Storage: Lottery Tickets (r:1 w:0) // Storage: Lottery LotteryIndex (r:1 w:1) fn on_initialize_repeat() -> Weight { - (54_167 * WEIGHT_PER_NANOS) + (55_169 as Weight).saturating_mul(WEIGHT_PER_NANOS) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(5 as Weight)) }