9

Some pallets configure this field like so : type WeightInfo = pallet_pallet_name::weights::SubstrateWeight<Runtime>;

However I've seen some runtimes in which they have an extra WeightInfo module from which they import weights, typically with a header like "using weights that were auto generated by running benchmarks". How can I generate weights by running benchmarks? Or is it okay to use this type WeightInfo = pallet_pallet_name::weights::SubstrateWeight<Runtime>;

1

3 Answers 3

10

On a production chain, you should always re-calculate your weights by running benchmarks on the final runtime. The point of benchmarking is that your runtime can be configured in different ways, and your blockchain may have different assumptions than others. So running benchmarks gives you real results for exactly how you developed your runtime.

The benchmarking tooling is enabled in Substrate based chains by compiling with the feature flag --features=runtime-benchmarks.

And can be run like so:

./target/release/substrate benchmark \
    --chain dev \                  # Configurable Chain Spec
    --execution=wasm \             # Always test with Wasm
    --wasm-execution=compiled \    # Always used `wasm-time`
    --pallet pallet_balances \     # Select the pallet
    --extrinsic transfer \         # Select the extrinsic
    --steps 50 \                   # Number of samples across component ranges
    --repeat 20 \                  # Number of times we repeat a benchmark
    --output <path> \              # Output benchmark results into a folder or file

More information about benchmarking can be found here: https://github.com/paritytech/substrate/tree/master/frame/benchmarking

And this video: https://www.youtube.com/watch?v=Qa6sTyUqgek

2
  • 1
    SubstrateWeights are default weights for test networks I think.
    – Squirrel
    Feb 23 at 10:06
  • 1
    They are a placeholder weight coming from running benchmarks on the node included in Substrate. They are not accurate to your specific runtime, since they come from Substrate, but also probably not that far off as a placeholder for a test network.
    – Shawn Tabrizi
    Feb 23 at 16:04
4

Write benchmarks and do benchmarks, see https://docs.substrate.io/v3/runtime/benchmarking/ .

The following commands will do benchmarks in specified pallet and generate weights.rs.

./target/release/node benchmark \
--chain=dev \
--execution=wasm \
--wasm-execution=compiled \
--pallet="pallet_${pallet}" \
--extrinsic='*' \
--steps=$steps \
--repeat=$repeat \
--template="./.maintain/frame-weight-template.hbs" \
--output="./pallets/${pallet}/src/weights.rs"
2

If you write and execute a script like this (move the build command outside of the loop) then you can run benchmarking and automatically generate the weights in an output folder for multiple pallets with a single command

1
  • 2
    I would suggest pasting the contents of the script in this answer in case the link breaks in the future.
    – Shawn Tabrizi
    Mar 18 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.