Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add benchmarking for parachain runtime paras pallet #3888

Merged
22 commits merged into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
00065e5
Crate basic barebones benchmarking infrastructure for paras
KiChjang Sep 17, 2021
0d967d5
Fill in benchmarking contents
KiChjang Sep 18, 2021
7122391
Merge branch 'master' of https://github.com/paritytech/polkadot into …
Sep 18, 2021
f5ceca5
cargo run --quiet --release --features=runtime-benchmarks -- benchmar…
Sep 18, 2021
0e4cac8
cargo run --quiet --release --features=runtime-benchmarks -- benchmar…
Sep 18, 2021
264bb07
Use autogenerated WeightInfos for kusama and westend
KiChjang Sep 18, 2021
e2fe358
cargo fmt
KiChjang Sep 18, 2021
20388c8
Use saturating_sub
KiChjang Sep 18, 2021
d92cb3f
Add missing import
KiChjang Sep 18, 2021
ee36011
Merge branch 'master' into kckyeung/parachains-paras-benchmarking
KiChjang Sep 18, 2021
5a368b2
Try and hit the worst possible time complexity as much as possible
KiChjang Sep 19, 2021
0b47140
cargo fmt
KiChjang Sep 19, 2021
0bd9cd2
Merge branch 'master' of https://github.com/paritytech/polkadot into …
Sep 19, 2021
f9ceaf3
cargo run --quiet --release --features=runtime-benchmarks -- benchmar…
Sep 19, 2021
69b4b5f
cargo run --quiet --release --features=runtime-benchmarks -- benchmar…
Sep 19, 2021
7c79304
Add a MAX_HEAD_DATA_SIZE constant
KiChjang Sep 20, 2021
496bf51
Prefill vectors with sample data for worst case complexity
KiChjang Sep 21, 2021
74c46f4
Merge branch 'master' of https://github.com/paritytech/polkadot into …
Sep 21, 2021
10b97f9
cargo run --quiet --release --features=runtime-benchmarks -- benchmar…
Sep 21, 2021
fcd3c43
Merge branch 'master' of https://github.com/paritytech/polkadot into …
Sep 21, 2021
0a0b1cc
cargo run --quiet --release --features=runtime-benchmarks -- benchmar…
Sep 21, 2021
b91f0e5
Improve comment on SAMPLE_SIZE constant
KiChjang Sep 21, 2021
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
1 change: 1 addition & 0 deletions runtime/common/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl shared::Config for Test {}
impl paras::Config for Test {
type Origin = Origin;
type Event = Event;
type WeightInfo = paras::weights::WeightInfo<Test>;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions runtime/common/src/paras_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ mod tests {
impl paras::Config for Test {
type Origin = Origin;
type Event = Event;
type WeightInfo = paras::weights::WeightInfo<Test>;
}

impl configuration::Config for Test {
Expand Down
3 changes: 3 additions & 0 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,7 @@ impl parachains_inclusion::Config for Runtime {
impl parachains_paras::Config for Runtime {
type Origin = Origin;
type Event = Event;
type WeightInfo = weights::runtime_parachains_paras::WeightInfo<Runtime>;
}

parameter_types! {
Expand Down Expand Up @@ -2003,6 +2004,7 @@ sp_api::impl_runtime_apis! {
list_benchmark!(list, extra, runtime_common::slots, Slots);
list_benchmark!(list, extra, runtime_common::paras_registrar, Registrar);
list_benchmark!(list, extra, runtime_parachains::configuration, Configuration);
list_benchmark!(list, extra, runtime_parachains::paras, Paras);
// Substrate
list_benchmark!(list, extra, pallet_bags_list, BagsList);
list_benchmark!(list, extra, pallet_balances, Balances);
Expand Down Expand Up @@ -2078,6 +2080,7 @@ sp_api::impl_runtime_apis! {
add_benchmark!(params, batches, runtime_common::slots, Slots);
add_benchmark!(params, batches, runtime_common::paras_registrar, Registrar);
add_benchmark!(params, batches, runtime_parachains::configuration, Configuration);
add_benchmark!(params, batches, runtime_parachains::paras, Paras);
// Substrate
add_benchmark!(params, batches, pallet_balances, Balances);
add_benchmark!(params, batches, pallet_bags_list, BagsList);
Expand Down
1 change: 1 addition & 0 deletions runtime/kusama/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ pub mod runtime_common_crowdloan;
pub mod runtime_common_paras_registrar;
pub mod runtime_common_slots;
pub mod runtime_parachains_configuration;
pub mod runtime_parachains_paras;
95 changes: 95 additions & 0 deletions runtime/kusama/src/weights/runtime_parachains_paras.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2017-2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Autogenerated weights for `runtime_parachains::paras`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2021-09-18, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("kusama-dev"), DB CACHE: 128

// Executed Command:
// target/release/polkadot
// benchmark
// --chain=kusama-dev
// --steps=50
// --repeat=20
// --pallet=runtime_parachains::paras
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --header=./file_header.txt
// --output=./runtime/kusama/src/weights/runtime_parachains_paras.rs


#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::Weight};
use sp_std::marker::PhantomData;

/// Weight functions for `runtime_parachains::paras`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> runtime_parachains::paras::WeightInfo for WeightInfo<T> {
// Storage: Paras CurrentCodeHash (r:1 w:1)
// Storage: Paras CodeByHashRefs (r:1 w:1)
// Storage: Paras PastCodeMeta (r:1 w:1)
// Storage: Paras PastCodePruning (r:1 w:1)
// Storage: Paras PastCodeHash (r:0 w:1)
// Storage: Paras CodeByHash (r:0 w:1)
fn force_set_current_code(c: u32, ) -> Weight {
(36_613_000 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(c as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(6 as Weight))
}
// Storage: Paras Heads (r:0 w:1)
fn force_set_current_head() -> Weight {
(16_152_000 as Weight)
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Configuration ActiveConfig (r:1 w:0)
// Storage: Paras FutureCodeUpgrades (r:1 w:1)
// Storage: Paras UpcomingUpgrades (r:1 w:1)
// Storage: Paras UpgradeCooldowns (r:1 w:1)
// Storage: System Digest (r:1 w:1)
// Storage: Paras CodeByHashRefs (r:1 w:1)
// Storage: Paras CodeByHash (r:0 w:1)
// Storage: Paras FutureCodeHash (r:0 w:1)
// Storage: Paras UpgradeRestrictionSignal (r:0 w:1)
fn force_schedule_code_upgrade(c: u32, ) -> Weight {
(42_270_000 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(c as Weight))
.saturating_add(T::DbWeight::get().reads(6 as Weight))
.saturating_add(T::DbWeight::get().writes(8 as Weight))
}
// Storage: Paras FutureCodeUpgrades (r:1 w:0)
// Storage: Paras Heads (r:0 w:1)
fn force_note_new_head() -> Weight {
(22_422_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: ParasShared CurrentSessionIndex (r:1 w:0)
// Storage: Paras ActionsQueue (r:1 w:1)
fn force_queue_action() -> Weight {
(20_518_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
}
1 change: 1 addition & 0 deletions runtime/parachains/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl crate::shared::Config for Test {}
impl crate::paras::Config for Test {
type Origin = Origin;
type Event = Event;
type WeightInfo = crate::paras::weights::WeightInfo<Test>;
}

impl crate::dmp::Config for Test {}
Expand Down
25 changes: 20 additions & 5 deletions runtime/parachains/src/paras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ use serde::{Deserialize, Serialize};

pub use crate::Origin as ParachainOrigin;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;

pub use pallet::*;

// the two key times necessary to track for every code replacement.
Expand Down Expand Up @@ -267,6 +271,14 @@ pub struct ParaGenesisArgs {
pub parachain: bool,
}

pub trait WeightInfo {
fn force_set_current_code(c: u32) -> Weight;
fn force_set_current_head() -> Weight;
fn force_schedule_code_upgrade(c: u32) -> Weight;
fn force_note_new_head() -> Weight;
fn force_queue_action() -> Weight;
}

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand All @@ -283,6 +295,9 @@ pub mod pallet {
+ Into<result::Result<Origin, <Self as Config>::Origin>>;

type Event: From<Event> + IsType<<Self as frame_system::Config>::Event>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}

#[pallet::event]
Expand Down Expand Up @@ -490,7 +505,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Set the storage for the parachain validation code immediately.
#[pallet::weight(0)]
#[pallet::weight(T::WeightInfo::force_set_current_code(new_code.0.len() as u32))]
pub fn force_set_current_code(
origin: OriginFor<T>,
para: ParaId,
Expand All @@ -509,7 +524,7 @@ pub mod pallet {
}

/// Set the storage for the current parachain head data immediately.
#[pallet::weight(0)]
#[pallet::weight(T::WeightInfo::force_set_current_head())]
pub fn force_set_current_head(
origin: OriginFor<T>,
para: ParaId,
Expand All @@ -522,7 +537,7 @@ pub mod pallet {
}

/// Schedule an upgrade as if it was scheduled in the given relay parent block.
#[pallet::weight(0)]
#[pallet::weight(T::WeightInfo::force_schedule_code_upgrade(new_code.0.len() as u32))]
pub fn force_schedule_code_upgrade(
origin: OriginFor<T>,
para: ParaId,
Expand All @@ -537,7 +552,7 @@ pub mod pallet {
}

/// Note a new block head for para within the context of the current block.
#[pallet::weight(0)]
#[pallet::weight(T::WeightInfo::force_note_new_head())]
pub fn force_note_new_head(
origin: OriginFor<T>,
para: ParaId,
Expand All @@ -553,7 +568,7 @@ pub mod pallet {
/// Put a parachain directly into the next session's action queue.
/// We can't queue it any sooner than this without going into the
/// initializer...
#[pallet::weight(0)]
#[pallet::weight(T::WeightInfo::force_queue_action())]
pub fn force_queue_action(origin: OriginFor<T>, para: ParaId) -> DispatchResult {
ensure_root(origin)?;
let next_session = shared::Pallet::<T>::session_index().saturating_add(One::one());
Expand Down
82 changes: 82 additions & 0 deletions runtime/parachains/src/paras/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use super::*;
use crate::configuration::HostConfiguration;
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};
use frame_system::RawOrigin;
use primitives::v1::{HeadData, Id as ParaId, ValidationCode};
use sp_runtime::traits::{One, Saturating};

fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::Event = generic_event.into();
// compare to the last event record
let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}

benchmarks! {
force_set_current_code {
let c in 1024 .. 2048;
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
let new_code = ValidationCode(vec![0; c as usize]);
let para_id = ParaId::from(c as u32);
}: _(RawOrigin::Root, para_id, new_code)
verify {
assert_last_event::<T>(Event::CurrentCodeUpdated(para_id).into());
}
force_set_current_head {
let new_head = HeadData(vec![0]);
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
let para_id = ParaId::from(1000);
}: _(RawOrigin::Root, para_id, new_head)
verify {
assert_last_event::<T>(Event::CurrentHeadUpdated(para_id).into());
}
force_schedule_code_upgrade {
let c in 1024 .. 2048;
let new_code = ValidationCode(vec![0; c as usize]);
let para_id = ParaId::from(c as u32);
let block = T::BlockNumber::from(c);
}: _(RawOrigin::Root, para_id, new_code, block)
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
verify {
assert_last_event::<T>(Event::CodeUpgradeScheduled(para_id).into());
}
force_note_new_head {
let para_id = ParaId::from(1000);
let new_head = HeadData(vec![0]);
// schedule an expired code upgrade for this para_id so that force_note_new_head would use
// the worst possible code path
let expired = frame_system::Pallet::<T>::block_number().saturating_sub(One::one());
let config = HostConfiguration::<T::BlockNumber>::default();
Pallet::<T>::schedule_code_upgrade(para_id, ValidationCode(vec![0]), expired, &config);
}: _(RawOrigin::Root, para_id, new_head)
verify {
assert_last_event::<T>(Event::NewHeadNoted(para_id).into());
}
force_queue_action {
let para_id = ParaId::from(1000);
}: _(RawOrigin::Root, para_id)
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
verify {
let next_session = crate::shared::Pallet::<T>::session_index().saturating_add(One::one());
assert_last_event::<T>(Event::ActionQueued(para_id, next_session).into());
}
}

impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext(Default::default()),
crate::mock::Test
);
86 changes: 86 additions & 0 deletions runtime/parachains/src/paras/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

//! Autogenerated weights for `runtime_parachains::paras`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2021-09-18, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("westend-dev"), DB CACHE: 128

// Executed Command:
// ./target/release/polkadot
// benchmark
// --chain
// westend-dev
// --execution=wasm
// --wasm-execution=compiled
// --pallet
// runtime_parachains::paras
// --steps
// 50
// --repeat
// 20
// --raw
// --extrinsic
// *
// --output
// runtime/parachains/src/paras/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;

/// Weight functions for `runtime_parachains::paras`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> super::WeightInfo for WeightInfo<T> {
// Storage: Paras CurrentCodeHash (r:1 w:1)
// Storage: Paras CodeByHashRefs (r:1 w:1)
// Storage: Paras PastCodeMeta (r:1 w:1)
// Storage: Paras PastCodePruning (r:1 w:1)
// Storage: Paras PastCodeHash (r:0 w:1)
// Storage: Paras CodeByHash (r:0 w:1)
fn force_set_current_code(c: u32, ) -> Weight {
(44_794_000 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(c as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(6 as Weight))
}
// Storage: Paras Heads (r:0 w:1)
fn force_set_current_head() -> Weight {
(18_999_000 as Weight)
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Configuration ActiveConfig (r:1 w:0)
// Storage: Paras FutureCodeUpgrades (r:1 w:1)
// Storage: Paras UpcomingUpgrades (r:1 w:1)
// Storage: Paras UpgradeCooldowns (r:1 w:1)
// Storage: System Digest (r:1 w:1)
// Storage: Paras CodeByHashRefs (r:1 w:1)
// Storage: Paras CodeByHash (r:0 w:1)
// Storage: Paras FutureCodeHash (r:0 w:1)
// Storage: Paras UpgradeRestrictionSignal (r:0 w:1)
fn force_schedule_code_upgrade(c: u32, ) -> Weight {
(50_520_000 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(c as Weight))
.saturating_add(T::DbWeight::get().reads(6 as Weight))
.saturating_add(T::DbWeight::get().writes(8 as Weight))
}
// Storage: Paras FutureCodeUpgrades (r:1 w:0)
// Storage: Paras Heads (r:0 w:1)
fn force_note_new_head() -> Weight {
(26_383_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: ParasShared CurrentSessionIndex (r:1 w:0)
// Storage: Paras ActionsQueue (r:1 w:1)
fn force_queue_action() -> Weight {
(24_305_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
}
Loading