From fda776951df2546b4e27fa245cfc10033b728b47 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Tue, 13 Jun 2023 18:51:18 +0200 Subject: [PATCH] benchmark pallet: Error if files are overwritten twice (#14352) * benchmark pallet: Error if files are overwritten twice Signed-off-by: Oliver Tale-Yazdi * Fix Signed-off-by: Oliver Tale-Yazdi * Fix error message Signed-off-by: Oliver Tale-Yazdi --------- Signed-off-by: Oliver Tale-Yazdi --- .../frame/benchmarking-cli/src/pallet/mod.rs | 6 +++++ .../benchmarking-cli/src/pallet/writer.rs | 23 ++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/substrate/utils/frame/benchmarking-cli/src/pallet/mod.rs b/substrate/utils/frame/benchmarking-cli/src/pallet/mod.rs index f214569051..b47cb156ec 100644 --- a/substrate/utils/frame/benchmarking-cli/src/pallet/mod.rs +++ b/substrate/utils/frame/benchmarking-cli/src/pallet/mod.rs @@ -195,4 +195,10 @@ pub struct PalletCmd { /// the analysis is read from this file. #[arg(long)] pub json_input: Option, + + /// Allow overwriting a single file with multiple results. + /// + /// This exists only to restore legacy behaviour. It should never actually be needed. + #[arg(long)] + pub unsafe_overwrite_results: bool, } diff --git a/substrate/utils/frame/benchmarking-cli/src/pallet/writer.rs b/substrate/utils/frame/benchmarking-cli/src/pallet/writer.rs index 89cce45627..c8da658ea8 100644 --- a/substrate/utils/frame/benchmarking-cli/src/pallet/writer.rs +++ b/substrate/utils/frame/benchmarking-cli/src/pallet/writer.rs @@ -375,7 +375,7 @@ fn get_benchmark_data( } } -// Create weight file from benchmark data and Handlebars template. +/// Create weight file from benchmark data and Handlebars template. pub(crate) fn write_results( batches: &[BenchmarkBatchSplitResults], storage_info: &[StorageInfo], @@ -384,7 +384,7 @@ pub(crate) fn write_results( default_pov_mode: PovEstimationMode, path: &PathBuf, cmd: &PalletCmd, -) -> Result<(), std::io::Error> { +) -> Result<(), sc_cli::Error> { // Use custom template if provided. let template: String = match &cmd.template { Some(template_file) => fs::read_to_string(template_file)?, @@ -492,10 +492,21 @@ pub(crate) fn write_results( created_files.push(file_path); } - for file in created_files.iter().duplicates() { - // This can happen when there are multiple instances of a pallet deployed - // and `--output` forces the output of all instances into the same file. - println!("Multiple benchmarks were written to the same file: {:?}.", file); + let overwritten_files = created_files.iter().duplicates().collect::>(); + if !overwritten_files.is_empty() { + let msg = format!( + "Multiple results were written to the same file. This can happen when \ + there are multiple instances of a pallet deployed and `--output` forces the output of all \ + instances into the same file. Use `--unsafe-overwrite-results` to ignore this error. The \ + affected files are: {:?}", + overwritten_files + ); + + if cmd.unsafe_overwrite_results { + println!("{msg}"); + } else { + return Err(msg.into()) + } } Ok(()) }