use codec::{Decode, Encode};
use frame_support::{
dispatch::{DispatchError, DispatchErrorWithPostInfo},
pallet_prelude::*,
traits::StorageInfo,
};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_io::hashing::blake2_256;
use sp_runtime::traits::TrailingZeroInput;
use sp_std::{prelude::Box, vec::Vec};
use sp_storage::TrackedStorageKey;
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Clone, Copy, PartialEq, Debug, TypeInfo)]
#[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)
}
}
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Clone, PartialEq, Debug, TypeInfo)]
pub struct BenchmarkBatch {
#[cfg_attr(feature = "std", serde(with = "serde_as_str"))]
pub pallet: Vec<u8>,
#[cfg_attr(feature = "std", serde(with = "serde_as_str"))]
pub instance: Vec<u8>,
#[cfg_attr(feature = "std", serde(with = "serde_as_str"))]
pub benchmark: Vec<u8>,
pub results: Vec<BenchmarkResult>,
}
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Clone, PartialEq, Debug)]
pub struct BenchmarkBatchSplitResults {
#[cfg_attr(feature = "std", serde(with = "serde_as_str"))]
pub pallet: Vec<u8>,
#[cfg_attr(feature = "std", serde(with = "serde_as_str"))]
pub instance: Vec<u8>,
#[cfg_attr(feature = "std", serde(with = "serde_as_str"))]
pub benchmark: Vec<u8>,
pub time_results: Vec<BenchmarkResult>,
pub db_results: Vec<BenchmarkResult>,
}
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Default, Clone, PartialEq, Debug, TypeInfo)]
pub struct BenchmarkResult {
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,
pub proof_size: u32,
#[cfg_attr(feature = "std", serde(skip))]
pub keys: Vec<(Vec<u8>, u32, u32, bool)>,
}
impl BenchmarkResult {
pub fn from_weight(w: Weight) -> Self {
Self { extrinsic_time: (w.ref_time() / 1_000) as u128, ..Default::default() }
}
}
#[cfg(feature = "std")]
mod serde_as_str {
pub fn serialize<S>(value: &Vec<u8>, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let s = std::str::from_utf8(value).map_err(serde::ser::Error::custom)?;
serializer.collect_str(s)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let s: &str = serde::de::Deserialize::deserialize(deserializer)?;
Ok(s.into())
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum BenchmarkError {
Stop(&'static str),
Override(BenchmarkResult),
Skip,
Weightless,
}
impl From<BenchmarkError> for &'static str {
fn from(e: BenchmarkError) -> Self {
match e {
BenchmarkError::Stop(s) => s,
BenchmarkError::Override(_) => "benchmark override",
BenchmarkError::Skip => "benchmark skip",
BenchmarkError::Weightless => "benchmark weightless",
}
}
}
impl From<&'static str> for BenchmarkError {
fn from(s: &'static str) -> Self {
Self::Stop(s)
}
}
impl From<DispatchErrorWithPostInfo> for BenchmarkError {
fn from(e: DispatchErrorWithPostInfo) -> Self {
Self::Stop(e.into())
}
}
impl From<DispatchError> for BenchmarkError {
fn from(e: DispatchError) -> Self {
Self::Stop(e.into())
}
}
#[derive(Encode, Decode, Default, Clone, PartialEq, Debug, TypeInfo)]
pub struct BenchmarkConfig {
pub pallet: Vec<u8>,
pub benchmark: Vec<u8>,
pub selected_components: Vec<(BenchmarkParameter, u32)>,
pub verify: bool,
pub internal_repeats: u32,
}
#[derive(Encode, Decode, Default, Clone, PartialEq, Debug, TypeInfo)]
pub struct BenchmarkList {
pub pallet: Vec<u8>,
pub instance: Vec<u8>,
pub benchmarks: Vec<BenchmarkMetadata>,
}
#[derive(Encode, Decode, Default, Clone, PartialEq, Debug, TypeInfo)]
pub struct BenchmarkMetadata {
pub name: Vec<u8>,
pub components: Vec<(BenchmarkParameter, u32, u32)>,
pub pov_modes: Vec<(Vec<u8>, Vec<u8>)>,
}
sp_api::decl_runtime_apis! {
pub trait Benchmark {
fn benchmark_metadata(extra: bool) -> (Vec<BenchmarkList>, Vec<StorageInfo>);
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.reads += add.reads;
item.writes += add.writes;
item.whitelisted = item.whitelisted || add.whitelisted;
},
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);
}
fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)> {
self.get_read_and_written_keys()
}
fn proof_size(&self) -> Option<u32> {
self.proof_size()
}
}
pub trait Benchmarking {
fn benchmarks(extra: bool) -> Vec<BenchmarkMetadata>;
fn run_benchmark(
name: &[u8],
selected_components: &[(BenchmarkParameter, u32)],
whitelist: &[TrackedStorageKey],
verify: bool,
internal_repeats: u32,
) -> Result<Vec<BenchmarkResult>, BenchmarkError>;
}
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<(), BenchmarkError>>, BenchmarkError>;
}
pub fn account<AccountId: Decode>(name: &'static str, index: u32, seed: u32) -> AccountId {
let entropy = (name, index, seed).using_encoded(blake2_256);
Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref()))
.expect("infinite length input; no invalid inputs for type; qed")
}
pub fn whitelisted_caller<AccountId: Decode>() -> 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(),
);
};
}