Skip to content

Commit 237bff7

Browse files
committed
benchmark update
1 parent da04eff commit 237bff7

File tree

10 files changed

+274
-84
lines changed

10 files changed

+274
-84
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
.cargo
1111

1212
# The cache for chain data in container
13-
.local
13+
.local
14+
15+
# Temp files for local development
16+
maintain

.maintain/frame-weight-template.hbs

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! Autogenerated weights for {{pallet}}
19+
//!
20+
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}}
21+
//! DATE: {{date}}, STEPS: `{{cmd.steps}}`, REPEAT: {{cmd.repeat}}, LOW RANGE: `{{cmd.lowest_range_values}}`, HIGH RANGE: `{{cmd.highest_range_values}}`
22+
//! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}}, CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}}
23+
24+
// Executed Command:
25+
{{#each args as |arg|~}}
26+
// {{arg}}
27+
{{/each}}
28+
29+
#![allow(unused_parens)]
30+
#![allow(unused_imports)]
31+
32+
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
33+
use sp_std::marker::PhantomData;
34+
35+
/// Weight functions needed for {{pallet}}.
36+
pub trait WeightInfo {
37+
{{~#each benchmarks as |benchmark|}}
38+
fn {{benchmark.name~}}
39+
(
40+
{{~#each benchmark.components as |c| ~}}
41+
{{c.name}}: u32, {{/each~}}
42+
) -> Weight;
43+
{{~/each}}
44+
}
45+
46+
/// Weights for {{pallet}} using the Substrate node and recommended hardware.
47+
pub struct SubstrateWeight<T>(PhantomData<T>);
48+
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
49+
{{~#each benchmarks as |benchmark|}}
50+
fn {{benchmark.name~}}
51+
(
52+
{{~#each benchmark.components as |c| ~}}
53+
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
54+
) -> Weight {
55+
({{underscore benchmark.base_weight}} as Weight)
56+
{{~#each benchmark.component_weight as |cw|}}
57+
// Standard Error: {{underscore cw.error}}
58+
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
59+
{{~/each}}
60+
{{~#if (ne benchmark.base_reads "0")}}
61+
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight))
62+
{{~/if}}
63+
{{~#each benchmark.component_reads as |cr|}}
64+
.saturating_add(T::DbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))
65+
{{~/each}}
66+
{{~#if (ne benchmark.base_writes "0")}}
67+
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as Weight))
68+
{{~/if}}
69+
{{~#each benchmark.component_writes as |cw|}}
70+
.saturating_add(T::DbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))
71+
{{~/each}}
72+
}
73+
{{~/each}}
74+
}
75+
76+
// For backwards compatibility and tests
77+
impl WeightInfo for () {
78+
{{~#each benchmarks as |benchmark|}}
79+
fn {{benchmark.name~}}
80+
(
81+
{{~#each benchmark.components as |c| ~}}
82+
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
83+
) -> Weight {
84+
({{underscore benchmark.base_weight}} as Weight)
85+
{{~#each benchmark.component_weight as |cw|}}
86+
// Standard Error: {{underscore cw.error}}
87+
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
88+
{{~/each}}
89+
{{~#if (ne benchmark.base_reads "0")}}
90+
.saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight))
91+
{{~/if}}
92+
{{~#each benchmark.component_reads as |cr|}}
93+
.saturating_add(RocksDbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))
94+
{{~/each}}
95+
{{~#if (ne benchmark.base_writes "0")}}
96+
.saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}} as Weight))
97+
{{~/if}}
98+
{{~#each benchmark.component_writes as |cw|}}
99+
.saturating_add(RocksDbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))
100+
{{~/each}}
101+
}
102+
{{~/each}}
103+
}

Cargo.lock

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ members = [
1010
# 'pallets/data-type',
1111
# 'pallets/genesis-config',
1212
# 'pallets/weight',
13-
# 'pallets/benchmark-demo',
13+
'pallets/benchmark-demo',
1414
'runtime',
1515
]

pallets/benchmark-demo/Cargo.toml

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ homepage = 'https://substrate.dev'
66
license = 'Unlicense'
77
name = 'pallet-benchmark-demo'
88
repository = 'https://github.com/kaichaosun/play-substrate/'
9-
version = '2.0.0'
9+
version = '3.0.0'
1010

1111
[package.metadata.docs.rs]
1212
targets = ['x86_64-unknown-linux-gnu']
@@ -15,13 +15,13 @@ targets = ['x86_64-unknown-linux-gnu']
1515
default-features = false
1616
features = ['derive']
1717
package = 'parity-scale-codec'
18-
version = '1.3.4'
18+
version = '2.0.0'
1919

2020
[dependencies]
21-
frame-support = { default-features = false, version = '2.0.0' }
22-
frame-system = { default-features = false, version = '2.0.0' }
23-
frame-benchmarking = { default-features = false, version = '2.0.0', optional = true }
24-
sp-std = { default-features = false, version = '2.0.0' }
21+
frame-support = { default-features = false, version = '3.0.0' }
22+
frame-system = { default-features = false, version = '3.0.0' }
23+
frame-benchmarking = { default-features = false, version = '3.0.0', optional = true }
24+
sp-std = { default-features = false, version = '3.0.0' }
2525

2626
[features]
2727
default = ['std']

pallets/benchmark-demo/src/benchmarking.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@ use frame_system::RawOrigin;
99
use sp_std::prelude::*;
1010

1111
benchmarks!{
12-
_ {
13-
let b in 1 .. 1000 => ();
14-
}
15-
1612
do_something {
17-
let b in ...;
13+
let b in 1 .. 1000;
1814
let caller = account("caller", 0, 0);
1915
}: _ (RawOrigin::Signed(caller), b.into())
2016
verify {
21-
let value = Something::get();
17+
let value = Something::<T>::get();
2218
assert_eq!(value, b.into());
2319
}
2420
}

pallets/benchmark-demo/src/lib.rs

+61-67
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,84 @@
22

33
/// A FRAME pallet for demo benchmark
44
5-
use frame_support::{
6-
decl_module, decl_storage, decl_event, decl_error, dispatch,
7-
traits::{
8-
Get
9-
},
10-
};
11-
use frame_system::{self as system, ensure_signed};
5+
pub use pallet::*;
126

137
mod benchmarking;
8+
pub mod weights;
149

15-
/// The pallet's configuration trait.
16-
pub trait Trait: system::Trait {
17-
// Add other types and constants required to configure this pallet.
10+
#[frame_support::pallet]
11+
pub mod pallet {
12+
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
13+
use frame_system::pallet_prelude::*;
14+
pub use crate::weights::WeightInfo;
1815

19-
/// The overarching event type.
20-
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
21-
}
16+
/// Configure the pallet by specifying the parameters and types on which it depends.
17+
#[pallet::config]
18+
pub trait Config: frame_system::Config {
19+
/// Because this pallet emits events, it depends on the runtime's definition of an event.
20+
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
2221

23-
// This pallet's storage items.
24-
decl_storage! {
25-
// It is important to update your storage name so that your pallet's
26-
// storage items are isolated from other pallets.
27-
// ---------------------------------vvvvvvvvvvvvvv
28-
trait Store for Module<T: Trait> as TemplateModule {
29-
// Just a dummy storage item.
30-
// Here we are declaring a StorageValue, `Something` as a Option<u32>
31-
// `get(fn something)` is the default getter which returns either the stored `u32` or `None` if nothing stored
32-
Something get(fn something): Option<u32>;
22+
/// Information on runtime weights.
23+
type WeightInfo: WeightInfo;
3324
}
34-
}
3525

36-
// The pallet's events
37-
decl_event!(
38-
pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {
39-
/// Just a dummy event.
40-
/// Event `Something` is declared with a parameter of the type `u32` and `AccountId`
41-
/// To emit this event, we call the deposit function, from our runtime functions
42-
SomethingStored(u32, AccountId),
26+
#[pallet::pallet]
27+
#[pallet::generate_store(pub(super) trait Store)]
28+
pub struct Pallet<T>(_);
29+
30+
// The pallet's runtime storage items.
31+
// https://substrate.dev/docs/en/knowledgebase/runtime/storage
32+
// Learn more about declaring storage items:
33+
// https://substrate.dev/docs/en/knowledgebase/runtime/storage#declaring-storage-items
34+
#[pallet::storage]
35+
#[pallet::getter(fn something)]
36+
pub type Something<T> = StorageValue<_, u32>;
37+
38+
// Pallets use events to inform users when important changes are made.
39+
// https://substrate.dev/docs/en/knowledgebase/runtime/events
40+
#[pallet::event]
41+
#[pallet::metadata(T::AccountId = "AccountId")]
42+
#[pallet::generate_deposit(pub(super) fn deposit_event)]
43+
pub enum Event<T: Config> {
44+
/// Event documentation should end with an array that provides descriptive names for event
45+
/// parameters. [something, who]
46+
SomethingStored(u32, T::AccountId),
4347
}
44-
);
4548

46-
// The pallet's errors
47-
decl_error! {
48-
pub enum Error for Module<T: Trait> {
49-
/// Value was None
49+
// Errors inform users that something went wrong.
50+
#[pallet::error]
51+
pub enum Error<T> {
52+
/// Error names should be descriptive.
5053
NoneValue,
51-
/// Value reached maximum and cannot be incremented further
54+
/// Errors should have helpful documentation associated with them.
5255
StorageOverflow,
5356
}
54-
}
55-
56-
// The pallet's dispatchable functions.
57-
decl_module! {
58-
/// The module declaration.
59-
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
60-
// Initializing errors
61-
// this includes information about your errors in the node's metadata.
62-
// it is needed only if you are using errors in your pallet
63-
type Error = Error<T>;
6457

65-
// Initializing events
66-
// this is needed only if you are using events in your pallet
67-
fn deposit_event() = default;
58+
#[pallet::hooks]
59+
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
6860

69-
/// Just a dummy entry point.
70-
/// function that can be called by the external world as an extrinsics call
71-
/// takes a parameter of the type `AccountId`, stores it, and emits an event
72-
/// # <weight>
73-
/// - Base Weight: 44.66 µs
74-
/// - DB Weight: 1 Write
75-
/// # </weight>
76-
#[weight = T::DbWeight::get().writes(1) + 44_660_000]
77-
pub fn do_something(origin, something: u32) -> dispatch::DispatchResult {
78-
// Check it was signed and get the signer. See also: ensure_root and ensure_none
61+
// Dispatchable functions allows users to interact with the pallet and invoke state changes.
62+
// These functions materialize as "extrinsics", which are often compared to transactions.
63+
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
64+
#[pallet::call]
65+
impl<T: Config> Pallet<T> {
66+
/// An example dispatchable that takes a single value as a parameter, writes the value to
67+
/// storage and emits an event. This function must be dispatched by a signed extrinsic.
68+
#[pallet::weight(T::WeightInfo::do_something(*something))]
69+
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResultWithPostInfo {
70+
// Check that the extrinsic was signed and get the signer.
71+
// This function will return an error if the extrinsic is not signed.
72+
// https://substrate.dev/docs/en/knowledgebase/runtime/origin
7973
let who = ensure_signed(origin)?;
8074

81-
// Code to execute when something calls this.
82-
// For example: the following line stores the passed in u32 in the storage
83-
Something::put(something);
75+
// Update storage.
76+
<Something<T>>::put(something);
8477

85-
// Here we are raising the Something event
86-
Self::deposit_event(RawEvent::SomethingStored(something, who));
87-
Ok(())
78+
// Emit an event.
79+
Self::deposit_event(Event::SomethingStored(something, who));
80+
// Return a successful DispatchResultWithPostInfo
81+
Ok(().into())
8882
}
89-
9083
}
84+
9185
}

0 commit comments

Comments
 (0)