3

I have written benchmarking functions from my pallet. How I can test these benchmarking functions please?

1 Answer 1

2

You have to add the following line to your benchmarking. For example like in pallet-assets. Make sure to adjust it to your needs.

impl_benchmark_test_suite!(
    MyPallet,
    crate::mock::new_test_ext(),
    crate::mock::Test
)

This will create a unit test for each benchmark and run it a few times with different values for each component. Starting the tests looks like this:

cargo test -p pallet-whatever --features runtime-benchmarks

as described in the docs and README.md.
If you try this for example for the lottery pallet, you should see the following output:

test benchmarking::bench_start_lottery ... ok
test benchmarking::bench_stop_repeat ... ok
test benchmarking::bench_on_initialize_end ... ok
test benchmarking::bench_set_calls ... ok
test benchmarking::bench_buy_ticket ... ok
test benchmarking::bench_on_initialize_repeat ... ok

Each line is the output of one benchmark unit-test.
Please not that benchmark tests are no-std and that normally you cannot use functions from the mock.rs since it is std. You can, however, create a nostd_mock.rs with helper functions that are usable from tests and benchmarks.

4
  • Dear @Oliver, Thank you very much for your time. Let me try it to mark this as answer. Just one followup question. We will run it from the node (in which we have added that pallet) or from within the pallet itself? Commented Nov 22, 2022 at 13:53
  • The tests run in the mocked runtime completely without any node runtime. The benchmarks will then indeed need the node's runtime to produce the correct weights. Commented Nov 22, 2022 at 21:06
  • After running this command in my pallet the output is: "running 0 tests". So there are no test running Commented Nov 23, 2022 at 11:59
  • How am I supposed to check without source code? Commented Nov 23, 2022 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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