referrerpolicy=no-referrer-when-downgrade

pallet_timestamp/
benchmarking.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//! Timestamp pallet benchmarking.
19
20#![cfg(feature = "runtime-benchmarks")]
21
22use frame_benchmarking::{benchmarking::add_to_whitelist, v2::*};
23use frame_support::traits::OnFinalize;
24use frame_system::RawOrigin;
25use sp_storage::TrackedStorageKey;
26
27use crate::*;
28
29const MAX_TIME: u32 = 100;
30
31#[benchmarks]
32mod benchmarks {
33	use super::*;
34
35	#[benchmark]
36	fn set() {
37		let t = MAX_TIME;
38		// Ignore write to `DidUpdate` since it transient.
39		let did_update_key = DidUpdate::<T>::hashed_key().to_vec();
40		add_to_whitelist(TrackedStorageKey {
41			key: did_update_key,
42			reads: 0,
43			writes: 1,
44			whitelisted: false,
45			child_trie_key: None,
46		});
47
48		#[extrinsic_call]
49		_(RawOrigin::None, t.into());
50
51		assert_eq!(Now::<T>::get(), t.into(), "Time was not set.");
52	}
53
54	#[benchmark]
55	fn on_finalize() {
56		let t = MAX_TIME;
57		Pallet::<T>::set(RawOrigin::None.into(), t.into()).unwrap();
58		assert!(DidUpdate::<T>::exists(), "Time was not set.");
59
60		// Ignore read/write to `DidUpdate` since it is transient.
61		let did_update_key = DidUpdate::<T>::hashed_key().to_vec();
62		add_to_whitelist(did_update_key.into());
63
64		#[block]
65		{
66			Pallet::<T>::on_finalize(t.into());
67		}
68
69		assert!(!DidUpdate::<T>::exists(), "Time was not removed.");
70	}
71
72	impl_benchmark_test_suite! {
73		Pallet,
74		mock::new_test_ext(),
75		mock::Test
76	}
77}