Skip to content

Commit 767e9ab

Browse files
bearhackfisher
bear
andauthored
Add benchmark framework (#700)
* Add benchmark for crab * Add benchmark for darwinia * Test works Co-authored-by: HackFisher <[email protected]>
1 parent 55a5491 commit 767e9ab

File tree

13 files changed

+227
-3
lines changed

13 files changed

+227
-3
lines changed

Cargo.lock

+107-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ dev = [
3333

3434
try-runtime = ["darwinia-cli/try-runtime"]
3535

36+
runtime-benchmarks = ["darwinia-cli/runtime-benchmarks"]
37+
3638
[workspace]
3739
members = [
3840
"cli",

cli/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ sc-network = { optional = true, git = "https://github.com/darwinia-network/
3333
sc-service = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
3434
sc-tracing = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
3535
try-runtime-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
36+
# benchmark
37+
frame-benchmarking-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
3638
# substrate primitives
3739
sp-core = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
3840
# this crate is used only to enable `trie-memory-tracker` feature
@@ -71,3 +73,8 @@ try-runtime = [
7173
"darwinia-service/try-runtime",
7274
"try-runtime-cli",
7375
]
76+
77+
runtime-benchmarks = [
78+
"frame-benchmarking-cli",
79+
"darwinia-service/runtime-benchmarks",
80+
]

cli/src/cli.rs

+5
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ pub enum Subcommand {
112112
/// testing.
113113
#[cfg(feature = "try-runtime")]
114114
TryRuntime(try_runtime_cli::TryRuntimeCmd),
115+
116+
/// The custom benchmark subcommmand benchmarking runtime pallets.
117+
#[cfg(feature = "runtime-benchmarks")]
118+
#[structopt(name = "benchmark", about = "Benchmark runtime pallets.")]
119+
Benchmark(frame_benchmarking_cli::BenchmarkCmd),
115120
}
116121

117122
#[derive(Debug, StructOpt)]

cli/src/command.rs

+16
Original file line numberDiff line numberDiff line change
@@ -379,5 +379,21 @@ pub fn run() -> sc_cli::Result<()> {
379379
})
380380
}
381381
}
382+
#[cfg(feature = "runtime-benchmarks")]
383+
Some(Subcommand::Benchmark(cmd)) => {
384+
let runner = cli.create_runner(cmd)?;
385+
let chain_spec = &runner.config().chain_spec;
386+
387+
if chain_spec.is_crab() {
388+
runner.sync_run(|config| cmd.run::<crab_runtime::Block, CrabExecutor>(config))
389+
} else if chain_spec.is_darwinia() {
390+
runner
391+
.sync_run(|config| cmd.run::<darwinia_runtime::Block, DarwiniaExecutor>(config))
392+
} else {
393+
Err("Benchmarking wasn't enabled when building the node. \
394+
You can enable it with `--features runtime-benchmarks`."
395+
.into())
396+
}
397+
}
382398
}
383399
}

file_header.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This file is part of Darwinia.
2+
//
3+
// Copyright (C) 2018-2021 Darwinia Network
4+
// SPDX-License-Identifier: GPL-3.0
5+
//
6+
// Darwinia is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
//
11+
// Darwinia is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU General Public License
17+
// along with Darwinia. If not, see <https://www.gnu.org/licenses/>.

node/service/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ sc-service = { git = "https://github.com/darwinia-network/substrate.
5151
sc-telemetry = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
5252
sc-transaction-pool = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
5353
# substrate frame
54+
frame-benchmarking = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
5455
frame-system-rpc-runtime-api = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
5556
pallet-im-online = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
5657
pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
@@ -92,3 +93,8 @@ try-runtime = [
9293
"crab-runtime/try-runtime",
9394
"darwinia-runtime/try-runtime",
9495
]
96+
97+
runtime-benchmarks = [
98+
"crab-runtime/runtime-benchmarks",
99+
"darwinia-runtime/runtime-benchmarks",
100+
]

node/service/src/service/crab.rs

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ native_executor_instance!(
7272
pub CrabExecutor,
7373
crab_runtime::api::dispatch,
7474
crab_runtime::native_version,
75+
frame_benchmarking::benchmarking::HostFunctions,
7576
);
7677

7778
impl_runtime_apis!(dvm_rpc_runtime_api::EthereumRuntimeRPCApi<Block>);

node/service/src/service/darwinia.rs

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ native_executor_instance!(
6363
pub DarwiniaExecutor,
6464
darwinia_runtime::api::dispatch,
6565
darwinia_runtime::native_version,
66+
frame_benchmarking::benchmarking::HostFunctions,
6667
);
6768

6869
impl_runtime_apis!();

runtime/crab/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ ethereum-primitives = { default-features = false, git = "https://github.com/darw
5151
# darwinia runtime
5252
darwinia-runtime-common = { default-features = false, path = "../common" }
5353
# substrate frame
54+
frame-benchmarking = { optional=true, default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
55+
frame-system-benchmarking = { optional=true, default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
5456
frame-executive = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
5557
frame-support = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
5658
frame-system = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
@@ -193,3 +195,12 @@ try-runtime = [
193195
on-chain-release-build = [
194196
"sp-api/disable-logging",
195197
]
198+
199+
runtime-benchmarks = [
200+
"frame-benchmarking",
201+
"frame-system-benchmarking",
202+
"frame-system/runtime-benchmarks",
203+
"frame-support/runtime-benchmarks",
204+
"pallet-collective/runtime-benchmarks",
205+
"pallet-society/runtime-benchmarks",
206+
]

runtime/crab/src/lib.rs

+22
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,28 @@ sp_api::impl_runtime_apis! {
644644
Ok((weight, RuntimeBlockWeights::get().max_block))
645645
}
646646
}
647+
648+
#[cfg(feature = "runtime-benchmarks")]
649+
impl frame_benchmarking::Benchmark<Block> for Runtime {
650+
fn dispatch_benchmark(
651+
config: frame_benchmarking::BenchmarkConfig
652+
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
653+
use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};
654+
use frame_system_benchmarking::Pallet as SystemBench;
655+
impl frame_system_benchmarking::Config for Runtime {}
656+
657+
let whitelist: Vec<TrackedStorageKey> = vec![];
658+
659+
let mut batches = Vec::<BenchmarkBatch>::new();
660+
let params = (&config, &whitelist);
661+
662+
663+
add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
664+
665+
if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
666+
Ok(batches)
667+
}
668+
}
647669
}
648670

649671
pub struct TransactionConverter;

runtime/darwinia/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ ethereum-primitives = { default-features = false, git = "https://github.com/darw
4242
# darwinia runtime
4343
darwinia-runtime-common = { default-features = false, path = "../common" }
4444
# substrate frame
45+
frame-benchmarking = { optional=true, default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
46+
frame-system-benchmarking = { optional=true, default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
4547
frame-executive = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
4648
frame-support = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
4749
frame-system = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" }
@@ -173,3 +175,12 @@ try-runtime = [
173175
on-chain-release-build = [
174176
"sp-api/disable-logging",
175177
]
178+
179+
runtime-benchmarks = [
180+
"frame-benchmarking",
181+
"frame-system-benchmarking",
182+
"frame-support/runtime-benchmarks",
183+
"frame-system/runtime-benchmarks",
184+
"pallet-collective/runtime-benchmarks",
185+
"pallet-society/runtime-benchmarks",
186+
]

0 commit comments

Comments
 (0)