Move WeightCounter to sp-weights (#12603)

* Move WeightCounter to sp_weights

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Rename to WeightMeter and test

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix pallet-scheduler for new usage

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update primitives/weights/src/weight_meter.rs

Co-authored-by: David <dvdplm@gmail.com>

* More tests for can_accrue

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Clippy

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Remove defensive_accrue and fixup consumed_ratio

I dont think there is a good use-case for defensive_accrue
without saturation. Only in tests maybe, will remove for now
until we have a use-case.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Test

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: David <dvdplm@gmail.com>
Co-authored-by: Gavin Wood <gavin@parity.io>
This commit is contained in:
Oliver Tale-Yazdi
2022-11-11 12:26:47 +01:00
committed by GitHub
parent e04b0c4929
commit bd2166de79
4 changed files with 195 additions and 39 deletions
+8 -12
View File
@@ -122,17 +122,13 @@ fn make_origin<T: Config>(signed: bool) -> <T as Config>::PalletsOrigin {
}
}
fn dummy_counter() -> WeightCounter {
WeightCounter { used: Weight::zero(), limit: Weight::MAX }
}
benchmarks! {
// `service_agendas` when no work is done.
service_agendas_base {
let now = T::BlockNumber::from(BLOCK_NUMBER);
IncompleteSince::<T>::put(now - One::one());
}: {
Scheduler::<T>::service_agendas(&mut dummy_counter(), now, 0);
Scheduler::<T>::service_agendas(&mut WeightMeter::max_limit(), now, 0);
} verify {
assert_eq!(IncompleteSince::<T>::get(), Some(now - One::one()));
}
@@ -144,7 +140,7 @@ benchmarks! {
fill_schedule::<T>(now, s)?;
let mut executed = 0;
}: {
Scheduler::<T>::service_agenda(&mut dummy_counter(), &mut executed, now, now, 0);
Scheduler::<T>::service_agenda(&mut WeightMeter::max_limit(), &mut executed, now, now, 0);
} verify {
assert_eq!(executed, 0);
}
@@ -155,7 +151,7 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(false, false, false, None, 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::zero() };
let mut counter = WeightMeter::from_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
@@ -169,7 +165,7 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(false, false, false, Some(s), 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::zero() };
let mut counter = WeightMeter::from_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
@@ -181,7 +177,7 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(false, true, false, None, 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::zero() };
let mut counter = WeightMeter::from_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
@@ -193,7 +189,7 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(true, false, false, None, 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::zero() };
let mut counter = WeightMeter::from_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
@@ -201,7 +197,7 @@ benchmarks! {
// `execute_dispatch` when the origin is `Signed`, not counting the dispatable's weight.
execute_dispatch_signed {
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::MAX };
let mut counter = WeightMeter::max_limit();
let origin = make_origin::<T>(true);
let call = T::Preimages::realize(&make_call::<T>(None)).unwrap().0;
}: {
@@ -212,7 +208,7 @@ benchmarks! {
// `execute_dispatch` when the origin is not `Signed`, not counting the dispatable's weight.
execute_dispatch_unsigned {
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::MAX };
let mut counter = WeightMeter::max_limit();
let origin = make_origin::<T>(false);
let call = T::Preimages::realize(&make_call::<T>(None)).unwrap().0;
}: {