Skip to content

Commit

Permalink
fix benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Jul 1, 2024
1 parent 0c4e651 commit ef9c4fb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
62 changes: 45 additions & 17 deletions substrate/frame/broker/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,22 +997,43 @@ mod benches {

advance_to::<T>(2);

// We assume max auto renewals for worst case.
(0..T::MaxAutoRenewals::get()-1).try_for_each(|indx| -> Result<(), BenchmarkError> {
let task = 1000 + indx;
let caller: T::AccountId = T::SovereignAccountOf::maybe_convert(task)
.expect("Failed to get sovereign account");
T::Currency::set_balance(
&caller.clone(),
T::Currency::minimum_balance().saturating_add(100u32.into()),
);

let region = Broker::<T>::do_purchase(caller.clone(), 10u32.into())
.map_err(|_| BenchmarkError::Weightless)?;

Broker::<T>::do_assign(region, None, task, Final)
.map_err(|_| BenchmarkError::Weightless)?;

Broker::<T>::do_enable_auto_renew(caller, region.core, task, Some(7))?;

Ok(())
})?;

let caller: T::AccountId =
T::SovereignAccountOf::maybe_convert(2001).expect("Failed to get sovereign account");
T::Currency::set_balance(
&caller.clone(),
T::Currency::minimum_balance().saturating_add(100u32.into()),
);

// The region for which we benchmark enable auto renew.
let region = Broker::<T>::do_purchase(caller.clone(), 10u32.into())
.map_err(|_| BenchmarkError::Weightless)?;

Broker::<T>::do_assign(region, None, 2001, Final)
.map_err(|_| BenchmarkError::Weightless)?;
// advance to next bulk sale:
advance_to::<T>(6);

// The most 'intensive' path is when we renew the core upon enabling auto-renewal.
// Therefore, we advance to next bulk sale:
advance_to::<T>(6);

#[extrinsic_call]
_(RawOrigin::Signed(caller), region.core, 2001, None);
Expand All @@ -1034,26 +1055,33 @@ mod benches {

advance_to::<T>(2);

let caller: T::AccountId =
T::SovereignAccountOf::maybe_convert(2001).expect("Failed to get sovereign account");
T::Currency::set_balance(
&caller.clone(),
T::Currency::minimum_balance().saturating_add(100u32.into()),
);
// We assume max auto renewals for worst case.
(0..T::MaxAutoRenewals::get()-1).try_for_each(|indx| -> Result<(), BenchmarkError> {
let task = 1000 + indx;
let caller: T::AccountId = T::SovereignAccountOf::maybe_convert(task)
.expect("Failed to get sovereign account");
T::Currency::set_balance(
&caller.clone(),
T::Currency::minimum_balance().saturating_add(100u32.into()),
);

let region = Broker::<T>::do_purchase(caller.clone(), 10u32.into())
.map_err(|_| BenchmarkError::Weightless)?;
let region = Broker::<T>::do_purchase(caller.clone(), 10u32.into())
.map_err(|_| BenchmarkError::Weightless)?;

Broker::<T>::do_assign(region, None, 2001, Final)
.map_err(|_| BenchmarkError::Weightless)?;
Broker::<T>::do_assign(region, None, task, Final)
.map_err(|_| BenchmarkError::Weightless)?;

advance_to::<T>(6);
Broker::<T>::do_enable_auto_renew(caller.clone(), region.core, 2001, None)?;
Broker::<T>::do_enable_auto_renew(caller, region.core, task, Some(7))?;

Ok(())
})?;

let caller: T::AccountId = T::SovereignAccountOf::maybe_convert(1000)
.expect("Failed to get sovereign account");
#[extrinsic_call]
_(RawOrigin::Signed(caller), region.core, 2001);
_(RawOrigin::Signed(caller), _core, 1000);

assert_last_event::<T>(Event::AutoRenewalDisabled { core: region.core, task: 2001 }.into());
assert_last_event::<T>(Event::AutoRenewalDisabled { core: _core, task: 1000 }.into());

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/broker/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl crate::Config for Test {
type PalletId = TestBrokerId;
type AdminOrigin = EnsureOneOrRoot;
type SovereignAccountOf = SovereignAccountOf;
type MaxAutoRenewals = ConstU32<5>;
type MaxAutoRenewals = ConstU32<3>;
type PriceAdapter = CenterTargetPrice<BalanceOf<Self>>;
}

Expand Down
12 changes: 11 additions & 1 deletion substrate/frame/broker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ fn renewal_works_leases_ended_before_start_sales() {
#[test]
fn enable_auto_renew_works() {
TestExt::new().endow(1, 1000).limit_cores_offered(Some(10)).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 3));
assert_ok!(Broker::do_start_sales(100, 5));
advance_to(2);
let region_id = Broker::do_purchase(1, u64::max_value()).unwrap();
let record = Regions::<Test>::get(region_id).unwrap();
Expand Down Expand Up @@ -1534,6 +1534,16 @@ fn enable_auto_renew_works() {
AutoRenewalRecord { core: 2, task: 1003, next_renewal: 7 },
]
);

// Ensure that we cannot enable more auto renewals than `MaxAutoRenewals`.
// We already enabled it for three cores, and the limit is set to 3.
let region_4 = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region_4, Some(1), 1004, Final));

assert_noop!(
Broker::do_enable_auto_renew(1004, region_4.core, 1004, Some(7)),
Error::<Test>::TooManyAutoRenewals
);
});
}

Expand Down

0 comments on commit ef9c4fb

Please sign in to comment.