improve benchmarking error output (#7863)

* add concat Vec<u8> function and use it for better error logging in add_benchmark! macro

* refactor benchmark error reporting to use format! and RuntimeString
This commit is contained in:
ropottnik
2021-01-15 13:38:21 +01:00
committed by GitHub
parent cf37f44c38
commit 81efcc4e52
4 changed files with 75 additions and 5 deletions
+57 -2
View File
@@ -955,6 +955,39 @@ macro_rules! impl_benchmark_test {
};
}
/// show error message and debugging info for the case of an error happening
/// during a benchmark
pub fn show_benchmark_debug_info(
instance_string: &[u8],
benchmark: &[u8],
lowest_range_values: &sp_std::prelude::Vec<u32>,
highest_range_values: &sp_std::prelude::Vec<u32>,
steps: &sp_std::prelude::Vec<u32>,
repeat: &u32,
verify: &bool,
error_message: &str,
) -> sp_runtime::RuntimeString {
sp_runtime::format_runtime_string!(
"\n* Pallet: {}\n\
* Benchmark: {}\n\
* Lowest_range_values: {:?}\n\
* Highest_range_values: {:?}\n\
* Steps: {:?}\n\
* Repeat: {:?}\n\
* Verify: {:?}\n\
* Error message: {}",
sp_std::str::from_utf8(instance_string)
.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"),
lowest_range_values,
highest_range_values,
steps,
repeat,
verify,
error_message,
)
}
/// This macro adds pallet benchmarks to a `Vec<BenchmarkBatch>` object.
///
@@ -1050,7 +1083,18 @@ macro_rules! add_benchmark {
*repeat,
whitelist,
*verify,
)?,
).map_err(|e| {
$crate::show_benchmark_debug_info(
instance_string,
benchmark,
lowest_range_values,
highest_range_values,
steps,
repeat,
verify,
e,
)
})?,
});
}
} else {
@@ -1066,7 +1110,18 @@ macro_rules! add_benchmark {
*repeat,
whitelist,
*verify,
)?,
).map_err(|e| {
$crate::show_benchmark_debug_info(
instance_string,
benchmark,
lowest_range_values,
highest_range_values,
steps,
repeat,
verify,
e,
)
})?,
});
}
}
+1 -2
View File
@@ -20,7 +20,6 @@
use codec::{Encode, Decode};
use sp_std::{vec::Vec, prelude::Box};
use sp_io::hashing::blake2_256;
use sp_runtime::RuntimeString;
use sp_storage::TrackedStorageKey;
/// An alphabet of possible parameters to use for benchmarking.
@@ -90,7 +89,7 @@ sp_api::decl_runtime_apis! {
/// Runtime api for benchmarking a FRAME runtime.
pub trait Benchmark {
/// Dispatch the given benchmark.
fn dispatch_benchmark(config: BenchmarkConfig) -> Result<Vec<BenchmarkBatch>, RuntimeString>;
fn dispatch_benchmark(config: BenchmarkConfig) -> Result<Vec<BenchmarkBatch>, sp_runtime::RuntimeString>;
}
}
@@ -32,6 +32,22 @@ pub enum RuntimeString {
Owned(Vec<u8>),
}
/// Convenience macro to use the format! interface to get a `RuntimeString::Owned`
#[macro_export]
macro_rules! format_runtime_string {
($($args:tt)*) => {{
#[cfg(feature = "std")]
{
sp_runtime::RuntimeString::Owned(format!($($args)*))
}
#[cfg(not(feature = "std"))]
{
sp_runtime::RuntimeString::Owned(sp_std::alloc::format!($($args)*).as_bytes().to_vec())
}
}};
}
impl From<&'static str> for RuntimeString {
fn from(data: &'static str) -> Self {
Self::Borrowed(data)
@@ -174,7 +174,7 @@ impl BenchmarkCmd {
}
}
},
Err(error) => eprintln!("Error: {:?}", error),
Err(error) => eprintln!("Error: {}", error),
}
Ok(())