From 45a3650ede249439ba67c5d3c7d622468c327585 Mon Sep 17 00:00:00 2001 From: Krayt78 Date: Thu, 21 Nov 2024 23:32:42 +0100 Subject: [PATCH] Migrated benchmarking to V2 --- .../src/paras_inherent/benchmarking.rs | 166 ++++++++++-------- 1 file changed, 94 insertions(+), 72 deletions(-) diff --git a/polkadot/runtime/parachains/src/paras_inherent/benchmarking.rs b/polkadot/runtime/parachains/src/paras_inherent/benchmarking.rs index 485e7211c1d2..2c553714d9f2 100644 --- a/polkadot/runtime/parachains/src/paras_inherent/benchmarking.rs +++ b/polkadot/runtime/parachains/src/paras_inherent/benchmarking.rs @@ -13,42 +13,50 @@ // You should have received a copy of the GNU General Public License // along with Polkadot. If not, see . +#![cfg(feature = "runtime-benchmarks")] use super::*; use crate::{inclusion, ParaId}; use alloc::collections::btree_map::BTreeMap; use core::cmp::{max, min}; -use frame_benchmarking::{benchmarks, impl_benchmark_test_suite}; +use frame_benchmarking::v2::*; use frame_system::RawOrigin; use polkadot_primitives::v8::GroupIndex; use crate::builder::BenchBuilder; -benchmarks! { - enter_empty { - let scenario = BenchBuilder::::new() - .build(); +#[benchmarks] +mod benchmarks { + use super::*; + + #[benchmark] + fn enter_empty() -> Result<(), BenchmarkError> { + let scenario = BenchBuilder::::new().build(); let mut benchmark = scenario.data.clone(); benchmark.bitfields.clear(); benchmark.backed_candidates.clear(); benchmark.disputes.clear(); - }: enter(RawOrigin::None, benchmark) - verify { + + #[extrinsic_call] + enter(RawOrigin::None, benchmark); + // Assert that the block was not discarded assert!(Included::::get().is_some()); + + Ok(()) } + // Variant over `v`, the number of dispute statements in a dispute statement set. This gives the // weight of a single dispute statement set. - enter_variable_disputes { - // The number of statements needs to be at least a third of the validator set size. - let v in 400..BenchBuilder::::fallback_max_validators(); - - let scenario = BenchBuilder::::new() - .set_dispute_sessions(&[2]) - .build(); + // The number of statements needs to be at least a third of the validator set size. + #[benchmark] + fn enter_variable_disputes( + v: Linear<400, { BenchBuilder::::fallback_max_validators() }>, + ) -> Result<(), BenchmarkError> { + let scenario = BenchBuilder::::new().set_dispute_sessions(&[2]).build(); let mut benchmark = scenario.data.clone(); let dispute = benchmark.disputes.pop().unwrap(); @@ -59,8 +67,10 @@ benchmarks! { benchmark.disputes.push(dispute); benchmark.disputes.get_mut(0).unwrap().statements.drain(v as usize..); - }: enter(RawOrigin::None, benchmark) - verify { + + #[extrinsic_call] + enter(RawOrigin::None, benchmark); + // Assert that the block was not discarded assert!(Included::::get().is_some()); @@ -71,14 +81,15 @@ benchmarks! { // Ensure that the votes are for the correct session assert_eq!(vote.session, scenario._session); + + Ok(()) } // The weight of one bitfield. - enter_bitfields { - let cores_with_backed: BTreeMap<_, _> - = vec![(0, BenchBuilder::::fallback_max_validators())] - .into_iter() - .collect(); + #[benchmark] + fn enter_bitfields() -> Result<(), BenchmarkError> { + let cores_with_backed: BTreeMap<_, _> = + vec![(0, BenchBuilder::::fallback_max_validators())].into_iter().collect(); let scenario = BenchBuilder::::new() .set_backed_and_concluding_paras(cores_with_backed) @@ -92,8 +103,10 @@ benchmarks! { benchmark.disputes.clear(); benchmark.bitfields.push(bitfield); - }: enter(RawOrigin::None, benchmark) - verify { + + #[extrinsic_call] + enter(RawOrigin::None, benchmark); + // Assert that the block was not discarded assert!(Included::::get().is_some()); // Assert that there are on-chain votes that got scraped @@ -102,18 +115,27 @@ benchmarks! { let vote = onchain_votes.unwrap(); // Ensure that the votes are for the correct session assert_eq!(vote.session, scenario._session); + + Ok(()) } // Variant over `v`, the amount of validity votes for a backed candidate. This gives the weight // of a single backed candidate. - enter_backed_candidates_variable { - let v in (BenchBuilder::::fallback_min_backing_votes()) - .. max(BenchBuilder::::fallback_min_backing_votes() + 1, BenchBuilder::::fallback_max_validators_per_core()); - - let cores_with_backed: BTreeMap<_, _> - = vec![(0, v)] // The backed candidate will have `v` validity votes. - .into_iter() - .collect(); + #[benchmark] + fn enter_backed_candidates_variable( + v: Linear< + { BenchBuilder::::fallback_min_backing_votes() }, + { + max( + BenchBuilder::::fallback_min_backing_votes() + 1, + BenchBuilder::::fallback_max_validators_per_core(), + ) + }, + >, + ) -> Result<(), BenchmarkError> { + let cores_with_backed: BTreeMap<_, _> = vec![(0, v)] // The backed candidate will have `v` validity votes. + .into_iter() + .collect(); let scenario = BenchBuilder::::new() .set_backed_in_inherent_paras(cores_with_backed.clone()) @@ -124,13 +146,18 @@ benchmarks! { // There is 1 backed, assert_eq!(benchmark.backed_candidates.len(), 1); // with `v` validity votes. - let votes = min(scheduler::Pallet::::group_validators(GroupIndex::from(0)).unwrap().len(), v as usize); + let votes = min( + scheduler::Pallet::::group_validators(GroupIndex::from(0)).unwrap().len(), + v as usize, + ); assert_eq!(benchmark.backed_candidates.get(0).unwrap().validity_votes().len(), votes); benchmark.bitfields.clear(); benchmark.disputes.clear(); - }: enter(RawOrigin::None, benchmark) - verify { + + #[extrinsic_call] + enter(RawOrigin::None, benchmark); + let max_validators_per_core = BenchBuilder::::fallback_max_validators_per_core(); // Assert that the block was not discarded assert!(Included::::get().is_some()); @@ -143,27 +170,27 @@ benchmarks! { // Ensure that there are an expected number of candidates let header = BenchBuilder::::header(scenario._block_number); // Traverse candidates and assert descriptors are as expected - for (para_id, backing_validators) in vote.backing_validators_per_candidate.iter().enumerate() { + for (para_id, backing_validators) in + vote.backing_validators_per_candidate.iter().enumerate() + { let descriptor = backing_validators.0.descriptor(); assert_eq!(ParaId::from(para_id), descriptor.para_id()); assert_eq!(header.hash(), descriptor.relay_parent()); assert_eq!(backing_validators.1.len(), votes); } - assert_eq!( - inclusion::PendingAvailability::::iter().count(), - cores_with_backed.len() - ); + assert_eq!(inclusion::PendingAvailability::::iter().count(), cores_with_backed.len()); + + Ok(()) } - enter_backed_candidate_code_upgrade { + #[benchmark] + fn enter_backed_candidate_code_upgrade() -> Result<(), BenchmarkError> { // For now we always assume worst case code size. In the future we could vary over this. let v = crate::configuration::ActiveConfig::::get().max_code_size; - let cores_with_backed: BTreeMap<_, _> - = vec![(0, BenchBuilder::::fallback_min_backing_votes())] - .into_iter() - .collect(); + let cores_with_backed: BTreeMap<_, _> = + vec![(0, BenchBuilder::::fallback_min_backing_votes())].into_iter().collect(); let scenario = BenchBuilder::::new() .set_backed_in_inherent_paras(cores_with_backed.clone()) @@ -174,21 +201,20 @@ benchmarks! { let votes = min( scheduler::Pallet::::group_validators(GroupIndex::from(0)).unwrap().len(), - BenchBuilder::::fallback_min_backing_votes() as usize + BenchBuilder::::fallback_min_backing_votes() as usize, ); // There is 1 backed assert_eq!(benchmark.backed_candidates.len(), 1); - assert_eq!( - benchmark.backed_candidates.get(0).unwrap().validity_votes().len(), - votes, - ); + assert_eq!(benchmark.backed_candidates.get(0).unwrap().validity_votes().len(), votes,); benchmark.bitfields.clear(); benchmark.disputes.clear(); crate::paras::benchmarking::generate_disordered_upgrades::(); - }: enter(RawOrigin::None, benchmark) - verify { + + #[extrinsic_call] + enter(RawOrigin::None, benchmark); + let max_validators_per_core = BenchBuilder::::fallback_max_validators_per_core(); // Assert that the block was not discarded assert!(Included::::get().is_some()); @@ -201,26 +227,22 @@ benchmarks! { // Ensure that there are an expected number of candidates let header = BenchBuilder::::header(scenario._block_number); // Traverse candidates and assert descriptors are as expected - for (para_id, backing_validators) - in vote.backing_validators_per_candidate.iter().enumerate() { - let descriptor = backing_validators.0.descriptor(); - assert_eq!(ParaId::from(para_id), descriptor.para_id()); - assert_eq!(header.hash(), descriptor.relay_parent()); - assert_eq!( - backing_validators.1.len(), - votes, - ); - } - - assert_eq!( - inclusion::PendingAvailability::::iter().count(), - cores_with_backed.len() - ); + for (para_id, backing_validators) in + vote.backing_validators_per_candidate.iter().enumerate() + { + let descriptor = backing_validators.0.descriptor(); + assert_eq!(ParaId::from(para_id), descriptor.para_id()); + assert_eq!(header.hash(), descriptor.relay_parent()); + assert_eq!(backing_validators.1.len(), votes,); + } + + assert_eq!(inclusion::PendingAvailability::::iter().count(), cores_with_backed.len()); + Ok(()) } -} -impl_benchmark_test_suite!( - Pallet, - crate::mock::new_test_ext(Default::default()), - crate::mock::Test -); + impl_benchmark_test_suite! { + Pallet, + crate::mock::new_test_ext(Default::default()), + crate::mock::Test + } +}