Skip to content
Snippets Groups Projects
Unverified Commit 0adcd132 authored by bgallois's avatar bgallois
Browse files

add benchmarks for runtime

parent 1d5cd029
No related branches found
No related tags found
1 merge request!187Distance pallet benchmark
......@@ -5775,6 +5775,7 @@ dependencies = [
"frame-system",
"pallet-authority-members",
"pallet-authorship",
"pallet-balances",
"pallet-identity",
"pallet-membership",
"pallet-session",
......@@ -5783,6 +5784,8 @@ dependencies = [
"sp-core",
"sp-distance",
"sp-inherents",
"sp-io",
"sp-keystore",
"sp-runtime",
"sp-std 5.0.0",
]
......
......@@ -11,7 +11,9 @@ version = '1.0.0'
[features]
default = ['std']
runtime-benchmarks = ['frame-benchmarking/runtime-benchmarks']
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks",
"pallet-balances",
]
std = [
'codec/std',
'frame-support/std',
......@@ -39,11 +41,7 @@ scale-info = { version = "2.1.1", default-features = false, features = [
"derive",
] }
[dependencies.frame-benchmarking]
default-features = false
git = 'https://github.com/duniter/substrate'
optional = true
branch = 'duniter-substrate-v0.9.42'
pallet-balances = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', optional = true, default-features = false }
[dependencies.codec]
default-features = false
......@@ -51,6 +49,12 @@ features = ['derive']
package = 'parity-scale-codec'
version = '3.1.5'
[dependencies.frame-benchmarking]
default-features = false
git = 'https://github.com/duniter/substrate'
optional = true
branch = 'duniter-substrate-v0.9.42'
[dependencies.frame-support]
default-features = false
git = 'https://github.com/duniter/substrate'
......@@ -95,3 +99,8 @@ branch = 'duniter-substrate-v0.9.42'
[package.metadata.docs.rs]
targets = ['x86_64-unknown-linux-gnu']
[dev-dependencies]
pallet-balances = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' }
sp-io = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' }
sp-keystore = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' }
// Copyright 2021-2023 Axiom-Team
//
// This file is part of Duniter-v2S.
//
// Duniter-v2S is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// Duniter-v2S 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::{benchmarks, vec};
use frame_support::traits::{Currency, OnFinalize};
use frame_system::RawOrigin;
use pallet_balances::Pallet as Balances;
use sp_runtime::traits::{Bounded, One};
use sp_runtime::Perbill;
use crate::Pallet;
fn populate_pool<T: Config>(i: u32) -> Result<(), &'static str> {
EvaluationPool0::<T>::mutate(|current_pool| -> Result<(), &'static str> {
for j in 0..i {
current_pool
.evaluations
.try_push((j.into(), median::MedianAcc::new()))
.map_err(|_| Error::<T>::QueueFull)?;
}
Ok(())
})
}
benchmarks! {
where_clause {
where
T: pallet_balances::Config, T::Balance: From<u64>,
T::BlockNumber: From<u32>,
}
request_distance_evaluation {
let idty = T::IdtyIndex::one();
let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key;
let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into();
let _ = <Balances<T> as Currency<_>>::make_free_balance_be(&caller, T::Balance::max_value());
}: _<T::RuntimeOrigin>(caller_origin.clone())
verify {
assert!(IdentityDistanceStatus::<T>::get(&idty) == Some((caller, DistanceStatus::Pending)), "Request not added");
}
update_evaluation {
let idty = T::IdtyIndex::one();
let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key;
let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into();
let i in 1 .. MAX_EVALUATIONS_PER_SESSION => populate_pool::<T>(i)?;
}: _<T::RuntimeOrigin>(RawOrigin::None.into(), ComputationResult{distances: vec![Perbill::one(); i as usize]})
force_update_evaluation {
let idty = T::IdtyIndex::one();
let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key;
let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into();
let i in 1 .. MAX_EVALUATIONS_PER_SESSION => populate_pool::<T>(i)?;
}: _<T::RuntimeOrigin>(RawOrigin::Root.into(), caller, ComputationResult{distances: vec![Perbill::one(); i as usize]})
force_set_distance_status {
let idty = T::IdtyIndex::one();
let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key;
let status = Some((caller.clone(), DistanceStatus::Valid));
}: _<T::RuntimeOrigin>(RawOrigin::Root.into(), idty, status)
verify {
assert!(IdentityDistanceStatus::<T>::get(&idty) == Some((caller, DistanceStatus::Valid)), "Status not set");
}
on_finalize {
DidUpdate::<T>::set(true);
}: { Pallet::<T>::on_finalize(Default::default()); }
verify {
assert!(!DidUpdate::<T>::get());
}
}
......@@ -247,8 +247,11 @@ macro_rules! pallets_config {
type WeightInfo = common_runtime::weights::pallet_authority_members::WeightInfo<Runtime>;
}
impl pallet_authorship::Config for Runtime {
type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Babe>;
type EventHandler = ImOnline;
#[cfg(feature = "runtime-benchmarks")]
type FindAuthor = common_runtime::providers::ConstantAuthor<Self::AccountId>;
#[cfg(not(feature = "runtime-benchmarks"))]
type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Babe>;
}
impl pallet_im_online::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
......
......@@ -155,3 +155,16 @@ macro_rules! impl_benchmark_setup_handler {
impl_benchmark_setup_handler!(pallet_membership::SetupBenchmark<<T as pallet_identity::Config>::IdtyIndex, T::AccountId>);
#[cfg(feature = "runtime-benchmarks")]
impl_benchmark_setup_handler!(pallet_identity::traits::SetupBenchmark<<T as pallet_identity::Config>::IdtyIndex, T::AccountId>);
#[cfg(feature = "runtime-benchmarks")]
pub struct ConstantAuthor<T>(PhantomData<T>);
#[cfg(feature = "runtime-benchmarks")]
impl<T: From<[u8; 32]>> frame_support::traits::FindAuthor<T> for ConstantAuthor<T> {
fn find_author<'a, I>(_: I) -> Option<T>
where
I: 'a + IntoIterator<Item = (sp_runtime::ConsensusEngineId, &'a [u8])>,
{
Some([0; 32].into())
}
}
......@@ -27,6 +27,7 @@ runtime-benchmarks = [
'pallet-balances/runtime-benchmarks',
'pallet-certification/runtime-benchmarks',
'pallet-collective/runtime-benchmarks',
'pallet-distance/runtime-benchmarks',
'pallet-duniter-test-parameters/runtime-benchmarks',
'pallet-duniter-account/runtime-benchmarks',
'pallet-quota/runtime-benchmarks',
......
......@@ -145,6 +145,7 @@ mod benches {
define_benchmarks!(
[pallet_certification, Cert]
[pallet_certification, SmithCert]
[pallet_distance, Distance]
[pallet_oneshot_account, OneshotAccount]
[pallet_universal_dividend, UniversalDividend]
[pallet_provide_randomness, ProvideRandomness]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment