Benchmark
Demonstrates how to use the benchmarking framework to estimate execution requirements for a pallet.
This guide illustrates how to write a simple benchmark for a pallet, test the benchmark, and run benchmarking commands to generate realistic estimates about the execution time required for the functions in a pallet. This guide does not cover how to use the benchmarking results to update transaction weights.
Add benchmarking to the pallet
Open the
Cargo.toml
file for your pallet in a text editor.Add the
frame-benchmarking
crate to the [dependencies] for the pallet using the same version and branch as the other dependencies in the pallet.For example:
Add
runtime-benchmarks
to the list of [features] for the pallet.For example:
Add
frame-benchmarking/std
to the list ofstd
features for the pallet.For example:
Add a benchmarking module
Create a new text file—for example,
benchmarking.rs
—in thesrc
folder for your pallet.Open the
benchmarking.rs
file in a text editor and create a Rust module that defines benchmarks for your pallet.You can use the
benchmarking.rs
for any prebuilt pallet as an example of what to include in the Rust module. In general, the module should include code similar to the following:Write individual benchmarks to test the most computationally expensive paths for the functions in the pallet.
The benchmarking macro automatically generates a test function for each benchmark you include in the benchmarking module. For example, the macro creates test functions similar to the following:
The benchmarking module for pallet-example-basic provides a few simple sample benchmarks. For example:
In this sample code:
The name of the benchmark is
set_dummy_benchmark
.The variable
b
stores input that is used to test the execution time of theset_dummy
function.The value of
b
varies between 1 to 1,000, so you can run the benchmark test repeatedly to measure the execution time using different input values.
Test the benchmarks
After you have added benchmarks to the benchmarks!
macros in the benchmarking module for your pallet, you can use a mock runtime to do unit testing and ensure that the test functions for your benchmarks return Ok(())
as a result.
Open the
benchmarking.rs
benchmarking module in a text editor.Add the
impl_benchmark_test_suite!
macro to the bottom of your benchmarking module:The
impl_benchmark_test_suite!
macro takes the following input:The Pallet struct generated by your pallet, in this example
MyPallet
.A function that generates a test genesis storage,
new_text_ext()
.The full mock runtime struct,
Test
.
This is the same information you use to set up a mock runtime for unit testing. If all benchmark tests pass in the mock runtime test environment, it's likely that they will work when you run the benchmarks in the actual runtime.
Execute the benchmark unit tests generated for your pallet in a mock runtime by running a command similar to the following for a pallet named
pallet-mycustom
:Verify the test results.
For example:
Add benchmarking to the runtime
After you have added benchmarking to your pallet, you must also update the runtime to include the pallet and the benchmarks for the pallet.
Open the
Cargo.toml
file for your runtime in a text editor.Add your pallet to the list of
[dependencies]
for the runtime:Update the
[features]
for the runtime to include theruntime-benchmarks
for your pallet:Update the
std
features for the runtime to include your pallet:Add the configuration trait for your pallet to the runtime.
Add the pallet the the
construct_runtime!
macro.If you need more details about adding a pallet to the runtime, see Add a pallet to the runtime or Import a pallet.
Add your pallet to the
define_benchmark!
macro in theruntime-benchmarks
feature.
Run your benchmarks
After you update the runtime, you are ready to compile it with the runtime-benchmarks
features enabled and start the benchmarking analysis for your pallet.
Build your project with the
runtime-benchmarks
feature enabled by running the following command:Review the command-line options for the node
benchmark pallet
subcommand:The
benchmark pallet
subcommand supports several command-line options that can help you automate your benchmarking. For example, you can set the--steps
and--repeat
command-line options to execute function calls multiple times with different values.Start benchmarking for your pallet by running a command similar to the following:
This command creates a
weights.rs
file in the specified directory. For information about how to configure your pallet to use those weights, see Use custom weights.
Examples
You can use the benchmarking.rs
and weights.rs
files for any prebuilt pallet to learn more about benchmarking different types of functions.
Last updated