Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Propagate DispatchError for benchmarks. #5075

Merged
merged 5 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,14 +821,14 @@ impl_runtime_apis! {
extrinsic: Vec<u8>,
steps: Vec<u32>,
repeat: u32,
) -> Option<Vec<frame_benchmarking::BenchmarkResults>> {
) -> Result<Vec<frame_benchmarking::BenchmarkResults>, Vec<u8>> {
use frame_benchmarking::Benchmarking;

match module.as_slice() {
b"pallet-balances" | b"balances" => Balances::run_benchmark(extrinsic, steps, repeat).ok(),
b"pallet-identity" | b"identity" => Identity::run_benchmark(extrinsic, steps, repeat).ok(),
b"pallet-timestamp" | b"timestamp" => Timestamp::run_benchmark(extrinsic, steps, repeat).ok(),
_ => None,
b"pallet-balances" | b"balances" => Balances::run_benchmark(extrinsic, steps, repeat),
b"pallet-identity" | b"identity" => Identity::run_benchmark(extrinsic, steps, repeat),
b"pallet-timestamp" | b"timestamp" => Timestamp::run_benchmark(extrinsic, steps, repeat),
_ => Err("Benchmark not found for this pallet.".as_bytes().to_vec()),
}
marcio-diaz marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
6 changes: 3 additions & 3 deletions frame/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ macro_rules! impl_benchmark {
$( $name:ident ),*
) => {
impl<T: Trait> $crate::Benchmarking<$crate::BenchmarkResults> for Module<T> {
fn run_benchmark(extrinsic: Vec<u8>, steps: Vec<u32>, repeat: u32) -> Result<Vec<$crate::BenchmarkResults>, &'static str> {
fn run_benchmark(extrinsic: Vec<u8>, steps: Vec<u32>, repeat: u32) -> Result<Vec<$crate::BenchmarkResults>, Vec<u8>> {
// Map the input to the selected benchmark.
let extrinsic = sp_std::str::from_utf8(extrinsic.as_slice())
.map_err(|_| "Could not find extrinsic")?;
.map_err(|_| "Could not find extrinsic".as_bytes().to_vec())?;
let selected_benchmark = match extrinsic {
$( stringify!($name) => SelectedBenchmark::$name, )*
_ => return Err("Could not find extrinsic."),
_ => return Err("Could not find extrinsic.".as_bytes().to_vec()),
};

// Warm up the DB
Expand Down
4 changes: 2 additions & 2 deletions frame/benchmarking/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ sp_api::decl_runtime_apis! {
extrinsic: Vec<u8>,
steps: Vec<u32>,
repeat: u32,
) -> Option<Vec<BenchmarkResults>>;
) -> Result<Vec<BenchmarkResults>, Vec<u8>>;
}
}

Expand Down Expand Up @@ -78,7 +78,7 @@ pub trait Benchmarking<T> {
/// - `extrinsic`: The name of extrinsic function you want to benchmark encoded as bytes.
/// - `steps`: The number of sample points you want to take across the range of parameters.
/// - `repeat`: The number of times you want to repeat a benchmark.
fn run_benchmark(extrinsic: Vec<u8>, steps: Vec<u32>, repeat: u32) -> Result<Vec<T>, &'static str>;
fn run_benchmark(extrinsic: Vec<u8>, steps: Vec<u32>, repeat: u32) -> Result<Vec<T>, Vec<u8>>;
}

/// The required setup for creating a benchmark.
Expand Down
7 changes: 7 additions & 0 deletions primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,13 @@ impl From<DispatchError> for &'static str {
}
}

impl From<DispatchError> for Vec<u8> {
marcio-diaz marked this conversation as resolved.
Show resolved Hide resolved
fn from(err: DispatchError) -> Vec<u8> {
let s: &'static str = err.into();
s.as_bytes().to_vec()
}
marcio-diaz marked this conversation as resolved.
Show resolved Hide resolved
}

impl traits::Printable for DispatchError {
fn print(&self) {
"DispatchError".print();
Expand Down
59 changes: 31 additions & 28 deletions utils/frame/benchmarking-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,34 +109,37 @@ impl BenchmarkCmd {
)
.execute(strategy.into())
.map_err(|e| format!("Error executing runtime benchmark: {:?}", e))?;
let results = <Option<Vec<BenchmarkResults>> as Decode>::decode(&mut &result[..])
.unwrap_or(None);

if let Some(results) = results {
// Print benchmark metadata
println!(
"Pallet: {:?}, Extrinsic: {:?}, Steps: {:?}, Repeat: {:?}",
self.pallet,
self.extrinsic,
self.steps,
self.repeat,
);

// Print the table header
results[0].0.iter().for_each(|param| print!("{:?},", param.0));

print!("extrinsic_time,storage_root_time\n");
// Print the values
results.iter().for_each(|result| {
let parameters = &result.0;
parameters.iter().for_each(|param| print!("{:?},", param.1));
// Print extrinsic time and storage root time
print!("{:?},{:?}\n", result.1, result.2);
});

eprintln!("Done.");
} else {
eprintln!("No Results.");
let results = <Result<Vec<BenchmarkResults>, Vec<u8>> as Decode>::decode(&mut &result[..])
marcio-diaz marked this conversation as resolved.
Show resolved Hide resolved
.unwrap_or(Err(b"failed to decode results".to_vec()));

match results {
Ok(results) => {
// Print benchmark metadata
println!(
"Pallet: {:?}, Extrinsic: {:?}, Steps: {:?}, Repeat: {:?}",
self.pallet,
self.extrinsic,
self.steps,
self.repeat,
);

// Print the table header
results[0].0.iter().for_each(|param| print!("{:?},", param.0));

print!("extrinsic_time,storage_root_time\n");
// Print the values
results.iter().for_each(|result| {
let parameters = &result.0;
parameters.iter().for_each(|param| print!("{:?},", param.1));
// Print extrinsic time and storage root time
print!("{:?},{:?}\n", result.1, result.2);
});

eprintln!("Done.");
}
Err(err) => {
eprintln!("Error: {:?}", std::str::from_utf8(&err).unwrap());
}
}

Ok(())
Expand Down