diff --git a/.maintain/frame-weight-template.hbs b/.maintain/frame-weight-template.hbs new file mode 100644 index 00000000..9c9e2978 --- /dev/null +++ b/.maintain/frame-weight-template.hbs @@ -0,0 +1,107 @@ +{{header}} +//! Autogenerated weights for {{pallet}} +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}} +//! DATE: {{date}}, STEPS: `{{cmd.steps}}`, REPEAT: {{cmd.repeat}}, LOW RANGE: `{{cmd.lowest_range_values}}`, HIGH RANGE: `{{cmd.highest_range_values}}` +//! HOSTNAME: `{{hostname}}`, CPU: `{{cpuname}}` +//! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}}, CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}} + +// Executed Command: +{{#each args as |arg|}} +// {{arg}} +{{/each}} + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use sp_std::marker::PhantomData; + +/// Weight functions needed for {{pallet}}. +pub trait WeightInfo { + {{#each benchmarks as |benchmark|}} + fn {{benchmark.name~}} + ( + {{~#each benchmark.components as |c| ~}} + {{c.name}}: u32, {{/each~}} + ) -> Weight; + {{/each}} +} + +/// Weights for {{pallet}} using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +{{#if (eq pallet "frame_system")}} +impl WeightInfo for SubstrateWeight { +{{else}} +impl WeightInfo for SubstrateWeight { +{{/if}} + {{#each benchmarks as |benchmark|}} + {{#each benchmark.comments as |comment|}} + // {{comment}} + {{/each}} + {{#each benchmark.component_ranges as |range|}} + /// The range of component `{{range.name}}` is `[{{range.min}}, {{range.max}}]`. + {{/each}} + fn {{benchmark.name~}} + ( + {{~#each benchmark.components as |c| ~}} + {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} + ) -> Weight { + // Minimum execution time: {{underscore benchmark.min_execution_time}} nanoseconds. + Weight::from_ref_time({{underscore benchmark.base_weight}}) + {{#each benchmark.component_weight as |cw|}} + // Standard Error: {{underscore cw.error}} + .saturating_add(Weight::from_ref_time({{underscore cw.slope}}).saturating_mul({{cw.name}}.into())) + {{/each}} + {{#if (ne benchmark.base_reads "0")}} + .saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}})) + {{/if}} + {{#each benchmark.component_reads as |cr|}} + .saturating_add(T::DbWeight::get().reads(({{cr.slope}}_u64).saturating_mul({{cr.name}}.into()))) + {{/each}} + {{#if (ne benchmark.base_writes "0")}} + .saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}})) + {{/if}} + {{#each benchmark.component_writes as |cw|}} + .saturating_add(T::DbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into()))) + {{/each}} + } + {{/each}} +} + +// For backwards compatibility and tests +impl WeightInfo for () { + {{#each benchmarks as |benchmark|}} + {{#each benchmark.comments as |comment|}} + // {{comment}} + {{/each}} + {{#each benchmark.component_ranges as |range|}} + /// The range of component `{{range.name}}` is `[{{range.min}}, {{range.max}}]`. + {{/each}} + fn {{benchmark.name~}} + ( + {{~#each benchmark.components as |c| ~}} + {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} + ) -> Weight { + // Minimum execution time: {{underscore benchmark.min_execution_time}} nanoseconds. + Weight::from_ref_time({{underscore benchmark.base_weight}}) + {{#each benchmark.component_weight as |cw|}} + // Standard Error: {{underscore cw.error}} + .saturating_add(Weight::from_ref_time({{underscore cw.slope}}).saturating_mul({{cw.name}}.into())) + {{/each}} + {{#if (ne benchmark.base_reads "0")}} + .saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}})) + {{/if}} + {{#each benchmark.component_reads as |cr|}} + .saturating_add(RocksDbWeight::get().reads(({{cr.slope}}_u64).saturating_mul({{cr.name}}.into()))) + {{/each}} + {{#if (ne benchmark.base_writes "0")}} + .saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}})) + {{/if}} + {{#each benchmark.component_writes as |cw|}} + .saturating_add(RocksDbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into()))) + {{/each}} + } + {{/each}} +} diff --git a/Cargo.lock b/Cargo.lock index e9d0501e..5f282625 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6099,6 +6099,7 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", + "sp-std", ] [[package]] diff --git a/HEADER-APACHE2 b/HEADER-APACHE2 new file mode 100644 index 00000000..52edacd5 --- /dev/null +++ b/HEADER-APACHE2 @@ -0,0 +1,16 @@ +// This file is part of Substrate. + +// Copyright (C) 2022 Hashed Network. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. diff --git a/pallets/fruniques/src/benchmarking.rs b/pallets/fruniques/src/benchmarking.rs index d496a9fc..1e565775 100644 --- a/pallets/fruniques/src/benchmarking.rs +++ b/pallets/fruniques/src/benchmarking.rs @@ -1,11 +1,60 @@ //! Benchmarking setup for pallet-template +#![cfg(feature = "runtime-benchmarks")] use super::*; +use frame_benchmarking::{ + account, benchmarks_instance_pallet, whitelist_account, whitelisted_caller, +}; -#[allow(unused)] -use crate::Pallet as Template; -use frame_benchmarking::{benchmarks, whitelisted_caller}; -use frame_system::RawOrigin; +use frame_support::{ + dispatch::UnfilteredDispatchable, + traits::{EnsureOrigin, Get}, + BoundedVec, +}; + +use frame_system::RawOrigin as SystemOrigin; +use sp_runtime::traits::Bounded; +use sp_std::prelude::*; + +use crate::Pallet as Fruniques; + +fn dummy_description() -> BoundedVec { + BoundedVec::::try_from(b"dummy description".to_vec()).unwrap() +} + +fn dummy_attributes() -> Vec<(BoundedVec, BoundedVec)> { + vec![( + BoundedVec::::try_from(b"dummy key".encode()) + .expect("Error on encoding key to BoundedVec"), + BoundedVec::::try_from(b"dummy value".encode()) + .expect("Error on encoding value to BoundedVec"), + )] +} + +fn dummy_empty_attributes() -> Vec<(BoundedVec, BoundedVec)> { + vec![] +} + +fn create_collection( +) -> (T::AccountId, AccountIdLookupOf, T::CollectionDescription) { + let caller: T::AccountId = whitelisted_caller(); + let caller_lookup = T::Lookup::unlookup(caller.clone()); + let collection_description = dummy_description(); + assert!(Fruniques::::create_collection( + SystemOrigin::Signed(caller.clone()).into(), + collection_description.clone(), + ) + .is_ok()); + (caller, caller_lookup, collection_description) +} + +fn spawn( +) -> (T::AccountId, AccountIdLookupOf, T::CollectionDescription, Attributes, Some(HierarchicalInfo)) { + let caller: T::AccountId = whitelisted_caller(); + let caller_lookup = T::Lookup::unlookup(caller.clone()); + let collection_description = dummy_description(); + let attributes = dummy_attributes(); +} benchmarks! { do_something { diff --git a/pallets/template/Cargo.toml b/pallets/template/Cargo.toml index a219ae6b..bd55e682 100644 --- a/pallets/template/Cargo.toml +++ b/pallets/template/Cargo.toml @@ -20,11 +20,13 @@ scale-info = { version = "2.1.1", default-features = false, features = ["derive" frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27"} frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" } frame-benchmarking = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27", optional = true } +sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" } [dev-dependencies] sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" } sp-io = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" } sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" } +sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" } [features] default = ["std"] @@ -34,6 +36,7 @@ std = [ "frame-support/std", "frame-system/std", "frame-benchmarking/std", + "sp-std/std", ] runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"] diff --git a/pallets/template/src/lib.rs b/pallets/template/src/lib.rs index 3d11317b..752da00e 100644 --- a/pallets/template/src/lib.rs +++ b/pallets/template/src/lib.rs @@ -4,7 +4,8 @@ /// Learn more about FRAME and the core library of Substrate FRAME pallets: /// pub use pallet::*; - +pub mod weights; +pub use weights::*; #[cfg(test)] mod mock; @@ -17,6 +18,8 @@ mod benchmarking; #[frame_support::pallet] pub mod pallet { + use super::*; + use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; @@ -25,6 +28,7 @@ use frame_support::pallet_prelude::*; pub trait Config: frame_system::Config { /// Because this pallet emits events, it depends on the runtime's definition of an event. type Event: From> + IsType<::Event>; + type WeightInfo: WeightInfo; } #[pallet::pallet] @@ -72,7 +76,7 @@ use frame_support::pallet_prelude::*; impl Pallet { /// An example dispatchable that takes a singles value as a parameter, writes the value to /// storage and emits an event. This function must be dispatched by a signed extrinsic. - #[pallet::weight(10_000 + T::DbWeight::get().writes(1))] + #[pallet::weight((T::WeightInfo::do_something(100), DispatchClass::Operational))] pub fn do_something(origin: OriginFor, something: u32) -> DispatchResult { // Check that the extrinsic was signed and get the signer. // This function will return an error if the extrinsic is not signed. diff --git a/pallets/template/src/mock.rs b/pallets/template/src/mock.rs index 733ac79d..adc65b7b 100644 --- a/pallets/template/src/mock.rs +++ b/pallets/template/src/mock.rs @@ -56,6 +56,7 @@ impl system::Config for Test { impl pallet_template::Config for Test { type Event = Event; + type WeightInfo = (); } // Build genesis storage according to the mock runtime. diff --git a/pallets/template/src/weights.rs b/pallets/template/src/weights.rs new file mode 100644 index 00000000..dc709e72 --- /dev/null +++ b/pallets/template/src/weights.rs @@ -0,0 +1,49 @@ + +//! Autogenerated weights for `pallet_template` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2022-12-05, STEPS: `20`, REPEAT: 10, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: `hashed`, CPU: `11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz` +//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// ./target/release/hashed +// benchmark +// pallet +// --chain +// dev +// --pallet +// pallet_template +// --extrinsic +// * +// --steps +// 20 +// --repeat +// 10 +// --output +// pallets/template/src/weights.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::{traits::Get, weights::Weight}; +use sp_std::marker::PhantomData; + +// Weights functions needed for the pallet_template pallet +// pub trait WeightInfo { +// fn do_something() -> Weight; +// } + +/// Weight functions for `pallet_template`. +pub struct WeightInfo(PhantomData); +impl pallet_template::WeightInfo for WeightInfo { + // Storage: TemplateModule Something (r:0 w:1) + /// The range of component `s` is `[0, 100]`. + fn do_something(s: u32, ) -> Weight { + (12_465_000 as Weight) + // Standard Error: 1_000 + .saturating_add((3_000 as Weight).saturating_mul(s as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } +} diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 065621b9..7133ed38 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -263,7 +263,6 @@ impl pallet_balances::Config for Runtime { type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; - type WeightInfo = pallet_balances::weights::SubstrateWeight; } parameter_types! { @@ -331,6 +330,7 @@ impl pallet_recovery::Config for Runtime { /// Configure the pallet-template in pallets/template. impl pallet_template::Config for Runtime { type Event = Event; + type WeightInfo = pallet_template::weights::SubstrateWeight; } parameter_types! { @@ -537,8 +537,6 @@ impl pallet_uniques::Config for Runtime { type KeyLimit = KeyLimit; type ValueLimit = ValueLimit; type WeightInfo = pallet_uniques::weights::SubstrateWeight; - #[cfg(feature = "runtime-benchmarks")] - type Helper = (); type CreateOrigin = AsEnsureOriginWithArg>; type Locker = (); } @@ -826,6 +824,23 @@ pub type Executive = frame_executive::Executive< AllPalletsWithSystem, >; +#[cfg(feature = "runtime-benchmarks")] +#[macro_use] +extern crate frame_benchmarking; + +#[cfg(feature = "runtime-benchmarks")] +mod benches { + define_benchmarks!( + [frame_system, SystemBench::] + [pallet_balances, Balances] + [pallet_session, SessionBench::] + [pallet_timestamp, Timestamp] + [pallet_collator_selection, CollatorSelection] + [cumulus_pallet_xcmp_queue, XcmpQueue] + [pallet_template, Template] + ); +} + impl_runtime_apis! { impl sp_api::Core for Runtime { fn version() -> RuntimeVersion { diff --git a/scripts/start_benchmarking.sh b/scripts/start_benchmarking.sh new file mode 100755 index 00000000..805ac4b8 --- /dev/null +++ b/scripts/start_benchmarking.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +echo "*** Initializing becnhmarking" + +cargo build --package node-template --release --features runtime-benchmarks + +./target/release/hashed benchmark pallet \  1  6.99  100% +--chain dev \ +--pallet pallet_template \ +--extrinsic '*' \ +--steps 20 \ +--repeat 10 \ +--output pallets/template/src/weights.rs