1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use codec::{Encode, Decode};
use sp_std::{vec::Vec, prelude::Box};
use sp_io::hashing::blake2_256;
use sp_storage::TrackedStorageKey;
#[derive(Encode, Decode, Clone, Copy, PartialEq, Debug)]
#[allow(missing_docs)]
#[allow(non_camel_case_types)]
pub enum BenchmarkParameter {
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,
}
#[cfg(feature = "std")]
impl std::fmt::Display for BenchmarkParameter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
#[derive(Encode, Decode, Clone, PartialEq, Debug)]
pub struct BenchmarkBatch {
pub pallet: Vec<u8>,
pub instance: Vec<u8>,
pub benchmark: Vec<u8>,
pub results: Vec<BenchmarkResults>,
}
#[derive(Encode, Decode, Default, Clone, PartialEq, Debug)]
pub struct BenchmarkResults {
pub components: Vec<(BenchmarkParameter, u32)>,
pub extrinsic_time: u128,
pub storage_root_time: u128,
pub reads: u32,
pub repeat_reads: u32,
pub writes: u32,
pub repeat_writes: u32,
}
#[derive(Encode, Decode, Default, Clone, PartialEq, Debug)]
pub struct BenchmarkConfig {
pub pallet: Vec<u8>,
pub benchmark: Vec<u8>,
pub lowest_range_values: Vec<u32>,
pub highest_range_values: Vec<u32>,
pub steps: Vec<u32>,
pub repeat: u32,
pub verify: bool,
pub extra: bool,
}
sp_api::decl_runtime_apis! {
pub trait Benchmark {
fn dispatch_benchmark(config: BenchmarkConfig) -> Result<Vec<BenchmarkBatch>, sp_runtime::RuntimeString>;
}
}
#[sp_runtime_interface::runtime_interface]
pub trait Benchmarking {
fn current_time() -> u128 {
std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH)
.expect("Unix time doesn't go backwards; qed")
.as_nanos()
}
fn wipe_db(&mut self) {
self.wipe()
}
fn commit_db(&mut self) {
self.commit()
}
fn read_write_count(&self) -> (u32, u32, u32, u32) {
self.read_write_count()
}
fn reset_read_write_count(&mut self) {
self.reset_read_write_count()
}
fn get_whitelist(&self) -> Vec<TrackedStorageKey> {
self.get_whitelist()
}
fn set_whitelist(&mut self, new: Vec<TrackedStorageKey>) {
self.set_whitelist(new)
}
fn add_to_whitelist(&mut self, add: TrackedStorageKey) {
let mut whitelist = self.get_whitelist();
match whitelist.iter_mut().find(|x| x.key == add.key) {
Some(item) => {
*item = TrackedStorageKey {
key: add.key,
has_been_read: item.has_been_read || add.has_been_read,
has_been_written: item.has_been_written || add.has_been_written,
}
},
None => {
whitelist.push(add);
}
}
self.set_whitelist(whitelist);
}
fn remove_from_whitelist(&mut self, remove: Vec<u8>) {
let mut whitelist = self.get_whitelist();
whitelist.retain(|x| x.key != remove);
self.set_whitelist(whitelist);
}
}
pub trait Benchmarking<T> {
fn benchmarks(extra: bool) -> Vec<&'static [u8]>;
fn run_benchmark(
name: &[u8],
lowest_range_values: &[u32],
highest_range_values: &[u32],
steps: &[u32],
repeat: u32,
whitelist: &[TrackedStorageKey],
verify: bool,
) -> Result<Vec<T>, &'static str>;
}
pub trait BenchmarkingSetup<T, I = ()> {
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)>;
fn instance(
&self,
components: &[(BenchmarkParameter, u32)],
verify: bool
) -> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>;
}
pub fn account<AccountId: Decode + Default>(name: &'static str, index: u32, seed: u32) -> AccountId {
let entropy = (name, index, seed).using_encoded(blake2_256);
AccountId::decode(&mut &entropy[..]).unwrap_or_default()
}
pub fn whitelisted_caller<AccountId: Decode + Default>() -> AccountId {
account::<AccountId>("whitelisted_caller", 0, 0)
}
#[macro_export]
macro_rules! whitelist_account {
($acc:ident) => {
frame_benchmarking::benchmarking::add_to_whitelist(
frame_system::Account::<T>::hashed_key_for(&$acc).into()
);
}
}