referrerpolicy=no-referrer-when-downgrade

cumulus_pallet_dmp_queue/
benchmarking.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Benchmarking for the `cumulus-pallet-dmp-queue`.
17
18#![cfg(feature = "runtime-benchmarks")]
19
20use crate::*;
21
22use alloc::vec;
23use frame_benchmarking::v2::*;
24use frame_support::{pallet_prelude::*, traits::Hooks};
25
26#[benchmarks]
27mod benchmarks {
28	use super::*;
29
30	/// This benchmark uses the proper maximal message length.
31	#[benchmark]
32	fn on_idle_good_msg() {
33		let msg = vec![123; MaxDmpMessageLenOf::<T>::get() as usize];
34
35		Pages::<T>::insert(0, vec![(123, msg.clone())]);
36		PageIndex::<T>::put(PageIndexData { begin_used: 0, end_used: 1, overweight_count: 0 });
37		MigrationStatus::<T>::set(MigrationState::StartedExport { next_begin_used: 0 });
38
39		#[block]
40		{
41			Pallet::<T>::on_idle(0u32.into(), Weight::MAX);
42		}
43
44		assert_last_event::<T>(Event::Exported { page: 0 }.into());
45	}
46
47	/// This benchmark uses 64 KiB messages to emulate a large old message.
48	#[benchmark]
49	fn on_idle_large_msg() {
50		let msg = vec![123; 1 << 16];
51
52		Pages::<T>::insert(0, vec![(123, msg.clone())]);
53		PageIndex::<T>::put(PageIndexData { begin_used: 0, end_used: 1, overweight_count: 0 });
54		MigrationStatus::<T>::set(MigrationState::StartedExport { next_begin_used: 0 });
55
56		#[block]
57		{
58			Pallet::<T>::on_idle(0u32.into(), Weight::MAX);
59		}
60
61		assert_last_event::<T>(Event::Exported { page: 0 }.into());
62	}
63
64	#[benchmark]
65	fn on_idle_overweight_good_msg() {
66		let msg = vec![123; MaxDmpMessageLenOf::<T>::get() as usize];
67
68		Overweight::<T>::insert(0, (123, msg.clone()));
69		PageIndex::<T>::put(PageIndexData { begin_used: 0, end_used: 1, overweight_count: 1 });
70		MigrationStatus::<T>::set(MigrationState::StartedOverweightExport {
71			next_overweight_index: 0,
72		});
73
74		#[block]
75		{
76			Pallet::<T>::on_idle(0u32.into(), Weight::MAX);
77		}
78
79		assert_last_event::<T>(Event::ExportedOverweight { index: 0 }.into());
80	}
81
82	#[benchmark]
83	fn on_idle_overweight_large_msg() {
84		let msg = vec![123; 1 << 16];
85
86		Overweight::<T>::insert(0, (123, msg.clone()));
87		PageIndex::<T>::put(PageIndexData { begin_used: 0, end_used: 1, overweight_count: 1 });
88		MigrationStatus::<T>::set(MigrationState::StartedOverweightExport {
89			next_overweight_index: 0,
90		});
91
92		#[block]
93		{
94			Pallet::<T>::on_idle(0u32.into(), Weight::MAX);
95		}
96
97		assert_last_event::<T>(Event::ExportOverweightFailed { index: 0 }.into());
98	}
99
100	impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime);
101}
102
103fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
104	let events = frame_system::Pallet::<T>::events();
105	let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
106	let frame_system::EventRecord { event, .. } = events.last().expect("Event expected");
107	assert_eq!(event, &system_event.into());
108}