referrerpolicy=no-referrer-when-downgrade

frame_benchmarking_cli/
lib.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
18//! Contains the root [`BenchmarkCmd`] command and exports its sub-commands.
19
20mod block;
21mod extrinsic;
22mod machine;
23mod overhead;
24mod pallet;
25mod shared;
26mod storage;
27
28pub 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;
38
39use sc_cli::{CliConfiguration, DatabaseParams, ImportParams, PruningParams, Result, SharedParams};
40
41/// 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}
53
54/// Unwraps a [`BenchmarkCmd`] into its concrete sub-command.
55macro_rules! unwrap_cmd {
56	{
57		$self:expr,
58		$cmd:ident,
59		$code:expr
60	} => {
61		match $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}
71
72/// 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 {
76	fn shared_params(&self) -> &SharedParams {
77		unwrap_cmd! {
78			self, cmd, cmd.shared_params()
79		}
80	}
81
82	fn import_params(&self) -> Option<&ImportParams> {
83		unwrap_cmd! {
84			self, cmd, cmd.import_params()
85		}
86	}
87
88	fn database_params(&self) -> Option<&DatabaseParams> {
89		unwrap_cmd! {
90			self, cmd, cmd.database_params()
91		}
92	}
93
94	fn base_path(&self) -> Result<Option<BasePath>> {
95		let inner = unwrap_cmd! {
96			self, cmd, cmd.base_path()
97		};
98
99		// 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.
101		match inner {
102			Ok(None) => Some(BasePath::new_temp_dir()).transpose().map_err(|e| e.into()),
103			e => e,
104		}
105	}
106
107	fn pruning_params(&self) -> Option<&PruningParams> {
108		unwrap_cmd! {
109			self, cmd, cmd.pruning_params()
110		}
111	}
112
113	fn trie_cache_maximum_size(&self) -> Result<Option<usize>> {
114		unwrap_cmd! {
115			self, cmd, cmd.trie_cache_maximum_size()
116		}
117	}
118
119	fn chain_id(&self, is_dev: bool) -> Result<String> {
120		unwrap_cmd! {
121			self, cmd, cmd.chain_id(is_dev)
122		}
123	}
124}