mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 08:51:09 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -17,10 +17,10 @@
|
||||
|
||||
//! Tools for analyzing the benchmark results.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use crate::BenchmarkResults;
|
||||
use core::convert::TryFrom;
|
||||
use linregress::{FormulaRegressionBuilder, RegressionDataBuilder};
|
||||
use crate::BenchmarkResults;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
pub use linregress::RegressionModel;
|
||||
|
||||
@@ -63,14 +63,12 @@ impl TryFrom<Option<String>> for AnalysisChoice {
|
||||
fn try_from(s: Option<String>) -> Result<Self, Self::Error> {
|
||||
match s {
|
||||
None => Ok(AnalysisChoice::default()),
|
||||
Some(i) => {
|
||||
match &i[..] {
|
||||
"min-squares" | "min_squares" => Ok(AnalysisChoice::MinSquares),
|
||||
"median-slopes" | "median_slopes" => Ok(AnalysisChoice::MedianSlopes),
|
||||
"max" => Ok(AnalysisChoice::Max),
|
||||
_ => Err("invalid analysis string")
|
||||
}
|
||||
}
|
||||
Some(i) => match &i[..] {
|
||||
"min-squares" | "min_squares" => Ok(AnalysisChoice::MinSquares),
|
||||
"median-slopes" | "median_slopes" => Ok(AnalysisChoice::MedianSlopes),
|
||||
"max" => Ok(AnalysisChoice::Max),
|
||||
_ => Err("invalid analysis string"),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,17 +77,20 @@ impl Analysis {
|
||||
// Useful for when there are no components, and we just need an median value of the benchmark results.
|
||||
// Note: We choose the median value because it is more robust to outliers.
|
||||
fn median_value(r: &Vec<BenchmarkResults>, selector: BenchmarkSelector) -> Option<Self> {
|
||||
if r.is_empty() { return None }
|
||||
if r.is_empty() {
|
||||
return None
|
||||
}
|
||||
|
||||
let mut values: Vec<u128> = r.iter().map(|result|
|
||||
match selector {
|
||||
let mut values: Vec<u128> = r
|
||||
.iter()
|
||||
.map(|result| match selector {
|
||||
BenchmarkSelector::ExtrinsicTime => result.extrinsic_time,
|
||||
BenchmarkSelector::StorageRootTime => result.storage_root_time,
|
||||
BenchmarkSelector::Reads => result.reads.into(),
|
||||
BenchmarkSelector::Writes => result.writes.into(),
|
||||
BenchmarkSelector::ProofSize => result.proof_size.into(),
|
||||
}
|
||||
).collect();
|
||||
})
|
||||
.collect();
|
||||
|
||||
values.sort();
|
||||
let mid = values.len() / 2;
|
||||
@@ -104,64 +105,80 @@ impl Analysis {
|
||||
}
|
||||
|
||||
pub fn median_slopes(r: &Vec<BenchmarkResults>, selector: BenchmarkSelector) -> Option<Self> {
|
||||
if r[0].components.is_empty() { return Self::median_value(r, selector) }
|
||||
if r[0].components.is_empty() {
|
||||
return Self::median_value(r, selector)
|
||||
}
|
||||
|
||||
let results = r[0].components.iter().enumerate().map(|(i, &(param, _))| {
|
||||
let mut counted = BTreeMap::<Vec<u32>, usize>::new();
|
||||
for result in r.iter() {
|
||||
let mut p = result.components.iter().map(|x| x.1).collect::<Vec<_>>();
|
||||
p[i] = 0;
|
||||
*counted.entry(p).or_default() += 1;
|
||||
}
|
||||
let others: Vec<u32> = counted.iter().max_by_key(|i| i.1).expect("r is not empty; qed").0.clone();
|
||||
let values = r.iter()
|
||||
.filter(|v|
|
||||
v.components.iter()
|
||||
.map(|x| x.1)
|
||||
.zip(others.iter())
|
||||
.enumerate()
|
||||
.all(|(j, (v1, v2))| j == i || v1 == *v2)
|
||||
).map(|result| {
|
||||
// Extract the data we are interested in analyzing
|
||||
let data = match selector {
|
||||
BenchmarkSelector::ExtrinsicTime => result.extrinsic_time,
|
||||
BenchmarkSelector::StorageRootTime => result.storage_root_time,
|
||||
BenchmarkSelector::Reads => result.reads.into(),
|
||||
BenchmarkSelector::Writes => result.writes.into(),
|
||||
BenchmarkSelector::ProofSize => result.proof_size.into(),
|
||||
};
|
||||
(result.components[i].1, data)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
(format!("{:?}", param), i, others, values)
|
||||
}).collect::<Vec<_>>();
|
||||
let results = r[0]
|
||||
.components
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, &(param, _))| {
|
||||
let mut counted = BTreeMap::<Vec<u32>, usize>::new();
|
||||
for result in r.iter() {
|
||||
let mut p = result.components.iter().map(|x| x.1).collect::<Vec<_>>();
|
||||
p[i] = 0;
|
||||
*counted.entry(p).or_default() += 1;
|
||||
}
|
||||
let others: Vec<u32> =
|
||||
counted.iter().max_by_key(|i| i.1).expect("r is not empty; qed").0.clone();
|
||||
let values = r
|
||||
.iter()
|
||||
.filter(|v| {
|
||||
v.components
|
||||
.iter()
|
||||
.map(|x| x.1)
|
||||
.zip(others.iter())
|
||||
.enumerate()
|
||||
.all(|(j, (v1, v2))| j == i || v1 == *v2)
|
||||
})
|
||||
.map(|result| {
|
||||
// Extract the data we are interested in analyzing
|
||||
let data = match selector {
|
||||
BenchmarkSelector::ExtrinsicTime => result.extrinsic_time,
|
||||
BenchmarkSelector::StorageRootTime => result.storage_root_time,
|
||||
BenchmarkSelector::Reads => result.reads.into(),
|
||||
BenchmarkSelector::Writes => result.writes.into(),
|
||||
BenchmarkSelector::ProofSize => result.proof_size.into(),
|
||||
};
|
||||
(result.components[i].1, data)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
(format!("{:?}", param), i, others, values)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let models = results.iter().map(|(_, _, _, ref values)| {
|
||||
let mut slopes = vec![];
|
||||
for (i, &(x1, y1)) in values.iter().enumerate() {
|
||||
for &(x2, y2) in values.iter().skip(i + 1) {
|
||||
if x1 != x2 {
|
||||
slopes.push((y1 as f64 - y2 as f64) / (x1 as f64 - x2 as f64));
|
||||
let models = results
|
||||
.iter()
|
||||
.map(|(_, _, _, ref values)| {
|
||||
let mut slopes = vec![];
|
||||
for (i, &(x1, y1)) in values.iter().enumerate() {
|
||||
for &(x2, y2) in values.iter().skip(i + 1) {
|
||||
if x1 != x2 {
|
||||
slopes.push((y1 as f64 - y2 as f64) / (x1 as f64 - x2 as f64));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
slopes.sort_by(|a, b| a.partial_cmp(b).expect("values well defined; qed"));
|
||||
let slope = slopes[slopes.len() / 2];
|
||||
slopes.sort_by(|a, b| a.partial_cmp(b).expect("values well defined; qed"));
|
||||
let slope = slopes[slopes.len() / 2];
|
||||
|
||||
let mut offsets = vec![];
|
||||
for &(x, y) in values.iter() {
|
||||
offsets.push(y as f64 - slope * x as f64);
|
||||
}
|
||||
offsets.sort_by(|a, b| a.partial_cmp(b).expect("values well defined; qed"));
|
||||
let offset = offsets[offsets.len() / 2];
|
||||
let mut offsets = vec![];
|
||||
for &(x, y) in values.iter() {
|
||||
offsets.push(y as f64 - slope * x as f64);
|
||||
}
|
||||
offsets.sort_by(|a, b| a.partial_cmp(b).expect("values well defined; qed"));
|
||||
let offset = offsets[offsets.len() / 2];
|
||||
|
||||
(offset, slope)
|
||||
}).collect::<Vec<_>>();
|
||||
(offset, slope)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let models = models.iter()
|
||||
let models = models
|
||||
.iter()
|
||||
.zip(results.iter())
|
||||
.map(|((offset, slope), (_, i, others, _))| {
|
||||
let over = others.iter()
|
||||
let over = others
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(j, _)| j != i)
|
||||
.map(|(j, v)| models[j].1 * *v as f64)
|
||||
@@ -183,18 +200,20 @@ impl Analysis {
|
||||
}
|
||||
|
||||
pub fn min_squares_iqr(r: &Vec<BenchmarkResults>, selector: BenchmarkSelector) -> Option<Self> {
|
||||
if r[0].components.is_empty() { return Self::median_value(r, selector) }
|
||||
if r[0].components.is_empty() {
|
||||
return Self::median_value(r, selector)
|
||||
}
|
||||
|
||||
let mut results = BTreeMap::<Vec<u32>, Vec<u128>>::new();
|
||||
for result in r.iter() {
|
||||
let p = result.components.iter().map(|x| x.1).collect::<Vec<_>>();
|
||||
results.entry(p).or_default().push(match selector {
|
||||
BenchmarkSelector::ExtrinsicTime => result.extrinsic_time,
|
||||
BenchmarkSelector::StorageRootTime => result.storage_root_time,
|
||||
BenchmarkSelector::Reads => result.reads.into(),
|
||||
BenchmarkSelector::Writes => result.writes.into(),
|
||||
BenchmarkSelector::ProofSize => result.proof_size.into(),
|
||||
})
|
||||
BenchmarkSelector::ExtrinsicTime => result.extrinsic_time,
|
||||
BenchmarkSelector::StorageRootTime => result.storage_root_time,
|
||||
BenchmarkSelector::Reads => result.reads.into(),
|
||||
BenchmarkSelector::Writes => result.writes.into(),
|
||||
BenchmarkSelector::ProofSize => result.proof_size.into(),
|
||||
})
|
||||
}
|
||||
|
||||
for (_, rs) in results.iter_mut() {
|
||||
@@ -203,21 +222,19 @@ impl Analysis {
|
||||
*rs = rs[ql..rs.len() - ql].to_vec();
|
||||
}
|
||||
|
||||
let mut data = vec![("Y", results.iter().flat_map(|x| x.1.iter().map(|v| *v as f64)).collect())];
|
||||
let mut data =
|
||||
vec![("Y", results.iter().flat_map(|x| x.1.iter().map(|v| *v as f64)).collect())];
|
||||
|
||||
let names = r[0].components.iter().map(|x| format!("{:?}", x.0)).collect::<Vec<_>>();
|
||||
data.extend(names.iter()
|
||||
.enumerate()
|
||||
.map(|(i, p)| (
|
||||
data.extend(names.iter().enumerate().map(|(i, p)| {
|
||||
(
|
||||
p.as_str(),
|
||||
results.iter()
|
||||
.flat_map(|x| Some(x.0[i] as f64)
|
||||
.into_iter()
|
||||
.cycle()
|
||||
.take(x.1.len())
|
||||
).collect::<Vec<_>>()
|
||||
))
|
||||
);
|
||||
results
|
||||
.iter()
|
||||
.flat_map(|x| Some(x.0[i] as f64).into_iter().cycle().take(x.1.len()))
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
}));
|
||||
|
||||
let data = RegressionDataBuilder::new().build_from(data).ok()?;
|
||||
|
||||
@@ -227,25 +244,31 @@ impl Analysis {
|
||||
.fit()
|
||||
.ok()?;
|
||||
|
||||
let slopes = model.parameters.regressor_values.iter()
|
||||
let slopes = model
|
||||
.parameters
|
||||
.regressor_values
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(_, x)| (*x + 0.5) as u128)
|
||||
.collect();
|
||||
|
||||
let value_dists = results.iter().map(|(p, vs)| {
|
||||
// Avoid divide by zero
|
||||
if vs.len() == 0 { return (p.clone(), 0, 0) }
|
||||
let total = vs.iter()
|
||||
.fold(0u128, |acc, v| acc + *v);
|
||||
let mean = total / vs.len() as u128;
|
||||
let sum_sq_diff = vs.iter()
|
||||
.fold(0u128, |acc, v| {
|
||||
let value_dists = results
|
||||
.iter()
|
||||
.map(|(p, vs)| {
|
||||
// Avoid divide by zero
|
||||
if vs.len() == 0 {
|
||||
return (p.clone(), 0, 0)
|
||||
}
|
||||
let total = vs.iter().fold(0u128, |acc, v| acc + *v);
|
||||
let mean = total / vs.len() as u128;
|
||||
let sum_sq_diff = vs.iter().fold(0u128, |acc, v| {
|
||||
let d = mean.max(*v) - mean.min(*v);
|
||||
acc + d * d
|
||||
});
|
||||
let stddev = (sum_sq_diff as f64 / vs.len() as f64).sqrt() as u128;
|
||||
(p.clone(), mean, stddev)
|
||||
}).collect::<Vec<_>>();
|
||||
let stddev = (sum_sq_diff as f64 / vs.len() as f64).sqrt() as u128;
|
||||
(p.clone(), mean, stddev)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Some(Self {
|
||||
base: (model.parameters.intercept_value + 0.5) as u128,
|
||||
@@ -261,32 +284,30 @@ impl Analysis {
|
||||
let min_squares = Self::min_squares_iqr(r, selector);
|
||||
|
||||
if median_slopes.is_none() || min_squares.is_none() {
|
||||
return None;
|
||||
return None
|
||||
}
|
||||
|
||||
let median_slopes = median_slopes.unwrap();
|
||||
let min_squares = min_squares.unwrap();
|
||||
|
||||
let base = median_slopes.base.max(min_squares.base);
|
||||
let slopes = median_slopes.slopes.into_iter()
|
||||
let slopes = median_slopes
|
||||
.slopes
|
||||
.into_iter()
|
||||
.zip(min_squares.slopes.into_iter())
|
||||
.map(|(a, b): (u128, u128)| { a.max(b) })
|
||||
.map(|(a, b): (u128, u128)| a.max(b))
|
||||
.collect::<Vec<u128>>();
|
||||
// components should always be in the same order
|
||||
median_slopes.names.iter()
|
||||
median_slopes
|
||||
.names
|
||||
.iter()
|
||||
.zip(min_squares.names.iter())
|
||||
.for_each(|(a, b)| assert!(a == b, "benchmark results not in the same order"));
|
||||
let names = median_slopes.names;
|
||||
let value_dists = min_squares.value_dists;
|
||||
let model = min_squares.model;
|
||||
|
||||
Some(Self {
|
||||
base,
|
||||
slopes,
|
||||
names,
|
||||
value_dists,
|
||||
model,
|
||||
})
|
||||
Some(Self { base, slopes, names, value_dists, model })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,7 +316,7 @@ fn ms(mut nanos: u128) -> String {
|
||||
while x > 1 {
|
||||
if nanos > x * 1_000 {
|
||||
nanos = nanos / x * x;
|
||||
break;
|
||||
break
|
||||
}
|
||||
x /= 10;
|
||||
}
|
||||
@@ -306,19 +327,35 @@ impl std::fmt::Display for Analysis {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
if let Some(ref value_dists) = self.value_dists {
|
||||
writeln!(f, "\nData points distribution:")?;
|
||||
writeln!(f, "{} mean µs sigma µs %", self.names.iter().map(|p| format!("{:>5}", p)).collect::<Vec<_>>().join(" "))?;
|
||||
writeln!(
|
||||
f,
|
||||
"{} mean µs sigma µs %",
|
||||
self.names.iter().map(|p| format!("{:>5}", p)).collect::<Vec<_>>().join(" ")
|
||||
)?;
|
||||
for (param_values, mean, sigma) in value_dists.iter() {
|
||||
if *mean == 0 {
|
||||
writeln!(f, "{} {:>8} {:>8} {:>3}.{}%",
|
||||
param_values.iter().map(|v| format!("{:>5}", v)).collect::<Vec<_>>().join(" "),
|
||||
writeln!(
|
||||
f,
|
||||
"{} {:>8} {:>8} {:>3}.{}%",
|
||||
param_values
|
||||
.iter()
|
||||
.map(|v| format!("{:>5}", v))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" "),
|
||||
ms(*mean),
|
||||
ms(*sigma),
|
||||
"?",
|
||||
"?"
|
||||
)?;
|
||||
} else {
|
||||
writeln!(f, "{} {:>8} {:>8} {:>3}.{}%",
|
||||
param_values.iter().map(|v| format!("{:>5}", v)).collect::<Vec<_>>().join(" "),
|
||||
writeln!(
|
||||
f,
|
||||
"{} {:>8} {:>8} {:>3}.{}%",
|
||||
param_values
|
||||
.iter()
|
||||
.map(|v| format!("{:>5}", v))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" "),
|
||||
ms(*mean),
|
||||
ms(*sigma),
|
||||
(sigma * 100 / mean),
|
||||
@@ -350,7 +387,7 @@ impl std::fmt::Debug for Analysis {
|
||||
for (&m, n) in self.slopes.iter().zip(self.names.iter()) {
|
||||
write!(f, " + ({} * {})", m, n)?;
|
||||
}
|
||||
write!(f,"")
|
||||
write!(f, "")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,17 +419,66 @@ mod tests {
|
||||
#[test]
|
||||
fn analysis_median_slopes_should_work() {
|
||||
let data = vec![
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 1), (BenchmarkParameter::m, 5)], 11_500_000, 0, 3, 10),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 2), (BenchmarkParameter::m, 5)], 12_500_000, 0, 4, 10),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 5)], 13_500_000, 0, 5, 10),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 4), (BenchmarkParameter::m, 5)], 14_500_000, 0, 6, 10),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 1)], 13_100_000, 0, 5, 2),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 3)], 13_300_000, 0, 5, 6),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 7)], 13_700_000, 0, 5, 14),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 10)], 14_000_000, 0, 5, 20),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 1), (BenchmarkParameter::m, 5)],
|
||||
11_500_000,
|
||||
0,
|
||||
3,
|
||||
10,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 2), (BenchmarkParameter::m, 5)],
|
||||
12_500_000,
|
||||
0,
|
||||
4,
|
||||
10,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 5)],
|
||||
13_500_000,
|
||||
0,
|
||||
5,
|
||||
10,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 4), (BenchmarkParameter::m, 5)],
|
||||
14_500_000,
|
||||
0,
|
||||
6,
|
||||
10,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 1)],
|
||||
13_100_000,
|
||||
0,
|
||||
5,
|
||||
2,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 3)],
|
||||
13_300_000,
|
||||
0,
|
||||
5,
|
||||
6,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 7)],
|
||||
13_700_000,
|
||||
0,
|
||||
5,
|
||||
14,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 10)],
|
||||
14_000_000,
|
||||
0,
|
||||
5,
|
||||
20,
|
||||
),
|
||||
];
|
||||
|
||||
let extrinsic_time = Analysis::median_slopes(&data, BenchmarkSelector::ExtrinsicTime).unwrap();
|
||||
let extrinsic_time =
|
||||
Analysis::median_slopes(&data, BenchmarkSelector::ExtrinsicTime).unwrap();
|
||||
assert_eq!(extrinsic_time.base, 10_000_000);
|
||||
assert_eq!(extrinsic_time.slopes, vec![1_000_000, 100_000]);
|
||||
|
||||
@@ -408,17 +494,66 @@ mod tests {
|
||||
#[test]
|
||||
fn analysis_median_min_squares_should_work() {
|
||||
let data = vec![
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 1), (BenchmarkParameter::m, 5)], 11_500_000, 0, 3, 10),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 2), (BenchmarkParameter::m, 5)], 12_500_000, 0, 4, 10),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 5)], 13_500_000, 0, 5, 10),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 4), (BenchmarkParameter::m, 5)], 14_500_000, 0, 6, 10),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 1)], 13_100_000, 0, 5, 2),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 3)], 13_300_000, 0, 5, 6),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 7)], 13_700_000, 0, 5, 14),
|
||||
benchmark_result(vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 10)], 14_000_000, 0, 5, 20),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 1), (BenchmarkParameter::m, 5)],
|
||||
11_500_000,
|
||||
0,
|
||||
3,
|
||||
10,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 2), (BenchmarkParameter::m, 5)],
|
||||
12_500_000,
|
||||
0,
|
||||
4,
|
||||
10,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 5)],
|
||||
13_500_000,
|
||||
0,
|
||||
5,
|
||||
10,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 4), (BenchmarkParameter::m, 5)],
|
||||
14_500_000,
|
||||
0,
|
||||
6,
|
||||
10,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 1)],
|
||||
13_100_000,
|
||||
0,
|
||||
5,
|
||||
2,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 3)],
|
||||
13_300_000,
|
||||
0,
|
||||
5,
|
||||
6,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 7)],
|
||||
13_700_000,
|
||||
0,
|
||||
5,
|
||||
14,
|
||||
),
|
||||
benchmark_result(
|
||||
vec![(BenchmarkParameter::n, 3), (BenchmarkParameter::m, 10)],
|
||||
14_000_000,
|
||||
0,
|
||||
5,
|
||||
20,
|
||||
),
|
||||
];
|
||||
|
||||
let extrinsic_time = Analysis::min_squares_iqr(&data, BenchmarkSelector::ExtrinsicTime).unwrap();
|
||||
let extrinsic_time =
|
||||
Analysis::min_squares_iqr(&data, BenchmarkSelector::ExtrinsicTime).unwrap();
|
||||
assert_eq!(extrinsic_time.base, 10_000_000);
|
||||
assert_eq!(extrinsic_time.slopes, vec![1_000_000, 100_000]);
|
||||
|
||||
|
||||
@@ -19,35 +19,35 @@
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
mod tests;
|
||||
mod utils;
|
||||
#[cfg(feature = "std")]
|
||||
mod analysis;
|
||||
mod tests;
|
||||
mod utils;
|
||||
|
||||
pub use utils::*;
|
||||
#[cfg(feature = "std")]
|
||||
pub use analysis::{Analysis, BenchmarkSelector, RegressionModel, AnalysisChoice};
|
||||
pub use analysis::{Analysis, AnalysisChoice, BenchmarkSelector, RegressionModel};
|
||||
#[doc(hidden)]
|
||||
pub use frame_support;
|
||||
#[doc(hidden)]
|
||||
pub use log;
|
||||
#[doc(hidden)]
|
||||
pub use paste;
|
||||
#[doc(hidden)]
|
||||
pub use sp_io::storage::root as storage_root;
|
||||
#[doc(hidden)]
|
||||
pub use sp_runtime::traits::Zero;
|
||||
#[doc(hidden)]
|
||||
pub use frame_support;
|
||||
#[doc(hidden)]
|
||||
pub use sp_std::{self, vec, prelude::Vec, boxed::Box};
|
||||
#[doc(hidden)]
|
||||
pub use paste;
|
||||
pub use sp_std::{self, boxed::Box, prelude::Vec, vec};
|
||||
#[doc(hidden)]
|
||||
pub use sp_storage::TrackedStorageKey;
|
||||
#[doc(hidden)]
|
||||
pub use log;
|
||||
pub use utils::*;
|
||||
|
||||
/// Whitelist the given account.
|
||||
#[macro_export]
|
||||
macro_rules! whitelist {
|
||||
($acc:ident) => {
|
||||
frame_benchmarking::benchmarking::add_to_whitelist(
|
||||
frame_system::Account::<T>::hashed_key_for(&$acc).into()
|
||||
frame_system::Account::<T>::hashed_key_for(&$acc).into(),
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -1081,7 +1081,6 @@ macro_rules! impl_benchmark_test {
|
||||
///
|
||||
/// - It must be the name of a method applied to the output of the `new_test_ext` argument.
|
||||
/// - That method must have a signature capable of receiving a single argument of the form `impl FnOnce()`.
|
||||
///
|
||||
// ## Notes (not for rustdoc)
|
||||
//
|
||||
// The biggest challenge for this macro is communicating the actual test functions to be run. We
|
||||
@@ -1260,9 +1259,9 @@ pub fn show_benchmark_debug_info(
|
||||
* Verify: {:?}\n\
|
||||
* Error message: {}",
|
||||
sp_std::str::from_utf8(instance_string)
|
||||
.expect("it's all just strings ran through the wasm interface. qed"),
|
||||
.expect("it's all just strings ran through the wasm interface. qed"),
|
||||
sp_std::str::from_utf8(benchmark)
|
||||
.expect("it's all just strings ran through the wasm interface. qed"),
|
||||
.expect("it's all just strings ran through the wasm interface. qed"),
|
||||
lowest_range_values,
|
||||
highest_range_values,
|
||||
steps,
|
||||
|
||||
@@ -20,9 +20,13 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use super::*;
|
||||
use sp_std::prelude::*;
|
||||
use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::{H256, Header}, BuildStorage};
|
||||
use frame_support::parameter_types;
|
||||
use sp_runtime::{
|
||||
testing::{Header, H256},
|
||||
traits::{BlakeTwo256, IdentityLookup},
|
||||
BuildStorage,
|
||||
};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
mod pallet_test {
|
||||
use frame_support::pallet_prelude::Get;
|
||||
@@ -59,7 +63,8 @@ mod pallet_test {
|
||||
}
|
||||
|
||||
pub trait Config: frame_system::Config + OtherConfig
|
||||
where Self::OtherEvent: Into<<Self as Config>::Event>
|
||||
where
|
||||
Self::OtherEvent: Into<<Self as Config>::Event>,
|
||||
{
|
||||
type Event;
|
||||
type LowerBound: Get<u32>;
|
||||
@@ -107,7 +112,7 @@ impl frame_system::Config for Test {
|
||||
type OnSetCode = ();
|
||||
}
|
||||
|
||||
parameter_types!{
|
||||
parameter_types! {
|
||||
pub const LowerBound: u32 = 1;
|
||||
pub const UpperBound: u32 = 100;
|
||||
}
|
||||
@@ -127,16 +132,20 @@ fn new_test_ext() -> sp_io::TestExternalities {
|
||||
}
|
||||
|
||||
mod benchmarks {
|
||||
use sp_std::prelude::*;
|
||||
use super::{
|
||||
new_test_ext,
|
||||
pallet_test::{self, Value},
|
||||
Test,
|
||||
};
|
||||
use crate::{account, BenchmarkParameter, BenchmarkingSetup};
|
||||
use frame_support::{assert_err, assert_ok, ensure, traits::Get, StorageValue};
|
||||
use frame_system::RawOrigin;
|
||||
use super::{Test, pallet_test::{self, Value}, new_test_ext};
|
||||
use frame_support::{assert_ok, assert_err, ensure, traits::Get, StorageValue};
|
||||
use crate::{BenchmarkingSetup, BenchmarkParameter, account};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
// Additional used internally by the benchmark macro.
|
||||
use super::pallet_test::{Call, Config, Pallet};
|
||||
|
||||
crate::benchmarks!{
|
||||
crate::benchmarks! {
|
||||
where_clause {
|
||||
where
|
||||
<T as pallet_test::OtherConfig>::OtherEvent: Into<<T as pallet_test::Config>::Event> + Clone,
|
||||
@@ -204,7 +213,8 @@ mod benchmarks {
|
||||
&selected,
|
||||
&[(BenchmarkParameter::b, 1)],
|
||||
true,
|
||||
).expect("failed to create closure");
|
||||
)
|
||||
.expect("failed to create closure");
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(closure());
|
||||
@@ -222,7 +232,8 @@ mod benchmarks {
|
||||
&selected,
|
||||
&[(BenchmarkParameter::b, 1)],
|
||||
true,
|
||||
).expect("failed to create closure");
|
||||
)
|
||||
.expect("failed to create closure");
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(closure());
|
||||
@@ -240,7 +251,8 @@ mod benchmarks {
|
||||
&selected,
|
||||
&[(BenchmarkParameter::x, 1)],
|
||||
true,
|
||||
).expect("failed to create closure");
|
||||
)
|
||||
.expect("failed to create closure");
|
||||
|
||||
assert_ok!(closure());
|
||||
}
|
||||
@@ -254,7 +266,8 @@ mod benchmarks {
|
||||
&selected,
|
||||
&[(BenchmarkParameter::b, 1)],
|
||||
true,
|
||||
).expect("failed to create closure");
|
||||
)
|
||||
.expect("failed to create closure");
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(closure());
|
||||
@@ -267,7 +280,8 @@ mod benchmarks {
|
||||
&selected,
|
||||
&[(BenchmarkParameter::x, 10000)],
|
||||
true,
|
||||
).expect("failed to create closure");
|
||||
)
|
||||
.expect("failed to create closure");
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_err!(closure(), "You forgot to sort!");
|
||||
|
||||
@@ -17,18 +17,43 @@
|
||||
|
||||
//! Interfaces, types and utils for benchmarking a FRAME runtime.
|
||||
|
||||
use codec::{Encode, Decode};
|
||||
use sp_std::{vec::Vec, prelude::Box};
|
||||
use sp_io::hashing::blake2_256;
|
||||
use sp_storage::TrackedStorageKey;
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::traits::StorageInfo;
|
||||
use sp_io::hashing::blake2_256;
|
||||
use sp_std::{prelude::Box, vec::Vec};
|
||||
use sp_storage::TrackedStorageKey;
|
||||
|
||||
/// An alphabet of possible parameters to use for benchmarking.
|
||||
#[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,
|
||||
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")]
|
||||
@@ -105,7 +130,8 @@ pub trait Benchmarking {
|
||||
/// WARNING! This is a non-deterministic call. Do not use this within
|
||||
/// consensus critical logic.
|
||||
fn current_time() -> u128 {
|
||||
std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH)
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::SystemTime::UNIX_EPOCH)
|
||||
.expect("Unix time doesn't go backwards; qed")
|
||||
.as_nanos()
|
||||
}
|
||||
@@ -153,7 +179,7 @@ pub trait Benchmarking {
|
||||
// If the key does not exist, add it.
|
||||
None => {
|
||||
whitelist.push(add);
|
||||
}
|
||||
},
|
||||
}
|
||||
self.set_whitelist(whitelist);
|
||||
}
|
||||
@@ -217,12 +243,16 @@ pub trait BenchmarkingSetup<T, I = ()> {
|
||||
fn instance(
|
||||
&self,
|
||||
components: &[(BenchmarkParameter, u32)],
|
||||
verify: bool
|
||||
verify: bool,
|
||||
) -> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>;
|
||||
}
|
||||
|
||||
/// Grab an account, seeded by a name and index.
|
||||
pub fn account<AccountId: Decode + Default>(name: &'static str, index: u32, seed: u32) -> AccountId {
|
||||
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()
|
||||
}
|
||||
@@ -236,7 +266,7 @@ pub fn whitelisted_caller<AccountId: Decode + Default>() -> AccountId {
|
||||
macro_rules! whitelist_account {
|
||||
($acc:ident) => {
|
||||
frame_benchmarking::benchmarking::add_to_whitelist(
|
||||
frame_system::Account::<T>::hashed_key_for(&$acc).into()
|
||||
frame_system::Account::<T>::hashed_key_for(&$acc).into(),
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user