Benchmark
Demonstrates how to use the benchmarking framework to estimate execution requirements for a pallet.
Last updated
Demonstrates how to use the benchmarking framework to estimate execution requirements for a pallet.
Last updated
This guide illustrates how to write a simple benchmark for a pallet, test the benchmark, and run 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.
Open the 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 of std
features for the pallet.
For example:
Create a new text file—for example, benchmarking.rs
—in the src
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:
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 the set_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.
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:
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 the runtime-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.
Add your pallet to the define_benchmark!
macro in the runtime-benchmarks
feature.
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:
You can use the benchmarking.rs
and weights.rs
files for any prebuilt pallet to learn more about benchmarking different types of functions.
The benchmarking module for provides a few simple sample benchmarks. For example:
If you need more details about adding a pallet to the runtime, see or .
This command creates a weights.rs
file in the specified directory. For information about how to configure your pallet to use those weights, see .