Sub-commands for benchmark (#11164)

* Restructure benchmark commands

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Add benchmark block test

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fixup imports

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* CI

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Review fixes

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Extend error message

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Apply suggestions from code review

Co-authored-by: Zeke Mostov <z.mostov@gmail.com>

* Review fixes

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Add commands to node-template

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

Co-authored-by: Zeke Mostov <z.mostov@gmail.com>
This commit is contained in:
Oliver Tale-Yazdi
2022-04-07 21:33:11 +02:00
committed by GitHub
parent ef5c4b7fc3
commit a7261180ee
33 changed files with 690 additions and 342 deletions
+3 -20
View File
@@ -38,28 +38,11 @@ pub enum Subcommand {
)]
Inspect(node_inspect::cli::InspectCmd),
/// The custom benchmark subcommmand benchmarking runtime pallets.
#[clap(name = "benchmark", about = "Benchmark runtime pallets.")]
/// Sub-commands concerned with benchmarking.
/// The pallet benchmarking moved to the `pallet` sub-command.
#[clap(subcommand)]
Benchmark(frame_benchmarking_cli::BenchmarkCmd),
/// Benchmark the execution time of historic blocks and compare it to their consumed weight.
#[clap(
name = "benchmark-block",
about = "Benchmark the execution time of historic blocks and compare it to their consumed weight."
)]
BenchmarkBlock(frame_benchmarking_cli::BlockCmd),
/// Sub command for benchmarking the per-block and per-extrinsic execution overhead.
#[clap(
name = "benchmark-overhead",
about = "Benchmark the per-block and per-extrinsic execution overhead."
)]
BenchmarkOverhead(frame_benchmarking_cli::OverheadCmd),
/// Sub command for benchmarking the storage speed.
#[clap(name = "benchmark-storage", about = "Benchmark storage speed.")]
BenchmarkStorage(frame_benchmarking_cli::StorageCmd),
/// Try some command against runtime state.
#[cfg(feature = "try-runtime")]
TryRuntime(try_runtime_cli::TryRuntimeCmd),
+31 -35
View File
@@ -16,11 +16,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use super::command_helper::{inherent_benchmark_data, BenchmarkExtrinsicBuilder};
use crate::{
chain_spec, service,
service::{new_partial, FullClient},
Cli, Subcommand,
};
use frame_benchmarking_cli::*;
use node_executor::ExecutorDispatch;
use node_primitives::Block;
use node_runtime::RuntimeApi;
@@ -92,45 +94,39 @@ pub fn run() -> Result<()> {
runner.sync_run(|config| cmd.run::<Block, RuntimeApi, ExecutorDispatch>(config))
},
Some(Subcommand::Benchmark(cmd)) =>
if cfg!(feature = "runtime-benchmarks") {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|config| cmd.run::<Block, ExecutorDispatch>(config))
} else {
Err("Benchmarking wasn't enabled when building the node. \
You can enable it with `--features runtime-benchmarks`."
.into())
},
Some(Subcommand::BenchmarkBlock(cmd)) => {
Some(Subcommand::Benchmark(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, .. } = new_partial(&config)?;
Ok((cmd.run(client), task_manager))
})
},
Some(Subcommand::BenchmarkOverhead(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|mut config| {
use super::command_helper::{inherent_data, ExtrinsicBuilder};
// We don't use the authority role since that would start producing blocks
// in the background which would mess with our benchmark.
config.role = sc_service::Role::Full;
let PartialComponents { client, task_manager, .. } = new_partial(&config)?;
let ext_builder = ExtrinsicBuilder::new(client.clone());
runner.sync_run(|config| {
let PartialComponents { client, backend, .. } = new_partial(&config)?;
Ok((cmd.run(config, client, inherent_data()?, Arc::new(ext_builder)), task_manager))
})
},
Some(Subcommand::BenchmarkStorage(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let PartialComponents { client, task_manager, backend, .. } = new_partial(&config)?;
let db = backend.expose_db();
let storage = backend.expose_storage();
// This switch needs to be in the client, since the client decides
// which sub-commands it wants to support.
match cmd {
BenchmarkCmd::Pallet(cmd) => {
if !cfg!(feature = "runtime-benchmarks") {
return Err(
"Runtime benchmarking wasn't enabled when building the node. \
You can enable it with `--features runtime-benchmarks`."
.into(),
)
}
Ok((cmd.run(config, client, db, storage), task_manager))
cmd.run::<Block, ExecutorDispatch>(config)
},
BenchmarkCmd::Block(cmd) => cmd.run(client),
BenchmarkCmd::Storage(cmd) => {
let db = backend.expose_db();
let storage = backend.expose_storage();
cmd.run(config, client, db, storage)
},
BenchmarkCmd::Overhead(cmd) => {
let ext_builder = BenchmarkExtrinsicBuilder::new(client.clone());
cmd.run(config, client, inherent_benchmark_data()?, Arc::new(ext_builder))
},
}
})
},
Some(Subcommand::Key(cmd)) => cmd.run(&cli),
+6 -6
View File
@@ -29,19 +29,19 @@ use sp_runtime::OpaqueExtrinsic;
use std::{sync::Arc, time::Duration};
/// Generates extrinsics for the `benchmark-overhead` command.
pub struct ExtrinsicBuilder {
/// Generates extrinsics for the `benchmark overhead` command.
pub struct BenchmarkExtrinsicBuilder {
client: Arc<FullClient>,
}
impl ExtrinsicBuilder {
impl BenchmarkExtrinsicBuilder {
/// Creates a new [`Self`] from the given client.
pub fn new(client: Arc<FullClient>) -> Self {
Self { client }
}
}
impl frame_benchmarking_cli::ExtrinsicBuilder for ExtrinsicBuilder {
impl frame_benchmarking_cli::ExtrinsicBuilder for BenchmarkExtrinsicBuilder {
fn remark(&self, nonce: u32) -> std::result::Result<OpaqueExtrinsic, &'static str> {
let acc = Sr25519Keyring::Bob.pair();
let extrinsic: OpaqueExtrinsic = create_extrinsic(
@@ -56,8 +56,8 @@ impl frame_benchmarking_cli::ExtrinsicBuilder for ExtrinsicBuilder {
}
}
/// Generates inherent data for the `benchmark-overhead` command.
pub fn inherent_data() -> Result<InherentData> {
/// Generates inherent data for the `benchmark overhead` command.
pub fn inherent_benchmark_data() -> Result<InherentData> {
let mut inherent_data = InherentData::new();
let d = Duration::from_millis(0);
let timestamp = sp_timestamp::InherentDataProvider::new(d.into());
@@ -0,0 +1,48 @@
// This file is part of Substrate.
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Unix only since it uses signals from [`common::run_node_for_a_while`].
#![cfg(unix)]
use assert_cmd::cargo::cargo_bin;
use std::process::Command;
use tempfile::tempdir;
pub mod common;
/// `benchmark block` works for the dev runtime using the wasm executor.
#[tokio::test]
async fn benchmark_block_works() {
let base_dir = tempdir().expect("could not create a temp dir");
common::run_node_for_a_while(base_dir.path(), &["--dev"]).await;
// Invoke `benchmark block` with all options to make sure that they are valid.
let status = Command::new(cargo_bin("substrate"))
.args(["benchmark", "block", "--dev"])
.arg("-d")
.arg(base_dir.path())
.args(["--pruning", "archive"])
.args(["--from", "1", "--to", "1"])
.args(["--repeat", "1"])
.args(["--execution", "wasm", "--wasm-execution", "compiled"])
.status()
.unwrap();
assert!(status.success())
}
@@ -20,7 +20,7 @@ use assert_cmd::cargo::cargo_bin;
use std::process::Command;
use tempfile::tempdir;
/// Tests that the `benchmark-overhead` command works for the substrate dev runtime.
/// Tests that the `benchmark overhead` command works for the substrate dev runtime.
#[test]
fn benchmark_overhead_works() {
let tmp_dir = tempdir().expect("could not create a temp dir");
@@ -29,7 +29,7 @@ fn benchmark_overhead_works() {
// Only put 10 extrinsics into the block otherwise it takes forever to build it
// especially for a non-release build.
let status = Command::new(cargo_bin("substrate"))
.args(&["benchmark-overhead", "--dev", "-d"])
.args(&["benchmark", "overhead", "--dev", "-d"])
.arg(base_path)
.arg("--weight-path")
.arg(base_path)
@@ -23,7 +23,7 @@ use std::{
};
use tempfile::tempdir;
/// Tests that the `benchmark-storage` command works for the dev runtime.
/// Tests that the `benchmark storage` command works for the dev runtime.
#[test]
fn benchmark_storage_works() {
let tmp_dir = tempdir().expect("could not create a temp dir");
@@ -39,7 +39,7 @@ fn benchmark_storage_works() {
fn benchmark_storage(db: &str, base_path: &Path) -> ExitStatus {
Command::new(cargo_bin("substrate"))
.args(&["benchmark-storage", "--dev"])
.args(&["benchmark", "storage", "--dev"])
.arg("--db")
.arg(db)
.arg("--weight-path")