frame_omni_bencher/command.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use clap::Parser;
19use frame_benchmarking_cli::{BenchmarkCmd, OpaqueBlock};
20use sc_cli::Result;
21use sp_runtime::traits::BlakeTwo256;
22
23/// # Polkadot Omni Benchmarking CLI
24///
25/// The Polkadot Omni benchmarker allows to benchmark the extrinsics of any Polkadot runtime. It is
26/// meant to replace the current manual integration of the `benchmark pallet` into every parachain
27/// node. This reduces duplicate code and makes maintenance for builders easier. The CLI is
28/// currently only able to benchmark extrinsics. In the future it is planned to extend this to some
29/// other areas.
30///
31/// General FRAME runtimes could also be used with this benchmarker, as long as they don't utilize
32/// any host functions that are not part of the Polkadot host specification.
33///
34/// ## Installation
35///
36/// Directly via crates.io:
37///
38/// ```sh
39/// cargo install frame-omni-bencher --profile=production
40/// ```
41///
42/// from GitHub:
43///
44/// ```sh
45/// cargo install --git https://github.com/paritytech/polkadot-sdk frame-omni-bencher --profile=production
46/// ```
47///
48/// or locally from the sources:
49///
50/// ```sh
51/// cargo install --path substrate/utils/frame/omni-bencher --profile=production
52/// ```
53///
54/// Check the installed version and print the docs:
55///
56/// ```sh
57/// frame-omni-bencher --help
58/// ```
59///
60/// ## Usage
61///
62/// First we need to ensure that there is a runtime available. As example we will build the Westend
63/// runtime:
64///
65/// ```sh
66/// cargo build -p westend-runtime --profile production --features runtime-benchmarks
67/// ```
68///
69/// Now as an example, we benchmark the `balances` pallet:
70///
71/// ```sh
72/// frame-omni-bencher v1 benchmark pallet \
73/// --runtime target/release/wbuild/westend-runtime/westend-runtime.compact.compressed.wasm \
74/// --pallet "pallet_balances" --extrinsic ""
75/// ```
76///
77/// For the exact arguments of the `pallet` command, please refer to the `pallet` sub-module.
78///
79/// ## Backwards Compatibility
80///
81/// The exposed pallet sub-command is identical as the node-integrated CLI. The only difference is
82/// that it needs to be prefixed with a `v1` to ensure drop-in compatibility.
83#[derive(Parser, Debug)]
84#[clap(author, version, about, verbatim_doc_comment)]
85pub struct Command {
86 #[command(subcommand)]
87 sub: SubCommand,
88}
89
90/// Root-level subcommands.
91#[derive(Debug, clap::Subcommand)]
92pub enum SubCommand {
93 /// Compatibility syntax with the old benchmark runner.
94 V1(V1Command),
95 // NOTE: Here we can add new commands in a forward-compatible way. For example when
96 // transforming the CLI from a monolithic design to a data driven pipeline, there could be
97 // commands like `measure`, `analyze` and `render`.
98}
99
100/// A command that conforms to the legacy `benchmark` argument syntax.
101#[derive(Parser, Debug)]
102pub struct V1Command {
103 #[command(subcommand)]
104 sub: V1SubCommand,
105}
106
107/// The `v1 benchmark` subcommand.
108#[derive(Debug, clap::Subcommand)]
109pub enum V1SubCommand {
110 Benchmark(V1BenchmarkCommand),
111}
112
113/// Subcommands for `v1 benchmark`.
114#[derive(Parser, Debug)]
115pub struct V1BenchmarkCommand {
116 #[command(subcommand)]
117 sub: BenchmarkCmd,
118}
119
120type HostFunctions = (
121 sp_statement_store::runtime_api::HostFunctions,
122 cumulus_primitives_proof_size_hostfunction::storage_proof_size::HostFunctions,
123);
124
125impl Command {
126 pub fn run(self) -> Result<()> {
127 match self.sub {
128 SubCommand::V1(V1Command { sub }) => sub.run(),
129 }
130 }
131}
132impl V1SubCommand {
133 pub fn run(self) -> Result<()> {
134 match self {
135 V1SubCommand::Benchmark(V1BenchmarkCommand { sub }) => match sub {
136 BenchmarkCmd::Pallet(pallet) => {
137 pallet.run_with_spec::<BlakeTwo256, HostFunctions>(None)
138 },
139 BenchmarkCmd::Overhead(overhead_cmd) =>
140 overhead_cmd.run_with_default_builder_and_spec::<OpaqueBlock, HostFunctions>(None),
141 _ =>
142 return Err(
143 "Only the `v1 benchmark pallet` and `v1 benchmark overhead` command is currently supported".into()
144 ),
145 },
146 }
147 }
148}