use crate::{Midds, MiddsId};
use frame_support::BoundedVec;
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};
#[cfg(feature = "std")]
use std::{vec, vec::Vec};
pub trait BenchmarkHelperT<T: Midds> {
fn min_size() -> T;
fn max_size() -> T;
fn variable_size(complexity: f32) -> T;
fn typical_size() -> T {
Self::variable_size(0.3) }
}
pub mod utils {
use super::*;
use frame_support::traits::Get;
pub fn bounded_vec_with_ratio<T: Clone + core::fmt::Debug, S: Get<u32>>(
item: T,
fill_ratio: f32,
) -> BoundedVec<T, S> {
let max_len = S::get() as usize;
let target_len = ((max_len as f32) * fill_ratio.clamp(0.0, 1.0)) as usize;
let mut vec = Vec::with_capacity(target_len);
for _ in 0..target_len {
vec.push(item.clone());
}
BoundedVec::try_from(vec).expect("Length should be within bounds")
}
pub fn bounded_string_with_length<S: Get<u32>>(
length: usize,
pattern: u8,
) -> BoundedVec<u8, S> {
let max_len = S::get() as usize;
let actual_length = length.min(max_len);
let bytes = vec![pattern; actual_length];
BoundedVec::try_from(bytes).expect("Length should be within bounds")
}
pub fn midds_id_sequence(count: usize, start_id: MiddsId) -> Vec<MiddsId> {
(start_id..start_id + count as u64).collect()
}
pub fn target_length_for_complexity<S: Get<u32>>(complexity: f32) -> usize {
let max_len = S::get() as usize;
((max_len as f32) * complexity.clamp(0.0, 1.0)) as usize
}
}
pub mod complexity {
pub const STEPS: &[f32] = &[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
pub const MIN: f32 = 0.0;
pub const TYPICAL: f32 = 0.3;
pub const MEDIUM: f32 = 0.5;
pub const HIGH: f32 = 0.8;
pub const MAX: f32 = 1.0;
}
#[macro_export]
macro_rules! generate_benchmark_series {
($midds_type:ty, $complexities:expr) => {{
$complexities
.iter()
.map(|&complexity| {
<$midds_type as $crate::Midds>::BenchmarkHelper::variable_size(complexity)
})
.collect::<Vec<$midds_type>>()
}};
}
pub fn generate_standard_series<T: Midds>() -> Vec<T> {
complexity::STEPS
.iter()
.map(|&complexity| T::BenchmarkHelper::variable_size(complexity))
.collect()
}