Skip to content

Commit a83f0fe

Browse files
re-giusseadanda
andauthored
Adding migration instruction from benchmarking v1 to v2 (#6093)
# Adding instruction to migrate benchmarking from v1 to v2 Even if the documentation for benchmarking v1 and v2 is clear and detailed, I feel that adding a migration guide from v1 to v2 would help doing it quicker. ## Integration This change only affects documentation, so it does not cause integration issues. ## Review Notes I followed the migration procedure I applied in PR #6018 . I added everything from there, but I may be missing some extra steps that are needed in specific case, so in case you notice something please let me know. --------- Co-authored-by: Dónal Murray <donal.murray@parity.io>
1 parent b48a6fa commit a83f0fe

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

  • substrate/frame/benchmarking/src

substrate/frame/benchmarking/src/lib.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,83 @@ pub use v1::*;
311311
/// }
312312
/// }
313313
/// ```
314+
///
315+
/// ## Migrate from v1 to v2
316+
///
317+
/// To migrate your code from benchmarking v1 to benchmarking v2, you may follow these
318+
/// steps:
319+
/// 1. Change the import from `frame_benchmarking::v1::` to `frame_benchmarking::v2::*`, or
320+
/// `frame::benchmarking::prelude::*` under the umbrella crate;
321+
/// 2. Move the code inside the v1 `benchmarks! { ... }` block to the v2 benchmarks module `mod
322+
/// benchmarks { ... }` under the benchmarks macro (`#[benchmarks]` for a regular module, or
323+
/// `#[instance_benchmarks]` to set up the module in instance benchmarking mode);
324+
/// 3. Turn each v1 benchmark into a function inside the v2 benchmarks module with the same name,
325+
/// having either a blank return type or a return type compatible with `Result<(),
326+
/// BenchmarkError>`. For instance, `foo { ... }` can become `fn foo() -> Result<(),
327+
/// BenchmarkError>`. More in detail:
328+
/// 1. Move all the v1 complexity parameters as [ParamRange](`v2::ParamRange`) arguments to the
329+
/// v2 function, and their setup code to the body of the function. For instance, `let y in 0
330+
/// .. 10 => setup(y)?;` from v1 will give a `y: Linear<0, 10>` argument to the corresponding
331+
/// function in v2, while `setup(y)?;` will be moved to the body of the function;
332+
/// 2. Move all the v1 setup code to the body of the v2 function;
333+
/// 3. Move the benchmarked code to the body of the v2 function under the appropriate macro
334+
/// attribute: `#[extrinsic_call]` for extrinsic pallet calls and `#[block]` for blocks of
335+
/// code;
336+
/// 4. Move the v1 verify code block to the body of the v2 function, after the
337+
/// `#[extrinsic_call]` or `#[block]` attribute.
338+
/// 5. If the function returns a `Result<(), BenchmarkError>`, end with `Ok(())`.
339+
///
340+
/// As for tests, the code is the same as v1 (see [Benchmark Tests](#benchmark-tests)).
341+
///
342+
/// As an example migration, the following v1 code
343+
///
344+
/// ```ignore
345+
/// #![cfg(feature = "runtime-benchmarks")]
346+
///
347+
/// use frame_benchmarking::v1::*;
348+
///
349+
/// benchmarks! {
350+
///
351+
/// // first dispatchable: this is a user dispatchable and operates on a `u8` vector of
352+
/// // size `l`
353+
/// foo {
354+
/// let caller = funded_account::<T>(b"caller", 0);
355+
/// let l in 1 .. 10_000 => initialize_l(l);
356+
/// }: {
357+
/// _(RuntimeOrigin::Signed(caller), vec![0u8; l])
358+
/// } verify {
359+
/// assert_last_event::<T>(Event::FooExecuted { result: Ok(()) }.into());
360+
/// }
361+
/// }
362+
/// ```
363+
///
364+
/// would become the following v2 code:
365+
///
366+
/// ```ignore
367+
/// #![cfg(feature = "runtime-benchmarks")]
368+
///
369+
/// use frame_benchmarking::v2::*;
370+
///
371+
/// #[benchmarks]
372+
/// mod benchmarks {
373+
/// use super::*;
374+
///
375+
/// // first dispatchable: foo; this is a user dispatchable and operates on a `u8` vector of
376+
/// // size `l`
377+
/// #[benchmark]
378+
/// fn foo(l: Linear<1 .. 10_000>) -> Result<(), BenchmarkError> {
379+
/// let caller = funded_account::<T>(b"caller", 0);
380+
/// initialize_l(l);
381+
///
382+
/// #[extrinsic_call]
383+
/// _(RuntimeOrigin::Signed(caller), vec![0u8; l]);
384+
///
385+
/// // Everything onwards will be treated as test.
386+
/// assert_last_event::<T>(Event::FooExecuted { result: Ok(()) }.into());
387+
/// Ok(())
388+
/// }
389+
/// }
390+
/// ```
314391
pub mod v2 {
315392
pub use super::*;
316393
pub use frame_support_procedural::{

0 commit comments

Comments
 (0)