or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Benchmarking and Weights for a New Pallet
Note these instructions are for a pallet authored into Substrate's
frame
path, and don't work for externally authored pallets yet.Write the initial
benchmarks.rs
Copy the following text into a new file
benchmarking.rs
, prepended with whatever licence you want:Introduce benchmarking code blocks
Inside the
benchmarks!
macro, introduce a block like this for each dispatchable:Introduce
let ... in ...
lines for each complexity factor your dispatchable has.Integrate benchmarks into your pallet
Ensure that your pallet is integrated into
node
Note this step may already be done as part of your usual pallet authoring process.
bin/node/cli/src/chain_spec.rs
includes your pallet's config if needed. Note: This is only needed if your pallet has aGenesisConfig
.bin/node/runtime/src/lib.rs
:Runtime
struct has animpl
for your pallet'sConfig
.construct_runtime!
macro has a line for your pallet.fn dispatch_benchmark
has aadd_benchmark!
macro invocation for your pallet.bin/node/runtime/Cargo.toml
:std
feature.runtime-benchmarks
feature. Don't forget this or you'll get weird build errors!Enable benchmarks in your pallet
lib.rs
, ensure you have apub mod benchmarking;
line at the top.Cargo.toml
, ensure you passruntime-benchmarks
feature down tosp-runtime
,frame-support
andframe-system
crates, and use it to enable theruntime-benchmarks
crate:Testing benchmarks
Just use
cargo test
, but with all features enabled:Generating your trait and evaluating benchmarks
This step creates or updates your pallet's
weights.rs
.This step is run to generate your pallet's
Weights
trait, for use in your pallet's#[pallet::weight]
tags. It should also be re-run to incorporate changes to the complexities of functions or new dispatchables.Replace
what_ever
&what-ever
with your pallet's name (e.g.gilt
,elections_phragmen
&elections-phragmen
).Incorporate weight formulae
In your pallet's
lib.rs
:pub mod weights;
at the top.pub use crate::weights::WeightInfo;
to expose theWeightInfo
trait from your pallet.WeightInfo
type into theConfig
trait:WeightInfo
type, e.g.:Define the
WeightInfo
type inConfig
trait implsAdditionally, you'll need to define the
WeightInfo
trait in both your pallet's tests mockupmock.rs
and the Substratenode
's runtime.In your pallet's
Config
trait impl inmock.rs
, introduce the line:In your pallet's
Config
trait impl inbin/node/runtime/src/lib.rs
, introduce the line:Integrating into other runtimes
When integrating into other runtimes, you'll want to use that runtime's custom overarching
WeightInfo
.Ensure that the runtime's
fn dispatch_benchmark
has aadd_benchmark!
macro invocation for your pallet.Initially, just use the
()
as theWeightInfo
type for your pallet. E.g.:Run the benchmarks for the runtime as usual. This will require you building your node's CLI with
--features runtime-benchmarks
and then running with the appropriate CLI options as above. E.g. for Kusama:Reference hardware should really be used. E.g. on the Polkadot CI to benchmark for Kusama, issue a comment:
One you have the new file, you'll want to integrate it into your runtime. First go into the runtime's weights
mod.rs
and include thepub mod
line to use the new weight file.E.g. for Kusama, go into
runtime/kusama/src/weights/mod.rs
and add to the list ofpub mod
lines:Once that's done, you can switch the pallet's config trait impl to use the real weights:
That's it!