diff --git a/.maintain/frame-weight-template.hbs b/.maintain/frame-weight-template.hbs index 34852daf7d47b..63940086d36be 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) + ({{weightAsNanoseconds benchmark.base_weight}} as Weight).saturating_mul(WEIGHT_PER_NANOS) {{#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: {{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 { - ({{underscore benchmark.base_weight}} as Weight) + ({{weightAsNanoseconds benchmark.base_weight}} as Weight).saturating_mul(WEIGHT_PER_NANOS) {{#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: {{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/frame/lottery/src/weights.rs b/frame/lottery/src/weights.rs index 3b6114a73cfaa..f8e42e0985dc3 100644 --- a/frame/lottery/src/weights.rs +++ b/frame/lottery/src/weights.rs @@ -18,30 +18,30 @@ //! 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-05-11, 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 -// --pallet=pallet_lottery -// --extrinsic=* +// --pallet=pallet-lottery +// --extrinsic= // --execution=wasm // --wasm-execution=compiled // --heap-pages=4096 // --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_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 { - (8_966_000 as Weight) - // Standard Error: 5_000 - .saturating_add((302_000 as Weight).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_282_000 as Weight) + (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 { - (3_903_000 as Weight) + (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 { - (51_489_000 as Weight) + (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 { - (53_046_000 as Weight) + (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 { - (37_257_000 as Weight) + (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 { - (8_966_000 as Weight) - // Standard Error: 5_000 - .saturating_add((302_000 as Weight).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_282_000 as Weight) + (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 { - (3_903_000 as Weight) + (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 { - (51_489_000 as Weight) + (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 { - (53_046_000 as Weight) + (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)) } diff --git a/utils/frame/benchmarking-cli/src/pallet/template.hbs b/utils/frame/benchmarking-cli/src/pallet/template.hbs index ea734e165919a..2bfc32da0cbd5 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::WEIGHT_PER_NANOS}}; 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) + ({{weightAsNanoseconds benchmark.base_weight}} as Weight).saturating_mul(WEIGHT_PER_NANOS) {{#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: {{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/pallet/writer.rs b/utils/frame/benchmarking-cli/src/pallet/writer.rs index 515582d8c6db9..f272be6ebefd3 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::UnderscoreHelper, PalletCmd}; +use crate::{ + shared::{UnderscoreHelper, WeightAsNanoseconds}, + PalletCmd, +}; use frame_benchmarking::{ Analysis, AnalysisChoice, BenchmarkBatchSplitResults, BenchmarkResult, BenchmarkSelector, RegressionModel, @@ -298,6 +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("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 f959c285a346e..9efc57d029580 100644 --- a/utils/frame/benchmarking-cli/src/shared/mod.rs +++ b/utils/frame/benchmarking-cli/src/shared/mod.rs @@ -25,6 +25,7 @@ pub use record::BenchRecord; pub use stats::{StatSelect, Stats}; pub use weight_params::WeightParams; +use frame_support::weights::constants; use rand::prelude::*; /// A Handlebars helper to add an underscore after every 3rd character, @@ -66,6 +67,48 @@ where s } +/// 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; + +impl handlebars::HelperDef for WeightAsNanoseconds { + fn call<'reg: 'rc, 'rc>( + &self, + h: &handlebars::Helper, + _: &handlebars::Handlebars, + _: &handlebars::Context, + _rc: &mut handlebars::RenderContext, + out: &mut dyn handlebars::Output, + ) -> handlebars::HelperResult { + let param = h.param(0).unwrap(); + let weight_as_nanoseconds_param = weight_as_nanoseconds(param.render()); + out.write(&weight_as_nanoseconds_param)?; + Ok(()) + } +} + +/// 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, +{ + let mut i = i.to_string().parse::().unwrap(); + i = i.checked_div(constants::WEIGHT_PER_NANOS).unwrap(); + + let mut s = String::new(); + let i_str = i.to_string(); + let a = i_str.chars().rev().enumerate(); + for (idx, val) in a { + if idx != 0 && idx % 3 == 0 { + s.insert(0, '_'); + } + s.insert(0, val); + } + s +} + /// Returns an rng and the seed that was used to create it. /// /// Uses a random seed if none is provided.