referrerpolicy=no-referrer-when-downgrade

pallet_bags_list/
benchmarks.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Benchmarks for the bags list pallet.
19
20use super::*;
21use crate::list::List;
22use alloc::{vec, vec::Vec};
23use frame_benchmarking::v1::{
24	account, benchmarks_instance_pallet, whitelist_account, whitelisted_caller,
25};
26use frame_election_provider_support::ScoreProvider;
27use frame_support::{assert_ok, traits::Get};
28use frame_system::RawOrigin as SystemOrigin;
29use sp_runtime::traits::One;
30
31benchmarks_instance_pallet! {
32	// iteration of any number of items should only touch that many nodes and bags.
33	#[extra]
34	iter {
35		let n = 100;
36
37		// clear any pre-existing storage.
38		List::<T, _>::unsafe_clear();
39
40		// add n nodes, half to the first bag and half to the second bag.
41		let bag_thresh = T::BagThresholds::get()[0];
42		let second_bag_thresh = T::BagThresholds::get()[1];
43
44
45		for i in 0..n/2 {
46			let node: T::AccountId = account("node", i, 0);
47			assert_ok!(List::<T, _>::insert(node.clone(), bag_thresh - One::one()));
48		}
49		for i in 0..n/2 {
50			let node: T::AccountId = account("node", i, 1);
51			assert_ok!(List::<T, _>::insert(node.clone(), bag_thresh + One::one()));
52		}
53		assert_eq!(
54			List::<T, _>::get_bags().into_iter().map(|(bag, nodes)| (bag, nodes.len())).collect::<Vec<_>>(),
55			vec![
56				(bag_thresh, (n / 2) as usize),
57				(second_bag_thresh, (n / 2) as usize),
58			]
59		);
60	}: {
61		let voters = <Pallet<T, _> as SortedListProvider<T::AccountId>>::iter();
62		let len = voters.collect::<Vec<_>>().len();
63		assert_eq!(len as u32, n,"len is {}, expected {}", len, n);
64	}
65
66	// iteration of any number of items should only touch that many nodes and bags.
67	#[extra]
68	iter_take {
69		let n = 100;
70
71		// clear any pre-existing storage.
72		List::<T, _>::unsafe_clear();
73
74		// add n nodes, half to the first bag and half to the second bag.
75		let bag_thresh = T::BagThresholds::get()[0];
76		let second_bag_thresh = T::BagThresholds::get()[1];
77
78
79		for i in 0..n/2 {
80			let node: T::AccountId = account("node", i, 0);
81			assert_ok!(List::<T, _>::insert(node.clone(), bag_thresh - One::one()));
82		}
83		for i in 0..n/2 {
84			let node: T::AccountId = account("node", i, 1);
85			assert_ok!(List::<T, _>::insert(node.clone(), bag_thresh + One::one()));
86		}
87		assert_eq!(
88			List::<T, _>::get_bags().into_iter().map(|(bag, nodes)| (bag, nodes.len())).collect::<Vec<_>>(),
89			vec![
90				(bag_thresh, (n / 2) as usize),
91				(second_bag_thresh, (n / 2) as usize),
92			]
93		);
94	}: {
95		// this should only go into one of the bags
96		let voters = <Pallet<T, _> as SortedListProvider<T::AccountId>>::iter().take(n as usize / 4 );
97		let len = voters.collect::<Vec<_>>().len();
98		assert_eq!(len as u32, n / 4,"len is {}, expected {}", len, n / 4);
99	}
100
101	#[extra]
102	iter_next {
103		let n = 100;
104
105		// clear any pre-existing storage.
106		List::<T, _>::unsafe_clear();
107
108		// add n nodes, half to the first bag and half to the second bag.
109		let bag_thresh = T::BagThresholds::get()[0];
110		let second_bag_thresh = T::BagThresholds::get()[1];
111
112
113		for i in 0..n/2 {
114			let node: T::AccountId = account("node", i, 0);
115			assert_ok!(List::<T, _>::insert(node.clone(), bag_thresh - One::one()));
116		}
117		for i in 0..n/2 {
118			let node: T::AccountId = account("node", i, 1);
119			assert_ok!(List::<T, _>::insert(node.clone(), bag_thresh + One::one()));
120		}
121		assert_eq!(
122			List::<T, _>::get_bags().into_iter().map(|(bag, nodes)| (bag, nodes.len())).collect::<Vec<_>>(),
123			vec![
124				(bag_thresh, (n / 2) as usize),
125				(second_bag_thresh, (n / 2) as usize),
126			]
127		);
128	}: {
129		// this should only go into one of the bags
130		let mut iter_var = <Pallet<T, _> as SortedListProvider<T::AccountId>>::iter();
131		let mut voters = Vec::<T::AccountId>::with_capacity((n/4) as usize);
132		for _ in 0..(n/4) {
133			let next = iter_var.next().unwrap();
134			voters.push(next);
135		}
136
137		let len = voters.len();
138		assert_eq!(len as u32, n / 4,"len is {}, expected {}", len, n / 4);
139	}
140
141	#[extra]
142	iter_from {
143		let n = 100;
144
145		// clear any pre-existing storage.
146		List::<T, _>::unsafe_clear();
147
148		// populate the first 4 bags with n/4 nodes each
149		let bag_thresh = T::BagThresholds::get()[0];
150
151		for i in 0..n/4 {
152			let node: T::AccountId = account("node", i, 0);
153			assert_ok!(List::<T, _>::insert(node.clone(), bag_thresh - One::one()));
154		}
155		for i in 0..n/4 {
156			let node: T::AccountId = account("node", i, 1);
157			assert_ok!(List::<T, _>::insert(node.clone(), bag_thresh + One::one()));
158		}
159
160		let bag_thresh = T::BagThresholds::get()[2];
161
162		for i in 0..n/4 {
163			let node: T::AccountId = account("node", i, 2);
164			assert_ok!(List::<T, _>::insert(node.clone(), bag_thresh - One::one()));
165		}
166
167		for i in 0..n/4 {
168			let node: T::AccountId = account("node", i, 3);
169			assert_ok!(List::<T, _>::insert(node.clone(), bag_thresh + One::one()));
170		}
171
172		assert_eq!(
173			List::<T, _>::get_bags().into_iter().map(|(bag, nodes)| (bag, nodes.len())).collect::<Vec<_>>(),
174			vec![
175				(T::BagThresholds::get()[0], (n / 4) as usize),
176				(T::BagThresholds::get()[1], (n / 4) as usize),
177				(T::BagThresholds::get()[2], (n / 4) as usize),
178				(T::BagThresholds::get()[3], (n / 4) as usize),
179			]
180		);
181
182		// iter from someone in the 3rd bag, so this should touch ~75 nodes and 3 bags
183		let from: T::AccountId = account("node", 0, 2);
184	}: {
185		let voters = <Pallet<T, _> as SortedListProvider<T::AccountId>>::iter_from(&from).unwrap();
186		let len = voters.collect::<Vec<_>>().len();
187		assert_eq!(len as u32, 74,"len is {}, expected {}", len, 74);
188	}
189
190
191	rebag_non_terminal {
192		// An expensive case for rebag-ing (rebag a non-terminal node):
193		//
194		// - The node to be rebagged, _R_, should exist as a non-terminal node in a bag with at
195		//   least 2 other nodes. Thus _R_ will have both its `prev` and `next` nodes updated when
196		//   it is removed. (3 W/R)
197		// - The destination bag is not empty, thus we need to update the `next` pointer of the last
198		//   node in the destination in addition to the work we do otherwise. (2 W/R)
199
200		// clear any pre-existing storage.
201		// NOTE: safe to call outside block production
202		List::<T, _>::unsafe_clear();
203
204		// define our origin and destination thresholds.
205		let origin_bag_thresh = T::BagThresholds::get()[0];
206		let dest_bag_thresh = T::BagThresholds::get()[1];
207
208		// seed items in the origin bag.
209		let origin_head: T::AccountId = account("origin_head", 0, 0);
210		assert_ok!(List::<T, _>::insert(origin_head.clone(), origin_bag_thresh));
211
212		let origin_middle: T::AccountId = account("origin_middle", 0, 0); // the node we rebag (_R_)
213		assert_ok!(List::<T, _>::insert(origin_middle.clone(), origin_bag_thresh));
214
215		let origin_tail: T::AccountId  = account("origin_tail", 0, 0);
216		assert_ok!(List::<T, _>::insert(origin_tail.clone(), origin_bag_thresh));
217
218		// seed items in the destination bag.
219		let dest_head: T::AccountId  = account("dest_head", 0, 0);
220		assert_ok!(List::<T, _>::insert(dest_head.clone(), dest_bag_thresh));
221
222		let origin_middle_lookup = T::Lookup::unlookup(origin_middle.clone());
223
224		// the bags are in the expected state after initial setup.
225		assert_eq!(
226			List::<T, _>::get_bags(),
227			vec![
228				(origin_bag_thresh, vec![origin_head.clone(), origin_middle.clone(), origin_tail.clone()]),
229				(dest_bag_thresh, vec![dest_head.clone()])
230			]
231		);
232
233		let caller = whitelisted_caller();
234		// update the weight of `origin_middle` to guarantee it will be rebagged into the destination.
235		T::ScoreProvider::set_score_of(&origin_middle, dest_bag_thresh);
236	}: rebag(SystemOrigin::Signed(caller), origin_middle_lookup.clone())
237	verify {
238		// check the bags have updated as expected.
239		assert_eq!(
240			List::<T, _>::get_bags(),
241			vec![
242				(
243					origin_bag_thresh,
244					vec![origin_head, origin_tail],
245				),
246				(
247					dest_bag_thresh,
248					vec![dest_head, origin_middle],
249				)
250			]
251		);
252	}
253
254	rebag_terminal {
255		// An expensive case for rebag-ing (rebag a terminal node):
256		//
257		// - The node to be rebagged, _R_, is a terminal node; so _R_, the node pointing to _R_ and
258		//   the origin bag itself will need to be updated. (3 W/R)
259		// - The destination bag is not empty, thus we need to update the `next` pointer of the last
260		//   node in the destination in addition to the work we do otherwise. (2 W/R)
261
262		// clear any pre-existing storage.
263		// NOTE: safe to call outside block production
264		List::<T, I>::unsafe_clear();
265
266		// define our origin and destination thresholds.
267		let origin_bag_thresh = T::BagThresholds::get()[0];
268		let dest_bag_thresh = T::BagThresholds::get()[1];
269
270		// seed items in the origin bag.
271		let origin_head: T::AccountId = account("origin_head", 0, 0);
272		assert_ok!(List::<T, _>::insert(origin_head.clone(), origin_bag_thresh));
273
274		let origin_tail: T::AccountId  = account("origin_tail", 0, 0); // the node we rebag (_R_)
275		assert_ok!(List::<T, _>::insert(origin_tail.clone(), origin_bag_thresh));
276
277		// seed items in the destination bag.
278		let dest_head: T::AccountId  = account("dest_head", 0, 0);
279		assert_ok!(List::<T, _>::insert(dest_head.clone(), dest_bag_thresh));
280
281		let origin_tail_lookup = T::Lookup::unlookup(origin_tail.clone());
282
283		// the bags are in the expected state after initial setup.
284		assert_eq!(
285			List::<T, _>::get_bags(),
286			vec![
287				(origin_bag_thresh, vec![origin_head.clone(), origin_tail.clone()]),
288				(dest_bag_thresh, vec![dest_head.clone()])
289			]
290		);
291
292		let caller = whitelisted_caller();
293		// update the weight of `origin_tail` to guarantee it will be rebagged into the destination.
294		T::ScoreProvider::set_score_of(&origin_tail, dest_bag_thresh);
295	}: rebag(SystemOrigin::Signed(caller), origin_tail_lookup.clone())
296	verify {
297		// check the bags have updated as expected.
298		assert_eq!(
299			List::<T, _>::get_bags(),
300			vec![
301				(origin_bag_thresh, vec![origin_head.clone()]),
302				(dest_bag_thresh, vec![dest_head.clone(), origin_tail])
303			]
304		);
305	}
306
307	put_in_front_of {
308		// The most expensive case for `put_in_front_of`:
309		//
310		// - both heavier's `prev` and `next` are nodes that will need to be read and written.
311		// - `lighter` is the bag's `head`, so the bag will need to be read and written.
312
313		// clear any pre-existing storage.
314		// NOTE: safe to call outside block production
315		List::<T, I>::unsafe_clear();
316
317		let bag_thresh = T::BagThresholds::get()[0];
318
319		// insert the nodes in order
320		let lighter: T::AccountId = account("lighter", 0, 0);
321		assert_ok!(List::<T, _>::insert(lighter.clone(), bag_thresh));
322
323		let heavier_prev: T::AccountId = account("heavier_prev", 0, 0);
324		assert_ok!(List::<T, _>::insert(heavier_prev.clone(), bag_thresh));
325
326		let heavier: T::AccountId = account("heavier", 0, 0);
327		assert_ok!(List::<T, _>::insert(heavier.clone(), bag_thresh));
328
329		let heavier_next: T::AccountId = account("heavier_next", 0, 0);
330		assert_ok!(List::<T, _>::insert(heavier_next.clone(), bag_thresh));
331
332		T::ScoreProvider::set_score_of(&lighter, bag_thresh - One::one());
333		T::ScoreProvider::set_score_of(&heavier, bag_thresh);
334
335		let lighter_lookup = T::Lookup::unlookup(lighter.clone());
336
337		assert_eq!(
338			List::<T, _>::iter().map(|n| n.id().clone()).collect::<Vec<_>>(),
339			vec![lighter.clone(), heavier_prev.clone(), heavier.clone(), heavier_next.clone()]
340		);
341
342		whitelist_account!(heavier);
343	}: _(SystemOrigin::Signed(heavier.clone()), lighter_lookup.clone())
344	verify {
345		assert_eq!(
346			List::<T, _>::iter().map(|n| n.id().clone()).collect::<Vec<_>>(),
347			vec![heavier, lighter, heavier_prev, heavier_next]
348		)
349	}
350
351	on_idle_rebag {
352		// Worst-case cost of a single `rebag_internal` call inside `on_idle`.
353		// This measures one non-terminal rebag with a pending rebag entry, which is the
354		// most expensive per-item path. `on_idle` consumes this weight per iteration via
355		// `WeightMeter`, so the benchmark is independent of `MaxAutoRebagPerBlock`.
356
357		List::<T, I>::unsafe_clear();
358
359		let bag_thresh = T::BagThresholds::get();
360		assert!(bag_thresh.len() >= 2, "on_idle_rebag benchmark requires at least 2 bag thresholds");
361		let origin_bag_thresh = bag_thresh[0];
362		let dest_bag_thresh = bag_thresh[1];
363
364		// Seed 3 nodes in the origin bag so the target node is non-terminal (has prev + next).
365		let origin_head: T::AccountId = account("origin_head", 0, 0);
366		assert_ok!(List::<T, I>::insert(origin_head.clone(), origin_bag_thresh));
367
368		let target: T::AccountId = account("target", 0, 0);
369		assert_ok!(List::<T, I>::insert(target.clone(), origin_bag_thresh));
370
371		let origin_tail: T::AccountId = account("origin_tail", 0, 0);
372		assert_ok!(List::<T, I>::insert(origin_tail.clone(), origin_bag_thresh));
373
374		// Seed a node in the destination bag so it's not empty (requires updating dest tail).
375		let dest_head: T::AccountId = account("dest_head", 0, 0);
376		assert_ok!(List::<T, I>::insert(dest_head.clone(), dest_bag_thresh));
377
378		// Add a PendingRebag entry for the target to exercise the contains_key + remove path.
379		PendingRebag::<T, I>::insert(&target, ());
380
381		// Update score to force a rebag into the destination bag.
382		T::ScoreProvider::set_score_of(&target, dest_bag_thresh);
383
384		assert_eq!(
385			List::<T, I>::get_bags(),
386			vec![
387				(origin_bag_thresh, vec![origin_head.clone(), target.clone(), origin_tail.clone()]),
388				(dest_bag_thresh, vec![dest_head.clone()])
389			]
390		);
391	}: {
392		Pallet::<T, I>::rebag_internal(&target).unwrap();
393	}
394	verify {
395		assert_eq!(
396			List::<T, I>::get_bags(),
397			vec![
398				(origin_bag_thresh, vec![origin_head, origin_tail]),
399				(dest_bag_thresh, vec![dest_head, target])
400			]
401		);
402		assert!(!PendingRebag::<T, I>::contains_key(&account::<T::AccountId>("target", 0, 0)));
403	}
404
405	impl_benchmark_test_suite!(
406		Pallet,
407		mock::ExtBuilder::default().skip_genesis_ids().build(),
408		mock::Runtime
409	);
410}