1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
56// 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.
1718//! Contains the root [`BenchmarkCmd`] command and exports its sub-commands.
1920mod block;
21mod extrinsic;
22mod machine;
23mod overhead;
24mod pallet;
25mod shared;
26mod storage;
2728pub use block::BlockCmd;
29pub use extrinsic::{ExtrinsicBuilder, ExtrinsicCmd, ExtrinsicFactory};
30pub use machine::{MachineCmd, SUBSTRATE_REFERENCE_HARDWARE};
31pub use overhead::{
32 remark_builder::{DynamicRemarkBuilder, SubstrateRemarkBuilder},
33 OpaqueBlock, OverheadCmd,
34};
35pub use pallet::PalletCmd;
36pub use sc_service::BasePath;
37pub use storage::StorageCmd;
3839use sc_cli::{CliConfiguration, DatabaseParams, ImportParams, PruningParams, Result, SharedParams};
4041/// The root `benchmarking` command.
42///
43/// Has no effect itself besides printing a help menu of the sub-commands.
44#[derive(Debug, clap::Subcommand)]
45pub enum BenchmarkCmd {
46 Pallet(PalletCmd),
47 Storage(StorageCmd),
48 Overhead(OverheadCmd),
49 Block(BlockCmd),
50 Machine(MachineCmd),
51 Extrinsic(ExtrinsicCmd),
52}
5354/// Unwraps a [`BenchmarkCmd`] into its concrete sub-command.
55macro_rules! unwrap_cmd {
56 {
57$self:expr,
58$cmd:ident,
59$code:expr
60 } => {
61match $self {
62 BenchmarkCmd::Pallet($cmd) => $code,
63 BenchmarkCmd::Storage($cmd) => $code,
64 BenchmarkCmd::Overhead($cmd) => $code,
65 BenchmarkCmd::Block($cmd) => $code,
66 BenchmarkCmd::Machine($cmd) => $code,
67 BenchmarkCmd::Extrinsic($cmd) => $code,
68 }
69 }
70}
7172/// Forward the [`CliConfiguration`] trait implementation.
73///
74/// Each time a sub-command exposes a new config option, it must be added here.
75impl CliConfiguration for BenchmarkCmd {
76fn shared_params(&self) -> &SharedParams {
77unwrap_cmd! {
78self, cmd, cmd.shared_params()
79 }
80 }
8182fn import_params(&self) -> Option<&ImportParams> {
83unwrap_cmd! {
84self, cmd, cmd.import_params()
85 }
86 }
8788fn database_params(&self) -> Option<&DatabaseParams> {
89unwrap_cmd! {
90self, cmd, cmd.database_params()
91 }
92 }
9394fn base_path(&self) -> Result<Option<BasePath>> {
95let inner = unwrap_cmd! {
96self, cmd, cmd.base_path()
97 };
9899// If the base path was not provided, benchmark command shall use temporary path. Otherwise
100 // we may end up using shared path, which may be inappropriate for benchmarking.
101match inner {
102Ok(None) => Some(BasePath::new_temp_dir()).transpose().map_err(|e| e.into()),
103 e => e,
104 }
105 }
106107fn pruning_params(&self) -> Option<&PruningParams> {
108unwrap_cmd! {
109self, cmd, cmd.pruning_params()
110 }
111 }
112113fn trie_cache_maximum_size(&self) -> Result<Option<usize>> {
114unwrap_cmd! {
115self, cmd, cmd.trie_cache_maximum_size()
116 }
117 }
118119fn chain_id(&self, is_dev: bool) -> Result<String> {
120unwrap_cmd! {
121self, cmd, cmd.chain_id(is_dev)
122 }
123 }
124}