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

add weight accounting

parent 943f7f26
No related branches found
No related tags found
1 merge request!252Change distance evaluation period from Sessions to Blocks
......@@ -20,6 +20,8 @@ use super::*;
use codec::Encode;
use frame_benchmarking::v2::*;
use frame_support::traits::Get;
use frame_support::traits::OnInitialize;
use frame_support::traits::{Currency, OnFinalize};
use frame_system::pallet_prelude::BlockNumberFor;
use frame_system::RawOrigin;
......@@ -164,6 +166,103 @@ mod benchmarks {
assert_has_event::<T>(Event::<T>::EvaluatedValid { idty_index: idty }.into());
}
#[benchmark]
fn on_initialize_overhead() {
// Benchmark on_initialize with no on_finalize and no do_evaluation.
let block_number: BlockNumberFor<T> = (T::EvaluationPeriod::get() + 1).into();
#[block]
{
Pallet::<T>::on_initialize(block_number);
}
}
#[benchmark]
fn do_evaluation_success() -> Result<(), BenchmarkError> {
// Benchmark do_evaluation in case of one success.
CurrentPoolIndex::<T>::put(0);
frame_system::pallet::Pallet::<T>::set_block_number(1u32.into());
let idty = T::IdtyIndex::one();
let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty)
.unwrap()
.owner_key;
let _ =
<Balances<T> as Currency<_>>::make_free_balance_be(&caller, T::Balance::max_value());
Pallet::<T>::request_distance_evaluation(RawOrigin::Signed(caller.clone()).into())?;
assert_has_event::<T>(
Event::<T>::EvaluationRequested {
idty_index: idty,
who: caller.clone(),
}
.into(),
);
CurrentPoolIndex::<T>::put(2);
Pallet::<T>::force_update_evaluation(
RawOrigin::Root.into(),
caller,
ComputationResult {
distances: vec![Perbill::one()],
},
)?;
#[block]
{
Pallet::<T>::do_evaluation(0);
}
assert_has_event::<T>(Event::<T>::EvaluatedValid { idty_index: idty }.into());
Ok(())
}
#[benchmark]
fn do_evaluation_echec() -> Result<(), BenchmarkError> {
// Benchmark do_evaluation in case of one echec.
CurrentPoolIndex::<T>::put(0);
frame_system::pallet::Pallet::<T>::set_block_number(1u32.into());
let idty = T::IdtyIndex::one();
let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty)
.unwrap()
.owner_key;
let _ =
<Balances<T> as Currency<_>>::make_free_balance_be(&caller, T::Balance::max_value());
Pallet::<T>::request_distance_evaluation(RawOrigin::Signed(caller.clone()).into())?;
assert_has_event::<T>(
Event::<T>::EvaluationRequested {
idty_index: idty,
who: caller.clone(),
}
.into(),
);
CurrentPoolIndex::<T>::put(2);
Pallet::<T>::force_update_evaluation(
RawOrigin::Root.into(),
caller,
ComputationResult {
distances: vec![Perbill::zero()],
},
)?;
#[block]
{
Pallet::<T>::do_evaluation(0);
}
assert_has_event::<T>(Event::<T>::EvaluatedInvalid { idty_index: idty }.into());
Ok(())
}
#[benchmark]
fn do_evaluation_overhead() -> Result<(), BenchmarkError> {
#[block]
{
Pallet::<T>::do_evaluation(0);
}
Ok(())
}
#[benchmark]
fn on_finalize() {
DidUpdate::<T>::set(true);
......
......@@ -221,14 +221,17 @@ pub mod pallet {
where
BlockNumberFor<T>: From<u32>,
{
let mut weight = <T as pallet::Config>::WeightInfo::on_initialize_overhead();
if block % BlockNumberFor::<T>::one().saturating_mul(T::EvaluationPeriod::get().into())
== BlockNumberFor::<T>::zero()
{
let index = (CurrentPoolIndex::<T>::get() + 1) % 3;
CurrentPoolIndex::<T>::put(index);
Self::do_evaluation(index);
weight = weight
.saturating_add(Self::do_evaluation(index))
.saturating_add(T::DbWeight::get().reads_writes(1, 1));
}
<T as pallet::Config>::WeightInfo::on_finalize()
weight.saturating_add(<T as pallet::Config>::WeightInfo::on_finalize())
}
fn on_finalize(_n: BlockNumberFor<T>) {
......@@ -519,7 +522,8 @@ pub mod pallet {
Self::deposit_event(Event::EvaluatedValid { idty_index: idty });
}
fn do_evaluation(index: u32) {
pub fn do_evaluation(index: u32) -> Weight {
let mut weight = <T as pallet::Config>::WeightInfo::do_evaluation_overhead();
// set evaluation block
EvaluationBlock::<T>::set(frame_system::Pallet::<T>::parent_hash());
......@@ -530,6 +534,7 @@ pub mod pallet {
<T as frame_system::Config>::AccountId,
<T as pallet_identity::Config>::IdtyIndex,
> = Pallet::<T>::take_current_pool(index);
for (idty, median_acc) in current_pool.evaluations.into_iter() {
// distance result
let mut distance_result: Option<bool> = None;
......@@ -545,6 +550,7 @@ pub mod pallet {
}
// take requester and perform unreserve or slash
if let Some(requester) = PendingEvaluationRequest::<T>::take(idty) {
match distance_result {
None => {
......@@ -553,6 +559,12 @@ pub mod pallet {
&requester,
<T as Config>::EvaluationPrice::get(),
);
weight = weight.saturating_add(
<T as pallet::Config>::WeightInfo::do_evaluation_echec()
.saturating_sub(
<T as pallet::Config>::WeightInfo::do_evaluation_overhead(),
),
);
}
Some(true) => {
// positive result, unreserve and apply
......@@ -561,6 +573,12 @@ pub mod pallet {
<T as Config>::EvaluationPrice::get(),
);
Self::do_valid_distance_status(idty);
weight = weight.saturating_add(
<T as pallet::Config>::WeightInfo::do_evaluation_success()
.saturating_sub(
<T as pallet::Config>::WeightInfo::do_evaluation_overhead(),
),
);
}
Some(false) => {
// negative result, slash and deposit event
......@@ -569,11 +587,18 @@ pub mod pallet {
<T as Config>::EvaluationPrice::get(),
);
Self::deposit_event(Event::EvaluatedInvalid { idty_index: idty });
weight = weight.saturating_add(
<T as pallet::Config>::WeightInfo::do_evaluation_echec()
.saturating_sub(
<T as pallet::Config>::WeightInfo::do_evaluation_overhead(),
),
);
}
}
}
// if evaluation happened without request, it's ok to do nothing
}
weight
}
}
......
......@@ -24,6 +24,10 @@ pub trait WeightInfo {
fn update_evaluation(i: u32) -> Weight;
fn force_update_evaluation(i: u32) -> Weight;
fn force_valid_distance_status() -> Weight;
fn on_initialize_overhead() -> Weight;
fn do_evaluation_overhead() -> Weight;
fn do_evaluation_success() -> Weight;
fn do_evaluation_echec() -> Weight;
fn on_finalize() -> Weight;
}
......@@ -90,6 +94,53 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().writes(5))
}
fn do_evaluation_success() -> Weight {
// Proof Size summary in bytes:
// Measured: `612 + i * (10 ±0)`
// Estimated: `2095 + i * (10 ±0)`
// Minimum execution time: 208_812_000 picoseconds.
Weight::from_parts(257_150_521, 0)
.saturating_add(Weight::from_parts(0, 2095))
// Standard Error: 53_366
.saturating_add(RocksDbWeight::get().reads(2))
.saturating_add(RocksDbWeight::get().writes(1))
}
fn do_evaluation_echec() -> Weight {
// Proof Size summary in bytes:
// Measured: `612 + i * (10 ±0)`
// Estimated: `2095 + i * (10 ±0)`
// Minimum execution time: 208_812_000 picoseconds.
Weight::from_parts(257_150_521, 0)
.saturating_add(Weight::from_parts(0, 2095))
// Standard Error: 53_366
.saturating_add(RocksDbWeight::get().reads(2))
.saturating_add(RocksDbWeight::get().writes(1))
}
fn do_evaluation_overhead() -> Weight {
// Proof Size summary in bytes:
// Measured: `612 + i * (10 ±0)`
// Estimated: `2095 + i * (10 ±0)`
// Minimum execution time: 208_812_000 picoseconds.
Weight::from_parts(257_150_521, 0)
.saturating_add(Weight::from_parts(0, 2095))
// Standard Error: 53_366
.saturating_add(RocksDbWeight::get().reads(2))
.saturating_add(RocksDbWeight::get().writes(1))
}
fn on_initialize_overhead() -> Weight {
// Proof Size summary in bytes:
// Measured: `170`
// Estimated: `1655`
// Minimum execution time: 93_595_000 picoseconds.
Weight::from_parts(109_467_000, 0)
.saturating_add(Weight::from_parts(0, 1655))
.saturating_add(RocksDbWeight::get().reads(1))
.saturating_add(RocksDbWeight::get().writes(1))
}
fn on_finalize() -> Weight {
// Proof Size summary in bytes:
// Measured: `170`
......
......@@ -17,7 +17,7 @@
//! Autogenerated weights for `pallet_distance`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0
//! DATE: 2024-02-19, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2024-02-27, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `bgallois-ms7d43`, CPU: `12th Gen Intel(R) Core(TM) i3-12100F`
//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024
......@@ -29,7 +29,7 @@
// --chain=dev
// --steps=50
// --repeat=20
// --pallet=*
// --pallet=pallet-distance
// --extrinsic=*
// --wasm-execution=compiled
// --heap-pages=4096
......@@ -59,19 +59,19 @@ impl<T: frame_system::Config> pallet_distance::WeightInfo for WeightInfo<T> {
/// Proof: `Parameters::ParametersStorage` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Certification::StorageIdtyCertMeta` (r:1 w:0)
/// Proof: `Certification::StorageIdtyCertMeta` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Session::CurrentIndex` (r:1 w:0)
/// Proof: `Session::CurrentIndex` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::CurrentPoolIndex` (r:1 w:0)
/// Proof: `Distance::CurrentPoolIndex` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::EvaluationPool2` (r:1 w:1)
/// Proof: `Distance::EvaluationPool2` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `MaxEncodedLen`)
fn request_distance_evaluation() -> Weight {
// Proof Size summary in bytes:
// Measured: `1596`
// Estimated: `5061`
// Minimum execution time: 39_987_000 picoseconds.
Weight::from_parts(41_680_000, 0)
.saturating_add(Weight::from_parts(0, 5061))
// Measured: `1119`
// Estimated: `4584`
// Minimum execution time: 40_278_000 picoseconds.
Weight::from_parts(41_411_000, 0)
.saturating_add(Weight::from_parts(0, 4584))
.saturating_add(T::DbWeight::get().reads(9))
.saturating_add(T::DbWeight::get().writes(3))
}
......@@ -87,19 +87,19 @@ impl<T: frame_system::Config> pallet_distance::WeightInfo for WeightInfo<T> {
/// Proof: `Parameters::ParametersStorage` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Certification::StorageIdtyCertMeta` (r:1 w:0)
/// Proof: `Certification::StorageIdtyCertMeta` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Session::CurrentIndex` (r:1 w:0)
/// Proof: `Session::CurrentIndex` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::CurrentPoolIndex` (r:1 w:0)
/// Proof: `Distance::CurrentPoolIndex` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::EvaluationPool2` (r:1 w:1)
/// Proof: `Distance::EvaluationPool2` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `MaxEncodedLen`)
fn request_distance_evaluation_for() -> Weight {
// Proof Size summary in bytes:
// Measured: `1625`
// Estimated: `7565`
// Minimum execution time: 43_537_000 picoseconds.
Weight::from_parts(45_197_000, 0)
.saturating_add(Weight::from_parts(0, 7565))
// Measured: `1148`
// Estimated: `7088`
// Minimum execution time: 42_131_000 picoseconds.
Weight::from_parts(43_903_000, 0)
.saturating_add(Weight::from_parts(0, 7088))
.saturating_add(T::DbWeight::get().reads(10))
.saturating_add(T::DbWeight::get().writes(3))
}
......@@ -111,38 +111,38 @@ impl<T: frame_system::Config> pallet_distance::WeightInfo for WeightInfo<T> {
/// Proof: `System::Digest` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Session::Validators` (r:1 w:0)
/// Proof: `Session::Validators` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Session::CurrentIndex` (r:1 w:0)
/// Proof: `Session::CurrentIndex` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::CurrentPoolIndex` (r:1 w:0)
/// Proof: `Distance::CurrentPoolIndex` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::EvaluationPool0` (r:1 w:1)
/// Proof: `Distance::EvaluationPool0` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// The range of component `i` is `[1, 600]`.
fn update_evaluation(i: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `773 + i * (10 ±0)`
// Estimated: `2257 + i * (10 ±0)`
// Minimum execution time: 13_230_000 picoseconds.
Weight::from_parts(15_236_858, 0)
.saturating_add(Weight::from_parts(0, 2257))
// Standard Error: 498
.saturating_add(Weight::from_parts(102_078, 0).saturating_mul(i.into()))
// Measured: `402 + i * (10 ±0)`
// Estimated: `1886 + i * (10 ±0)`
// Minimum execution time: 12_079_000 picoseconds.
Weight::from_parts(14_113_350, 0)
.saturating_add(Weight::from_parts(0, 1886))
// Standard Error: 368
.saturating_add(Weight::from_parts(101_595, 0).saturating_mul(i.into()))
.saturating_add(T::DbWeight::get().reads(6))
.saturating_add(T::DbWeight::get().writes(3))
.saturating_add(Weight::from_parts(0, 10).saturating_mul(i.into()))
}
/// Storage: `Session::CurrentIndex` (r:1 w:0)
/// Proof: `Session::CurrentIndex` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::CurrentPoolIndex` (r:1 w:0)
/// Proof: `Distance::CurrentPoolIndex` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::EvaluationPool0` (r:1 w:1)
/// Proof: `Distance::EvaluationPool0` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// The range of component `i` is `[1, 600]`.
fn force_update_evaluation(i: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `612 + i * (10 ±0)`
// Estimated: `2096 + i * (10 ±0)`
// Minimum execution time: 6_222_000 picoseconds.
Weight::from_parts(8_798_404, 0)
.saturating_add(Weight::from_parts(0, 2096))
// Standard Error: 525
.saturating_add(Weight::from_parts(101_030, 0).saturating_mul(i.into()))
// Measured: `139 + i * (10 ±0)`
// Estimated: `1623 + i * (10 ±0)`
// Minimum execution time: 5_304_000 picoseconds.
Weight::from_parts(6_358_633, 0)
.saturating_add(Weight::from_parts(0, 1623))
// Standard Error: 1_291
.saturating_add(Weight::from_parts(109_183, 0).saturating_mul(i.into()))
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(1))
.saturating_add(Weight::from_parts(0, 10).saturating_mul(i.into()))
......@@ -159,21 +159,93 @@ impl<T: frame_system::Config> pallet_distance::WeightInfo for WeightInfo<T> {
// Proof Size summary in bytes:
// Measured: `609`
// Estimated: `6549`
// Minimum execution time: 24_153_000 picoseconds.
Weight::from_parts(24_962_000, 0)
// Minimum execution time: 24_868_000 picoseconds.
Weight::from_parts(26_575_000, 0)
.saturating_add(Weight::from_parts(0, 6549))
.saturating_add(T::DbWeight::get().reads(5))
.saturating_add(T::DbWeight::get().writes(3))
}
fn on_initialize_overhead() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 130_000 picoseconds.
Weight::from_parts(158_000, 0)
.saturating_add(Weight::from_parts(0, 0))
}
/// Storage: `System::ParentHash` (r:1 w:0)
/// Proof: `System::ParentHash` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `Distance::EvaluationPool2` (r:1 w:1)
/// Proof: `Distance::EvaluationPool2` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::PendingEvaluationRequest` (r:1 w:1)
/// Proof: `Distance::PendingEvaluationRequest` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `MaxEncodedLen`)
/// Storage: `Identity::Identities` (r:1 w:0)
/// Proof: `Identity::Identities` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Membership::Membership` (r:1 w:1)
/// Proof: `Membership::Membership` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Membership::MembershipsExpireOn` (r:2 w:2)
/// Proof: `Membership::MembershipsExpireOn` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Parameters::ParametersStorage` (r:1 w:0)
/// Proof: `Parameters::ParametersStorage` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::EvaluationBlock` (r:0 w:1)
/// Proof: `Distance::EvaluationBlock` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn do_evaluation_success() -> Weight {
// Proof Size summary in bytes:
// Measured: `973`
// Estimated: `6913`
// Minimum execution time: 42_195_000 picoseconds.
Weight::from_parts(43_925_000, 0)
.saturating_add(Weight::from_parts(0, 6913))
.saturating_add(T::DbWeight::get().reads(9))
.saturating_add(T::DbWeight::get().writes(7))
}
/// Storage: `System::ParentHash` (r:1 w:0)
/// Proof: `System::ParentHash` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `Distance::EvaluationPool2` (r:1 w:1)
/// Proof: `Distance::EvaluationPool2` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::PendingEvaluationRequest` (r:1 w:1)
/// Proof: `Distance::PendingEvaluationRequest` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `MaxEncodedLen`)
/// Storage: `Distance::EvaluationBlock` (r:0 w:1)
/// Proof: `Distance::EvaluationBlock` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn do_evaluation_echec() -> Weight {
// Proof Size summary in bytes:
// Measured: `364`
// Estimated: `3829`
// Minimum execution time: 21_868_000 picoseconds.
Weight::from_parts(22_858_000, 0)
.saturating_add(Weight::from_parts(0, 3829))
.saturating_add(T::DbWeight::get().reads(4))
.saturating_add(T::DbWeight::get().writes(4))
}
/// Storage: `System::ParentHash` (r:1 w:0)
/// Proof: `System::ParentHash` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `Distance::EvaluationPool2` (r:1 w:0)
/// Proof: `Distance::EvaluationPool2` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Distance::EvaluationBlock` (r:0 w:1)
/// Proof: `Distance::EvaluationBlock` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn do_evaluation_overhead() -> Weight {
// Proof Size summary in bytes:
// Measured: `109`
// Estimated: `1594`
// Minimum execution time: 2_484_000 picoseconds.
Weight::from_parts(2_740_000, 0)
.saturating_add(Weight::from_parts(0, 1594))
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: `Distance::DidUpdate` (r:1 w:1)
/// Proof: `Distance::DidUpdate` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn on_finalize() -> Weight {
// Proof Size summary in bytes:
// Measured: `170`
// Estimated: `1655`
// Minimum execution time: 2_716_000 picoseconds.
Weight::from_parts(2_884_000, 0)
.saturating_add(Weight::from_parts(0, 1655))
// Measured: `133`
// Estimated: `1618`
// Minimum execution time: 2_312_000 picoseconds.
Weight::from_parts(2_562_000, 0)
.saturating_add(Weight::from_parts(0, 1618))
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment