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 17 commits
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
14 changes: 7 additions & 7 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)
{{weightAsNanoseconds 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: {{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))
Expand Down Expand Up @@ -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}}
{{#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}}
.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))
Expand Down
38 changes: 19 additions & 19 deletions frame/lottery/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -65,28 +65,28 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// 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))
}
Expand All @@ -96,7 +96,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// 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))
}
Expand All @@ -107,7 +107,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// 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))
}
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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))
}
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::WEIGHT_PER_NANOS}};
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)
{{weightAsNanoseconds 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: {{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))
Expand Down
6 changes: 5 additions & 1 deletion utils/frame/benchmarking-cli/src/pallet/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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() });
Expand Down
45 changes: 45 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,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,
Expand Down Expand Up @@ -66,6 +67,50 @@ 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 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(())
}
}

/// Add an underscore after every 3rd character, then format it, i.e. a separator for large numbers:
/// 123000000 -> 123_000 * WEIGHT_PER_NANOS.
fn weight_as_nanoseconds<Number>(i: Number) -> String
where
Number: std::string::ToString,
{
let mut i = i.to_string().parse::<u64>().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.insert(0, '(');
s.push_str(" as Weight).saturating_mul(WEIGHT_PER_NANOS)");
s
}

/// Returns an rng and the seed that was used to create it.
///
/// Uses a random seed if none is provided.
Expand Down