Skip to content

Commit

Permalink
WeightInfo for System, Timestamp, and Utility (paritytech#6868)
Browse files Browse the repository at this point in the history
* 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 12b08b7.

* Update weights for unaccounted for read
  • Loading branch information
shawntabrizi authored Aug 17, 2020
1 parent 399421a commit 8e1ed7d
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 68 deletions.
6 changes: 3 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down Expand Up @@ -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! {
Expand Down
58 changes: 58 additions & 0 deletions bin/node/runtime/src/weights/frame_system.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 3 additions & 0 deletions bin/node/runtime/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
34 changes: 34 additions & 0 deletions bin/node/runtime/src/weights/pallet_timestamp.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
35 changes: 35 additions & 0 deletions bin/node/runtime/src/weights/pallet_utility.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
21 changes: 9 additions & 12 deletions frame/system/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
57 changes: 57 additions & 0 deletions frame/system/src/default_weights.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
38 changes: 12 additions & 26 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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)?;
}
Expand All @@ -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());
Expand Down Expand Up @@ -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() {
Expand All @@ -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>) {
Expand All @@ -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>) {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 8e1ed7d

Please sign in to comment.