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

Commit

Permalink
benchmark pallet: Error if files are overwritten twice (#14352)
Browse files Browse the repository at this point in the history
* benchmark pallet: Error if files are overwritten twice

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix error message

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

---------

Signed-off-by: Oliver Tale-Yazdi <[email protected]>
  • Loading branch information
ggwpez authored Jun 13, 2023
1 parent 319a7bd commit 8e2fba0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
6 changes: 6 additions & 0 deletions utils/frame/benchmarking-cli/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,10 @@ pub struct PalletCmd {
/// the analysis is read from this file.
#[arg(long)]
pub json_input: Option<PathBuf>,

/// 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,
}
23 changes: 17 additions & 6 deletions utils/frame/benchmarking-cli/src/pallet/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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)?,
Expand Down Expand Up @@ -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::<Vec<_>>();
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(())
}
Expand Down

0 comments on commit 8e2fba0

Please sign in to comment.