WeightInfo for System, Timestamp, and Utility (#6868)

* initial updates to system

* fix compile

* Update writer.rs

* update weights

* finish system weights

* timestamp weights

* utility weight

* Fix overflow in weight calculations

* add back weight notes

* Update for whitelisted benchmarks

* add trait bounds

* Revert "add trait bounds"

This reverts commit 12b08b7189aa3969f96fa19b211a370860fdb240.

* Update weights for unaccounted for read
This commit is contained in:
Shawn Tabrizi
2020-08-17 22:59:23 +02:00
committed by GitHub
parent 93c73b6509
commit 74a583d147
14 changed files with 316 additions and 68 deletions
+3 -3
View File
@@ -182,13 +182,13 @@ impl frame_system::Trait for Runtime {
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SystemWeightInfo = weights::frame_system::WeightInfo;
}
impl pallet_utility::Trait for Runtime {
type Event = Event;
type Call = Call;
type WeightInfo = ();
type WeightInfo = weights::pallet_utility::WeightInfo;
}
parameter_types! {
@@ -352,7 +352,7 @@ impl pallet_timestamp::Trait for Runtime {
type Moment = Moment;
type OnTimestampSet = Babe;
type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
type WeightInfo = weights::pallet_timestamp::WeightInfo;
}
parameter_types! {
@@ -0,0 +1,58 @@
// This file is part of Substrate.
// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd.
// 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.
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc5
#![allow(unused_parens)]
use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
pub struct WeightInfo;
impl frame_system::WeightInfo for WeightInfo {
// WARNING! Some components were not used: ["b"]
fn remark() -> Weight {
(1305000 as Weight)
}
fn set_heap_pages() -> Weight {
(2023000 as Weight)
.saturating_add(DbWeight::get().writes(1 as Weight))
}
// WARNING! Some components were not used: ["d"]
fn set_changes_trie_config() -> Weight {
(10026000 as Weight)
.saturating_add(DbWeight::get().reads(1 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
fn set_storage(i: u32, ) -> Weight {
(0 as Weight)
.saturating_add((656000 as Weight).saturating_mul(i as Weight))
.saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(i as Weight)))
}
fn kill_storage(i: u32, ) -> Weight {
(4327000 as Weight)
.saturating_add((478000 as Weight).saturating_mul(i as Weight))
.saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(i as Weight)))
}
fn kill_prefix(p: u32, ) -> Weight {
(8349000 as Weight)
.saturating_add((838000 as Weight).saturating_mul(p as Weight))
.saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(p as Weight)))
}
fn suicide() -> Weight {
(29247000 as Weight)
}
}
@@ -15,5 +15,8 @@
//! A list of the different weight modules for our runtime.
pub mod frame_system;
pub mod pallet_balances;
pub mod pallet_democracy;
pub mod pallet_timestamp;
pub mod pallet_utility;
@@ -0,0 +1,34 @@
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
// 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.
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc5
#![allow(unused_parens)]
use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
pub struct WeightInfo;
impl pallet_timestamp::WeightInfo for WeightInfo {
// WARNING! Some components were not used: ["t"]
fn set() -> Weight {
(9133000 as Weight)
.saturating_add(DbWeight::get().reads(2 as Weight))
.saturating_add(DbWeight::get().writes(1 as Weight))
}
// WARNING! Some components were not used: ["t"]
fn on_finalize() -> Weight {
(5915000 as Weight)
}
}
@@ -0,0 +1,35 @@
// This file is part of Substrate.
// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd.
// 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.
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc5
#![allow(unused_parens)]
#![allow(unused_imports)]
use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
pub struct WeightInfo;
impl pallet_utility::WeightInfo for WeightInfo {
fn batch(c: u32, ) -> Weight {
(16461000 as Weight)
.saturating_add((1982000 as Weight).saturating_mul(c as Weight))
}
// WARNING! Some components were not used: ["u"]
fn as_derivative() -> Weight {
(4086000 as Weight)
}
}
+9 -12
View File
@@ -25,6 +25,7 @@ use sp_std::prelude::*;
use sp_core::{ChangesTrieConfiguration, storage::well_known_keys};
use sp_runtime::traits::Hash;
use frame_benchmarking::{benchmarks, account};
use frame_support::traits::Get;
use frame_support::storage::{self, StorageMap};
use frame_system::{Module as System, Call, RawOrigin, DigestItemOf, AccountInfo};
@@ -39,29 +40,26 @@ benchmarks! {
_ { }
remark {
// # of Bytes
let b in 0 .. 16_384;
let b in 0 .. T::MaximumBlockLength::get();
let remark_message = vec![1; b as usize];
let caller = account("caller", 0, SEED);
}: _(RawOrigin::Signed(caller), remark_message)
set_heap_pages {
// Heap page size
let i in 0 .. u32::max_value();
}: _(RawOrigin::Root, i.into())
}: _(RawOrigin::Root, Default::default())
// `set_code` was not benchmarked because it is pretty hard to come up with a real
// Wasm runtime to test the upgrade with. But this is okay because we will make
// `set_code` take a full block anyway.
#[extra]
set_code_without_checks {
// Version number
let b in 0 .. 16_384;
let code = vec![1; b as usize];
// Assume Wasm ~4MB
let code = vec![1; 4_000_000 as usize];
}: _(RawOrigin::Root, code)
verify {
let current_code = storage::unhashed::get_raw(well_known_keys::CODE).ok_or("Code not stored.")?;
assert_eq!(current_code.len(), b as usize);
assert_eq!(current_code.len(), 4_000_000 as usize);
}
set_changes_trie_config {
@@ -141,16 +139,15 @@ benchmarks! {
}
suicide {
let n in 1 .. 1000;
let caller: T::AccountId = account("caller", 0, SEED);
let account_info = AccountInfo::<T::Index, T::AccountData> {
nonce: n.into(),
nonce: 1337.into(),
refcount: 0,
data: T::AccountData::default()
};
frame_system::Account::<T>::insert(&caller, account_info);
let new_account_info = System::<T>::account(caller.clone());
assert_eq!(new_account_info.nonce, n.into());
assert_eq!(new_account_info.nonce, 1337.into());
}: _(RawOrigin::Signed(caller.clone()))
verify {
let account_info = System::<T>::account(&caller);
@@ -0,0 +1,57 @@
// This file is part of Substrate.
// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd.
// 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.
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc5
#![allow(unused_parens)]
use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
impl crate::WeightInfo for () {
// WARNING! Some components were not used: ["b"]
fn remark() -> Weight {
(1305000 as Weight)
}
fn set_heap_pages() -> Weight {
(2023000 as Weight)
.saturating_add(DbWeight::get().writes(1 as Weight))
}
// WARNING! Some components were not used: ["d"]
fn set_changes_trie_config() -> Weight {
(10026000 as Weight)
.saturating_add(DbWeight::get().reads(1 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
fn set_storage(i: u32, ) -> Weight {
(0 as Weight)
.saturating_add((656000 as Weight).saturating_mul(i as Weight))
.saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(i as Weight)))
}
fn kill_storage(i: u32, ) -> Weight {
(4327000 as Weight)
.saturating_add((478000 as Weight).saturating_mul(i as Weight))
.saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(i as Weight)))
}
fn kill_prefix(p: u32, ) -> Weight {
(8349000 as Weight)
.saturating_add((838000 as Weight).saturating_mul(p as Weight))
.saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(p as Weight)))
}
fn suicide() -> Weight {
(29247000 as Weight)
}
}
+12 -26
View File
@@ -139,6 +139,7 @@ mod extensions;
mod weights;
#[cfg(test)]
mod tests;
mod default_weights;
pub use extensions::{
check_mortality::CheckMortality, check_genesis::CheckGenesis, check_nonce::CheckNonce,
@@ -159,25 +160,13 @@ pub fn extrinsics_data_root<H: Hash>(xts: Vec<Vec<u8>>) -> H::Output {
}
pub trait WeightInfo {
fn remark(b: u32, ) -> Weight;
fn set_heap_pages(i: u32, ) -> Weight;
fn set_code_without_checks(b: u32, ) -> Weight;
fn set_changes_trie_config(d: u32, ) -> Weight;
fn remark() -> Weight;
fn set_heap_pages() -> Weight;
fn set_changes_trie_config() -> Weight;
fn set_storage(i: u32, ) -> Weight;
fn kill_storage(i: u32, ) -> Weight;
fn kill_prefix(p: u32, ) -> Weight;
fn suicide(n: u32, ) -> Weight;
}
impl WeightInfo for () {
fn remark(_b: u32, ) -> Weight { 1_000_000_000 }
fn set_heap_pages(_i: u32, ) -> Weight { 1_000_000_000 }
fn set_code_without_checks(_b: u32, ) -> Weight { 1_000_000_000 }
fn set_changes_trie_config(_d: u32, ) -> Weight { 1_000_000_000 }
fn set_storage(_i: u32, ) -> Weight { 1_000_000_000 }
fn kill_storage(_i: u32, ) -> Weight { 1_000_000_000 }
fn kill_prefix(_p: u32, ) -> Weight { 1_000_000_000 }
fn suicide(_n: u32, ) -> Weight { 1_000_000_000 }
fn suicide() -> Weight;
}
pub trait Trait: 'static + Eq + Clone {
@@ -564,7 +553,7 @@ decl_module! {
/// - Base Weight: 0.665 µs, independent of remark length.
/// - No DB operations.
/// # </weight>
#[weight = 700_000]
#[weight = T::SystemWeightInfo::remark()]
fn remark(origin, _remark: Vec<u8>) {
ensure_signed(origin)?;
}
@@ -577,7 +566,7 @@ decl_module! {
/// - Base Weight: 1.405 µs
/// - 1 write to HEAP_PAGES
/// # </weight>
#[weight = (T::DbWeight::get().writes(1) + 1_500_000, DispatchClass::Operational)]
#[weight = (T::SystemWeightInfo::set_heap_pages(), DispatchClass::Operational)]
fn set_heap_pages(origin, pages: u64) {
ensure_root(origin)?;
storage::unhashed::put_raw(well_known_keys::HEAP_PAGES, &pages.encode());
@@ -627,7 +616,7 @@ decl_module! {
/// - DB Weight:
/// - Writes: Changes Trie, System Digest
/// # </weight>
#[weight = (T::DbWeight::get().writes(2) + 10_000_000, DispatchClass::Operational)]
#[weight = (T::SystemWeightInfo::set_changes_trie_config(), DispatchClass::Operational)]
pub fn set_changes_trie_config(origin, changes_trie_config: Option<ChangesTrieConfiguration>) {
ensure_root(origin)?;
match changes_trie_config.clone() {
@@ -653,8 +642,7 @@ decl_module! {
/// - Writes: Number of items
/// # </weight>
#[weight = (
T::DbWeight::get().writes(items.len() as Weight)
.saturating_add((items.len() as Weight).saturating_mul(600_000)),
T::SystemWeightInfo::set_storage(items.len() as u32),
DispatchClass::Operational,
)]
fn set_storage(origin, items: Vec<KeyValue>) {
@@ -673,8 +661,7 @@ decl_module! {
/// - Writes: Number of items
/// # </weight>
#[weight = (
T::DbWeight::get().writes(keys.len() as Weight)
.saturating_add((keys.len() as Weight).saturating_mul(400_000)),
T::SystemWeightInfo::kill_storage(keys.len() as u32),
DispatchClass::Operational,
)]
fn kill_storage(origin, keys: Vec<Key>) {
@@ -696,8 +683,7 @@ decl_module! {
/// - Writes: Number of subkeys + 1
/// # </weight>
#[weight = (
T::DbWeight::get().writes(Weight::from(*_subkeys) + 1)
.saturating_add((Weight::from(*_subkeys) + 1).saturating_mul(850_000)),
T::SystemWeightInfo::kill_prefix(_subkeys.saturating_add(1)),
DispatchClass::Operational,
)]
fn kill_prefix(origin, prefix: Key, _subkeys: u32) {
@@ -715,7 +701,7 @@ decl_module! {
/// Base Weight: 8.626 µs
/// No DB Read or Write operations because caller is already in overlay
/// # </weight>
#[weight = (10_000_000, DispatchClass::Operational)]
#[weight = (T::SystemWeightInfo::suicide(), DispatchClass::Operational)]
pub fn suicide(origin) {
let who = ensure_signed(origin)?;
let account = Account::<T>::get(&who);
@@ -0,0 +1,35 @@
// This file is part of Substrate.
// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd.
// 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.
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc5
#![allow(unused_parens)]
use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
impl crate::WeightInfo for () {
// WARNING! Some components were not used: ["t"]
fn set() -> Weight {
(9133000 as Weight)
.saturating_add(DbWeight::get().reads(2 as Weight))
.saturating_add(DbWeight::get().writes(1 as Weight))
}
// WARNING! Some components were not used: ["t"]
fn on_finalize() -> Weight {
(5915000 as Weight)
}
}
+5 -13
View File
@@ -93,6 +93,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
mod benchmarking;
mod default_weights;
use sp_std::{result, cmp};
use sp_inherents::{ProvideInherent, InherentData, InherentIdentifier};
@@ -116,13 +117,8 @@ use sp_timestamp::{
};
pub trait WeightInfo {
fn set(t: u32, ) -> Weight;
fn on_finalize(t: u32, ) -> Weight;
}
impl WeightInfo for () {
fn set(_t: u32, ) -> Weight { 1_000_000_000 }
fn on_finalize(_t: u32, ) -> Weight { 1_000_000_000 }
fn set() -> Weight;
fn on_finalize() -> Weight;
}
/// The module configuration trait
@@ -166,12 +162,9 @@ decl_module! {
/// - `O(T)` where `T` complexity of `on_timestamp_set`
/// - 1 storage read and 1 storage mutation (codec `O(1)`). (because of `DidUpdate::take` in `on_finalize`)
/// - 1 event handler `on_timestamp_set` `O(T)`.
/// - Benchmark: 7.678 (min squares analysis)
/// - NOTE: This benchmark was done for a runtime with insignificant `on_timestamp_set` handlers.
/// New benchmarking is needed when adding new handlers.
/// # </weight>
#[weight = (
T::DbWeight::get().reads_writes(2, 1) + 8_000_000,
T::WeightInfo::set(),
DispatchClass::Mandatory
)]
fn set(origin, #[compact] now: T::Moment) {
@@ -191,13 +184,12 @@ decl_module! {
/// dummy `on_initialize` to return the weight used in `on_finalize`.
fn on_initialize() -> Weight {
// weight of `on_finalize`
5_000_000
T::WeightInfo::on_finalize()
}
/// # <weight>
/// - `O(1)`
/// - 1 storage deletion (codec `O(1)`).
/// - Benchmark: 4.928 µs (min squares analysis)
/// # </weight>
fn on_finalize() {
assert!(<Self as Store>::DidUpdate::take(), "Timestamp must be updated once in the block");
@@ -0,0 +1,34 @@
// This file is part of Substrate.
// Copyright (C) 2019-2020 Parity Technologies (UK) Ltd.
// 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.
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc5
#![allow(unused_parens)]
#![allow(unused_imports)]
use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
impl crate::WeightInfo for () {
fn batch(c: u32, ) -> Weight {
(16461000 as Weight)
.saturating_add((1982000 as Weight).saturating_mul(c as Weight))
}
// WARNING! Some components were not used: ["u"]
fn as_derivative() -> Weight {
(4086000 as Weight)
}
}
+6 -13
View File
@@ -69,15 +69,11 @@ use sp_runtime::{DispatchError, DispatchResult, traits::Dispatchable};
mod tests;
mod benchmarking;
mod default_weights;
pub trait WeightInfo {
fn batch(c: u32, ) -> Weight;
fn as_derivative(u: u32, ) -> Weight;
}
impl WeightInfo for () {
fn batch(_c: u32, ) -> Weight { 1_000_000_000 }
fn as_derivative(_u: u32, ) -> Weight { 1_000_000_000 }
fn as_derivative() -> Weight;
}
/// Configuration trait.
@@ -145,7 +141,8 @@ decl_module! {
#[weight = (
calls.iter()
.map(|call| call.get_dispatch_info().weight)
.fold(15_000_000, |a: Weight, n| a.saturating_add(n).saturating_add(1_000_000)),
.fold(0, |total: Weight, weight: Weight| total.saturating_add(weight))
.saturating_add(T::WeightInfo::batch(calls.len() as u32)),
{
let all_operational = calls.iter()
.map(|call| call.get_dispatch_info().class)
@@ -186,13 +183,9 @@ decl_module! {
/// NOTE: Prior to version *12, this was called `as_limited_sub`.
///
/// The dispatch origin for this call must be _Signed_.
///
/// # <weight>
/// - Base weight: 2.861 µs
/// - Plus the weight of the `call`
/// # </weight>
#[weight = (
call.get_dispatch_info().weight.saturating_add(3_000_000),
T::WeightInfo::as_derivative()
.saturating_add(call.get_dispatch_info().weight),
call.get_dispatch_info().class,
)]
fn as_derivative(origin, index: u16, call: Box<<T as Trait>::Call>) -> DispatchResult {
+19 -1
View File
@@ -54,7 +54,7 @@ impl_outer_dispatch! {
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockWeight: Weight = Weight::max_value();
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
@@ -121,6 +121,7 @@ type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type Utility = Module<Test>;
use frame_system::Call as SystemCall;
use pallet_balances::Call as BalancesCall;
use pallet_balances::Error as BalancesError;
@@ -236,3 +237,20 @@ fn batch_early_exit_works() {
assert_eq!(Balances::free_balance(2), 15);
});
}
#[test]
fn batch_weight_calculation_doesnt_overflow() {
new_test_ext().execute_with(|| {
let big_call = Call::System(SystemCall::fill_block(Perbill::from_percent(50)));
assert_eq!(big_call.get_dispatch_info().weight, Weight::max_value() / 2);
// 3 * 50% saturates to 100%
let batch_call = Call::Utility(crate::Call::batch(vec![
big_call.clone(),
big_call.clone(),
big_call.clone(),
]));
assert_eq!(batch_call.get_dispatch_info().weight, Weight::max_value());
});
}
@@ -107,6 +107,12 @@ pub fn write_results(batches: &[BenchmarkBatch]) -> Result<(), std::io::Error> {
VERSION,
)?;
// allow statements
write!(
file,
"#![allow(unused_parens)]\n#![allow(unused_imports)]\n\n",
)?;
// general imports
write!(
file,