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		});
46
47		#[extrinsic_call]
48		_(RawOrigin::None, t.into());
49
50		assert_eq!(Now::<T>::get(), t.into(), "Time was not set.");
51	}
52
53	#[benchmark]
54	fn on_finalize() {
55		let t = MAX_TIME;
56		Pallet::<T>::set(RawOrigin::None.into(), t.into()).unwrap();
57		assert!(DidUpdate::<T>::exists(), "Time was not set.");
58
59		// Ignore read/write to `DidUpdate` since it is transient.
60		let did_update_key = DidUpdate::<T>::hashed_key().to_vec();
61		add_to_whitelist(did_update_key.into());
62
63		#[block]
64		{
65			Pallet::<T>::on_finalize(t.into());
66		}
67
68		assert!(!DidUpdate::<T>::exists(), "Time was not removed.");
69	}
70
71	impl_benchmark_test_suite! {
72		Pallet,
73		mock::new_test_ext(),
74		mock::Test
75	}
76}