[FRAME] Runtime Omni Bencher (#3512)

This MR contains two major changes and some maintenance cleanup.  

## 1. Free Standing Pallet Benchmark Runner

Closes https://github.com/paritytech/polkadot-sdk/issues/3045, depends
on your runtime exposing the `GenesisBuilderApi` (like
https://github.com/paritytech/polkadot-sdk/pull/1492).

Introduces a new binary crate: `frame-omni-bencher`.  
It allows to directly benchmark a WASM blob - without needing a node or
chain spec.

This makes it much easier to generate pallet weights and should allow us
to remove bloaty code from the node.
It should work for all FRAME runtimes that dont use 3rd party host calls
or non `BlakeTwo256` block hashing (basically all polkadot parachains
should work).

It is 100% backwards compatible with the old CLI args, when the `v1`
compatibility command is used. This is done to allow for forwards
compatible addition of new commands.

### Example (full example in the Rust docs)

Installing the CLI:
```sh
cargo install --locked --path substrate/utils/frame/omni-bencher
frame-omni-bencher --help
```

Building the Westend runtime:
```sh
cargo build -p westend-runtime --release --features runtime-benchmarks
```

Benchmarking the runtime:
```sh
frame-omni-bencher v1 benchmark pallet --runtime target/release/wbuild/westend-runtime/westend_runtime.compact.compressed.wasm --all
```

## 2. Building the Benchmark Genesis State in the Runtime

Closes https://github.com/paritytech/polkadot-sdk/issues/2664

This adds `--runtime` and `--genesis-builder=none|runtime|spec`
arguments to the `benchmark pallet` command to make it possible to
generate the genesis storage by the runtime. This can be used with both
the node and the freestanding benchmark runners. It utilizes the new
`GenesisBuilder` RA and depends on having
https://github.com/paritytech/polkadot-sdk/pull/3412 deployed.

## 3. Simpler args for `PalletCmd::run`

You can do three things here to integrate the changes into your node:
- nothing: old code keeps working as before but emits a deprecated
warning
- delete: remove the pallet benchmarking code from your node and use the
omni-bencher instead
- patch: apply the patch below and keep using as currently. This emits a
deprecated warning at runtime, since it uses the old way to generate a
genesis state, but is the smallest change.

```patch
runner.sync_run(|config| cmd
-    .run::<HashingFor<Block>, ReclaimHostFunctions>(config)
+    .run_with_spec::<HashingFor<Block>, ReclaimHostFunctions>(Some(config.chain_spec))
)
```

## 4. Maintenance Change
- `pallet-nis` get a `BenchmarkSetup` config item to prepare its
counterparty asset.
- Add percent progress print when running benchmarks.
- Dont immediately exit on benchmark error but try to run as many as
possible and print errors last.

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
This commit is contained in:
Oliver Tale-Yazdi
2024-04-08 18:03:56 +02:00
committed by GitHub
parent 216509dbaa
commit 9543d31474
33 changed files with 1064 additions and 307 deletions
@@ -1,13 +1,23 @@
# The Benchmarking CLI
# The FRAME Benchmarking CLI
This crate contains commands to benchmark various aspects of Substrate and the hardware.
All commands are exposed by the Substrate node but can be exposed by any Substrate client.
This crate contains commands to benchmark various aspects of Substrate and the hardware.
The goal is to have a comprehensive suite of benchmarks that cover all aspects of Substrate and the hardware that its
running on.
running on.
There exist fundamentally two ways to use this crate. A node-integrated CLI version, and a freestanding CLI. If you are
only interested in pallet benchmarking, then skip ahead to the [Freestanding CLI](#freestanding-cli).
# Node Integrated CLI
Mostly all Substrate nodes will expose some commands for benchmarking. You can refer to the `staging-node-cli` crate as
an example on how to integrate those. Note that for solely benchmarking pallets, the freestanding CLI is more suitable.
## Usage
Here we invoke the root command on the `staging-node-cli`. Most Substrate nodes should have a similar output, depending
on their integration of these commands.
Invoking the root benchmark command prints a help menu:
```sh
$ cargo run --profile=production -- benchmark
$ cargo run -p staging-node-cli --profile=production --features=runtime-benchmarks -- benchmark
Sub-commands concerned with benchmarking.
@@ -31,7 +41,46 @@ use `--release`.
For the final results the `production` profile and reference hardware should be used, otherwise the results are not
comparable.
The sub-commands are explained in depth here:
# Freestanding CLI
The freestanding is a standalone CLI that does not rely on any node integration. It can be used to benchmark pallets of
any FRAME runtime that does not utilize 3rd party host functions.
It currently only supports pallet benchmarking, since the other commands still rely on a node.
## Installation
Installing from local source repository:
```sh
cargo install --locked --path substrate/utils/frame/omni-bencher --profile=production
```
## Usage
The exposed pallet sub-command is identical as the node-integrated CLI. The only difference is that it needs to be prefixed
with a `v1` to ensure drop-in compatibility.
First we need to ensure that there is a runtime available. As example we will build the Westend runtime:
```sh
cargo build -p westend-runtime --profile production --features runtime-benchmarks
```
Now the benchmarking can be started with:
```sh
frame-omni-bencher v1 \
benchmark pallet \
--runtime target/release/wbuild/westend-runtime/westend-runtime.compact.compressed.wasm \
--pallet "pallet_balances" --extrinsic ""
```
For the exact arguments of the `pallet` command, please refer to the [pallet] sub-module.
# Commands
The sub-commands of both CLIs have the same semantics and are documented in their respective sub-modules:
- [block] Compare the weight of a historic block to its actual resource usage
- [machine] Gauges the speed of the hardware
- [overhead] Creates weight files for the *Block*- and *Extrinsic*-base weights