frame_benchmarking/
baseline.rs1#![cfg(feature = "runtime-benchmarks")]
22
23use crate::benchmarks;
24use alloc::{vec, vec::Vec};
25use frame_system::Pallet as System;
26use sp_runtime::{
27 traits::{AppVerify, Hash},
28 RuntimeAppPublic,
29};
30
31mod crypto {
32 use sp_application_crypto::{app_crypto, sr25519, KeyTypeId};
33
34 pub const TEST_KEY_TYPE_ID: KeyTypeId = KeyTypeId(*b"test");
35 app_crypto!(sr25519, TEST_KEY_TYPE_ID);
36}
37pub type SignerId = crypto::Public;
38
39pub struct Pallet<T: Config>(System<T>);
40pub trait Config: frame_system::Config {}
41
42benchmarks! {
43 addition {
44 let i in 0 .. 1_000_000;
45 let mut start = 0;
46 }: {
47 (0..i).for_each(|_| start += 1);
48 } verify {
49 assert_eq!(start, i);
50 }
51
52 subtraction {
53 let i in 0 .. 1_000_000;
54 let mut start = u32::MAX;
55 }: {
56 (0..i).for_each(|_| start -= 1);
57 } verify {
58 assert_eq!(start, u32::MAX - i);
59 }
60
61 multiplication {
62 let i in 0 .. 1_000_000;
63 let mut out = 0;
64 }: {
65 (1..=i).for_each(|j| out = 2 * j);
66 } verify {
67 assert_eq!(out, 2 * i);
68 }
69
70 division {
71 let i in 0 .. 1_000_000;
72 let mut out = 0;
73 }: {
74 (0..=i).for_each(|j| out = j / 2);
75 } verify {
76 assert_eq!(out, i / 2);
77 }
78
79 hashing {
80 let mut hash = T::Hash::default();
81 }: {
82 (0..=100_000u32).for_each(|j| hash = T::Hashing::hash(&j.to_be_bytes()));
83 } verify {
84 assert!(hash != T::Hash::default());
85 }
86
87 sr25519_verification {
88 let i in 0 .. 100;
89
90 let public = SignerId::generate_pair(None);
91
92 let sigs_count: u8 = i.try_into().unwrap();
93 let msg_and_sigs: Vec<_> = (0..sigs_count).map(|j| {
94 let msg = vec![j, j];
95 (msg.clone(), public.sign(&msg).unwrap())
96 })
97 .collect();
98 }: {
99 msg_and_sigs.iter().for_each(|(msg, sig)| {
100 assert!(sig.verify(&msg[..], &public));
101 });
102 }
103
104 impl_benchmark_test_suite!(
105 Pallet,
106 mock::new_test_ext(),
107 mock::Test,
108 );
109}
110
111#[cfg(test)]
112pub mod mock {
113 use frame_support::derive_impl;
114 use sp_runtime::{testing::H256, BuildStorage};
115
116 type AccountId = u64;
117 type Nonce = u32;
118
119 type Block = frame_system::mocking::MockBlock<Test>;
120
121 frame_support::construct_runtime!(
122 pub enum Test
123 {
124 System: frame_system,
125 }
126 );
127
128 #[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
129 impl frame_system::Config for Test {
130 type BaseCallFilter = frame_support::traits::Everything;
131 type BlockWeights = ();
132 type BlockLength = ();
133 type DbWeight = ();
134 type RuntimeOrigin = RuntimeOrigin;
135 type Nonce = Nonce;
136 type RuntimeCall = RuntimeCall;
137 type Hash = H256;
138 type Hashing = ::sp_runtime::traits::BlakeTwo256;
139 type AccountId = AccountId;
140 type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
141 type Block = Block;
142 type RuntimeEvent = RuntimeEvent;
143 type BlockHashCount = ();
144 type Version = ();
145 type PalletInfo = PalletInfo;
146 type AccountData = ();
147 type OnNewAccount = ();
148 type OnKilledAccount = ();
149 type SystemWeightInfo = ();
150 type SS58Prefix = ();
151 type OnSetCode = ();
152 type MaxConsumers = frame_support::traits::ConstU32<16>;
153 }
154
155 impl super::Config for Test {}
156
157 pub fn new_test_ext() -> sp_io::TestExternalities {
158 use sp_keystore::{testing::MemoryKeystore, KeystoreExt};
159
160 let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
161 let mut ext = sp_io::TestExternalities::new(t);
162 ext.register_extension(KeystoreExt::new(MemoryKeystore::new()));
163
164 ext
165 }
166}