use codec::{Encode, Decode};
use sp_std::{vec::Vec, prelude::Box};
use sp_io::hashing::blake2_256;
use sp_runtime::RuntimeString;
#[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,
}
#[derive(Encode, Decode, Clone, PartialEq, Debug)]
pub struct BenchmarkBatch {
pub pallet: Vec<u8>,
pub benchmark: Vec<u8>,
pub results: Vec<BenchmarkResults>,
}
pub type BenchmarkResults = (Vec<(BenchmarkParameter, u32)>, u128, u128);
sp_api::decl_runtime_apis! {
pub trait Benchmark {
fn dispatch_benchmark(
pallet: Vec<u8>,
benchmark: Vec<u8>,
lowest_range_values: Vec<u32>,
highest_range_values: Vec<u32>,
steps: Vec<u32>,
repeat: u32,
) -> Result<Vec<BenchmarkBatch>, 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()
}
}
pub trait Benchmarking<T> {
fn benchmarks() -> Vec<&'static [u8]>;
fn run_benchmark(
name: &[u8],
lowest_range_values: &[u32],
highest_range_values: &[u32],
steps: &[u32],
repeat: u32,
) -> Result<Vec<T>, &'static str>;
}
pub trait BenchmarkingSetup<T> {
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)>;
fn instance(&self, components: &[(BenchmarkParameter, u32)]) -> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>;
fn verify(&self, components: &[(BenchmarkParameter, u32)]) -> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>;
}
pub trait BenchmarkingSetupInstance<T, I> {
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)>;
fn instance(&self, components: &[(BenchmarkParameter, u32)]) -> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>;
fn verify(&self, components: &[(BenchmarkParameter, u32)]) -> 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()
}