Benchmarking: Add pov_mode to V2 syntax (#3616)

Changes:
- Port the `pov_mode` attribute from the V1 syntax to V2
- Update `pallet-whitelist` and `frame-benchmarking-pallet-pov`

Follow up: also allow this attribute on top-level benchmark modules.

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: command-bot <>
This commit is contained in:
Oliver Tale-Yazdi
2024-03-19 13:35:22 +02:00
committed by GitHub
parent 430ad2f561
commit abd3f0c49a
18 changed files with 1086 additions and 602 deletions
+28
View File
@@ -0,0 +1,28 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
title: "Benchmarking pallet V2 syntax extension: pov_mode attribute"
doc:
- audience: Runtime Dev
description: |
Adds the `pov_mode` attribute from the V1 benchmarking syntax to the V2 syntax. This allows to
override the default PoV mode (`MaxEncodedLen`) to either `Measured` or `Ignored`. It can be
overridden for a whole benchmark, a key prefix of a specific key itself.
Example syntax looks like this:
```rust
#[benchmark(pov_mode = Measured {
Pallet: Measured,
Pallet::Storage: MaxEncodedLen,
})]
fn do_some() {
..
}
```
crates:
- name: frame-support-procedural
bump: minor
- name: frame-support
bump: minor
@@ -21,54 +21,78 @@
use super::*;
use frame_benchmarking::v2::*;
use frame_support::traits::UnfilteredDispatchable;
use frame_system::{Pallet as System, RawOrigin};
use sp_runtime::traits::Hash;
frame_benchmarking::benchmarks! {
storage_single_value_read {
#[benchmarks]
mod benchmarks {
use super::*;
#[benchmark]
fn storage_single_value_read() {
Value::<T>::put(123);
}: {
assert_eq!(Value::<T>::get(), Some(123));
#[block]
{
assert_eq!(Value::<T>::get(), Some(123));
}
}
#[pov_mode = Ignored]
storage_single_value_ignored_read {
#[benchmark(pov_mode = Ignored)]
fn storage_single_value_ignored_read() {
Value::<T>::put(123);
}: {
assert_eq!(Value::<T>::get(), Some(123));
#[block]
{
assert_eq!(Value::<T>::get(), Some(123));
}
}
#[pov_mode = MaxEncodedLen {
#[benchmark(pov_mode = MaxEncodedLen {
Pov::Value2: Ignored
}]
storage_single_value_ignored_some_read {
})]
fn storage_single_value_ignored_some_read() {
Value::<T>::put(123);
Value2::<T>::put(123);
}: {
assert_eq!(Value::<T>::get(), Some(123));
assert_eq!(Value2::<T>::get(), Some(123));
#[block]
{
assert_eq!(Value::<T>::get(), Some(123));
assert_eq!(Value2::<T>::get(), Some(123));
}
}
storage_single_value_read_twice {
#[benchmark]
fn storage_single_value_read_twice() {
Value::<T>::put(123);
}: {
assert_eq!(Value::<T>::get(), Some(123));
#[block]
{
assert_eq!(Value::<T>::get(), Some(123));
assert_eq!(Value::<T>::get(), Some(123));
}
}
#[benchmark]
fn storage_single_value_write() {
#[block]
{
Value::<T>::put(123);
}
assert_eq!(Value::<T>::get(), Some(123));
}
storage_single_value_write {
}: {
#[benchmark]
fn storage_single_value_kill() {
Value::<T>::put(123);
} verify {
assert_eq!(Value::<T>::get(), Some(123));
}
storage_single_value_kill {
Value::<T>::put(123);
}: {
Value::<T>::kill();
} verify {
#[block]
{
Value::<T>::kill();
}
assert!(!Value::<T>::exists());
}
@@ -78,263 +102,297 @@ frame_benchmarking::benchmarks! {
// created. Then the one value is read from the map. This demonstrates that the number of other
// nodes in the Trie influences the proof size. The number of inserted nodes can be interpreted
// as the number of `StorageMap`/`StorageValue` in the whole runtime.
#[pov_mode = Measured]
storage_1m_map_read_one_value_two_additional_layers {
(0..(1<<10)).for_each(|i| Map1M::<T>::insert(i, i));
#[benchmark(pov_mode = Measured)]
fn storage_1m_map_read_one_value_two_additional_layers() {
(0..(1 << 10)).for_each(|i| Map1M::<T>::insert(i, i));
// Assume there are 16-256 other storage items.
(0..(1u32<<4)).for_each(|i| {
(0..(1u32 << 4)).for_each(|i| {
let k = T::Hashing::hash(&i.to_be_bytes());
frame_support::storage::unhashed::put(k.as_ref(), &i);
});
}: {
assert_eq!(Map1M::<T>::get(1<<9), Some(1<<9));
#[block]
{
assert_eq!(Map1M::<T>::get(1 << 9), Some(1 << 9));
}
}
#[pov_mode = Measured]
storage_1m_map_read_one_value_three_additional_layers {
(0..(1<<10)).for_each(|i| Map1M::<T>::insert(i, i));
#[benchmark(pov_mode = Measured)]
fn storage_1m_map_read_one_value_three_additional_layers() {
(0..(1 << 10)).for_each(|i| Map1M::<T>::insert(i, i));
// Assume there are 256-4096 other storage items.
(0..(1u32<<8)).for_each(|i| {
(0..(1u32 << 8)).for_each(|i| {
let k = T::Hashing::hash(&i.to_be_bytes());
frame_support::storage::unhashed::put(k.as_ref(), &i);
});
}: {
assert_eq!(Map1M::<T>::get(1<<9), Some(1<<9));
#[block]
{
assert_eq!(Map1M::<T>::get(1 << 9), Some(1 << 9));
}
}
#[pov_mode = Measured]
storage_1m_map_read_one_value_four_additional_layers {
(0..(1<<10)).for_each(|i| Map1M::<T>::insert(i, i));
#[benchmark(pov_mode = Measured)]
fn storage_1m_map_read_one_value_four_additional_layers() {
(0..(1 << 10)).for_each(|i| Map1M::<T>::insert(i, i));
// Assume there are 4096-65536 other storage items.
(0..(1u32<<12)).for_each(|i| {
(0..(1u32 << 12)).for_each(|i| {
let k = T::Hashing::hash(&i.to_be_bytes());
frame_support::storage::unhashed::put(k.as_ref(), &i);
});
}: {
assert_eq!(Map1M::<T>::get(1<<9), Some(1<<9));
#[block]
{
assert_eq!(Map1M::<T>::get(1 << 9), Some(1 << 9));
}
}
// Reads from both storage maps each `n` and `m` times. Should result in two linear components.
storage_map_read_per_component {
let n in 0 .. 100;
let m in 0 .. 100;
#[benchmark]
fn storage_map_read_per_component(n: Linear<0, 100>, m: Linear<0, 100>) {
(0..m * 10).for_each(|i| Map1M::<T>::insert(i, i));
(0..n * 10).for_each(|i| Map16M::<T>::insert(i, i));
(0..m*10).for_each(|i| Map1M::<T>::insert(i, i));
(0..n*10).for_each(|i| Map16M::<T>::insert(i, i));
}: {
(0..m).for_each(|i|
assert_eq!(Map1M::<T>::get(i*10), Some(i*10)));
(0..n).for_each(|i|
assert_eq!(Map16M::<T>::get(i*10), Some(i*10)));
#[block]
{
(0..m).for_each(|i| assert_eq!(Map1M::<T>::get(i * 10), Some(i * 10)));
(0..n).for_each(|i| assert_eq!(Map16M::<T>::get(i * 10), Some(i * 10)));
}
}
#[pov_mode = MaxEncodedLen {
#[benchmark(pov_mode = MaxEncodedLen {
Pov::Map1M: Ignored
}]
storage_map_read_per_component_one_ignored {
let n in 0 .. 100;
let m in 0 .. 100;
})]
fn storage_map_read_per_component_one_ignored(n: Linear<0, 100>, m: Linear<0, 100>) {
(0..m * 10).for_each(|i| Map1M::<T>::insert(i, i));
(0..n * 10).for_each(|i| Map16M::<T>::insert(i, i));
(0..m*10).for_each(|i| Map1M::<T>::insert(i, i));
(0..n*10).for_each(|i| Map16M::<T>::insert(i, i));
}: {
(0..m).for_each(|i|
assert_eq!(Map1M::<T>::get(i*10), Some(i*10)));
(0..n).for_each(|i|
assert_eq!(Map16M::<T>::get(i*10), Some(i*10)));
#[block]
{
(0..m).for_each(|i| assert_eq!(Map1M::<T>::get(i * 10), Some(i * 10)));
(0..n).for_each(|i| assert_eq!(Map16M::<T>::get(i * 10), Some(i * 10)));
}
}
// Reads the same value from a storage map. Should not result in a component.
storage_1m_map_one_entry_repeated_read {
let n in 0 .. 100;
#[benchmark]
fn storage_1m_map_one_entry_repeated_read(n: Linear<0, 100>) {
Map1M::<T>::insert(0, 0);
}: {
(0..n).for_each(|i|
assert_eq!(Map1M::<T>::get(0), Some(0)));
#[block]
{
(0..n).for_each(|_| assert_eq!(Map1M::<T>::get(0), Some(0)));
}
}
// Reads the same values from a storage map. Should result in a `1x` linear component.
storage_1m_map_multiple_entry_repeated_read {
let n in 0 .. 100;
#[benchmark]
fn storage_1m_map_multiple_entry_repeated_read(n: Linear<0, 100>) {
(0..n).for_each(|i| Map1M::<T>::insert(i, i));
}: {
(0..n).for_each(|i| {
// Reading the same value 10 times does nothing.
(0..10).for_each(|j|
assert_eq!(Map1M::<T>::get(i), Some(i)));
});
#[block]
{
(0..n).for_each(|i| {
// Reading the same value 10 times does nothing.
(0..10).for_each(|_| assert_eq!(Map1M::<T>::get(i), Some(i)));
});
}
}
storage_1m_double_map_read_per_component {
let n in 0 .. 1024;
(0..(1<<10)).for_each(|i| DoubleMap1M::<T>::insert(i, i, i));
}: {
(0..n).for_each(|i|
assert_eq!(DoubleMap1M::<T>::get(i, i), Some(i)));
#[benchmark]
fn storage_1m_double_map_read_per_component(n: Linear<0, 1024>) {
(0..(1 << 10)).for_each(|i| DoubleMap1M::<T>::insert(i, i, i));
#[block]
{
(0..n).for_each(|i| assert_eq!(DoubleMap1M::<T>::get(i, i), Some(i)));
}
}
storage_value_bounded_read {
}: {
assert!(BoundedValue::<T>::get().is_none());
#[benchmark]
fn storage_value_bounded_read() {
#[block]
{
assert!(BoundedValue::<T>::get().is_none());
}
}
// Reading unbounded values will produce no mathematical worst case PoV size for this component.
storage_value_unbounded_read {
}: {
assert!(UnboundedValue::<T>::get().is_none());
#[benchmark]
fn storage_value_unbounded_read() {
#[block]
{
assert!(UnboundedValue::<T>::get().is_none());
}
}
#[pov_mode = Ignored]
storage_value_unbounded_ignored_read {
}: {
assert!(UnboundedValue::<T>::get().is_none());
#[benchmark(pov_mode = Ignored)]
fn storage_value_unbounded_ignored_read() {
#[block]
{
assert!(UnboundedValue::<T>::get().is_none());
}
}
// Same as above, but we still expect a mathematical worst case PoV size for the bounded one.
storage_value_bounded_and_unbounded_read {
#[benchmark]
fn storage_value_bounded_and_unbounded_read() {
(0..1024).for_each(|i| Map1M::<T>::insert(i, i));
}: {
assert!(UnboundedValue::<T>::get().is_none());
assert!(BoundedValue::<T>::get().is_none());
#[block]
{
assert!(UnboundedValue::<T>::get().is_none());
assert!(BoundedValue::<T>::get().is_none());
}
}
#[pov_mode = Measured]
measured_storage_value_read_linear_size {
let l in 0 .. 1<<22;
#[benchmark(pov_mode = Measured)]
fn measured_storage_value_read_linear_size(l: Linear<0, { 1 << 22 }>) {
let v: sp_runtime::BoundedVec<u8, _> = sp_std::vec![0u8; l as usize].try_into().unwrap();
LargeValue::<T>::put(&v);
}: {
assert!(LargeValue::<T>::get().is_some());
#[block]
{
assert!(LargeValue::<T>::get().is_some());
}
}
#[pov_mode = MaxEncodedLen]
mel_storage_value_read_linear_size {
let l in 0 .. 1<<22;
#[benchmark(pov_mode = MaxEncodedLen)]
fn mel_storage_value_read_linear_size(l: Linear<0, { 1 << 22 }>) {
let v: sp_runtime::BoundedVec<u8, _> = sp_std::vec![0u8; l as usize].try_into().unwrap();
LargeValue::<T>::put(&v);
}: {
assert!(LargeValue::<T>::get().is_some());
#[block]
{
assert!(LargeValue::<T>::get().is_some());
}
}
#[pov_mode = Measured]
measured_storage_double_value_read_linear_size {
let l in 0 .. 1<<22;
#[benchmark(pov_mode = Measured)]
fn measured_storage_double_value_read_linear_size(l: Linear<0, { 1 << 22 }>) {
let v: sp_runtime::BoundedVec<u8, _> = sp_std::vec![0u8; l as usize].try_into().unwrap();
LargeValue::<T>::put(&v);
LargeValue2::<T>::put(&v);
}: {
assert!(LargeValue::<T>::get().is_some());
assert!(LargeValue2::<T>::get().is_some());
#[block]
{
assert!(LargeValue::<T>::get().is_some());
assert!(LargeValue2::<T>::get().is_some());
}
}
#[pov_mode = MaxEncodedLen]
mel_storage_double_value_read_linear_size {
let l in 0 .. 1<<22;
#[benchmark(pov_mode = MaxEncodedLen)]
fn mel_storage_double_value_read_linear_size(l: Linear<0, { 1 << 22 }>) {
let v: sp_runtime::BoundedVec<u8, _> = sp_std::vec![0u8; l as usize].try_into().unwrap();
LargeValue::<T>::put(&v);
LargeValue2::<T>::put(&v);
}: {
assert!(LargeValue::<T>::get().is_some());
assert!(LargeValue2::<T>::get().is_some());
#[block]
{
assert!(LargeValue::<T>::get().is_some());
assert!(LargeValue2::<T>::get().is_some());
}
}
#[pov_mode = MaxEncodedLen {
#[benchmark(pov_mode = MaxEncodedLen {
Pov::LargeValue2: Measured
}]
mel_mixed_storage_double_value_read_linear_size {
let l in 0 .. 1<<22;
})]
fn mel_mixed_storage_double_value_read_linear_size(l: Linear<0, { 1 << 22 }>) {
let v: sp_runtime::BoundedVec<u8, _> = sp_std::vec![0u8; l as usize].try_into().unwrap();
LargeValue::<T>::put(&v);
LargeValue2::<T>::put(&v);
}: {
assert!(LargeValue::<T>::get().is_some());
assert!(LargeValue2::<T>::get().is_some());
#[block]
{
assert!(LargeValue::<T>::get().is_some());
assert!(LargeValue2::<T>::get().is_some());
}
}
#[pov_mode = Measured {
#[benchmark(pov_mode = Measured {
Pov::LargeValue2: MaxEncodedLen
}]
measured_mixed_storage_double_value_read_linear_size {
let l in 0 .. 1<<22;
})]
fn measured_mixed_storage_double_value_read_linear_size(l: Linear<0, { 1 << 22 }>) {
let v: sp_runtime::BoundedVec<u8, _> = sp_std::vec![0u8; l as usize].try_into().unwrap();
LargeValue::<T>::put(&v);
LargeValue2::<T>::put(&v);
}: {
assert!(LargeValue::<T>::get().is_some());
assert!(LargeValue2::<T>::get().is_some());
#[block]
{
assert!(LargeValue::<T>::get().is_some());
assert!(LargeValue2::<T>::get().is_some());
}
}
#[pov_mode = Measured]
storage_map_unbounded_both_measured_read {
let i in 0 .. 1000;
#[benchmark(pov_mode = Measured)]
fn storage_map_unbounded_both_measured_read(i: Linear<0, 1000>) {
UnboundedMap::<T>::insert(i, sp_std::vec![0; i as usize]);
UnboundedMap2::<T>::insert(i, sp_std::vec![0; i as usize]);
}: {
assert!(UnboundedMap::<T>::get(i).is_some());
assert!(UnboundedMap2::<T>::get(i).is_some());
#[block]
{
assert!(UnboundedMap::<T>::get(i).is_some());
assert!(UnboundedMap2::<T>::get(i).is_some());
}
}
#[pov_mode = MaxEncodedLen {
#[benchmark(pov_mode = MaxEncodedLen {
Pov::UnboundedMap: Measured
}]
storage_map_partial_unbounded_read {
let i in 0 .. 1000;
})]
fn storage_map_partial_unbounded_read(i: Linear<0, 1000>) {
Map1M::<T>::insert(i, 0);
UnboundedMap::<T>::insert(i, sp_std::vec![0; i as usize]);
}: {
assert!(Map1M::<T>::get(i).is_some());
assert!(UnboundedMap::<T>::get(i).is_some());
#[block]
{
assert!(Map1M::<T>::get(i).is_some());
assert!(UnboundedMap::<T>::get(i).is_some());
}
}
#[pov_mode = MaxEncodedLen {
#[benchmark(pov_mode = MaxEncodedLen {
Pov::UnboundedMap: Ignored
}]
storage_map_partial_unbounded_ignored_read {
let i in 0 .. 1000;
})]
fn storage_map_partial_unbounded_ignored_read(i: Linear<0, 1000>) {
Map1M::<T>::insert(i, 0);
UnboundedMap::<T>::insert(i, sp_std::vec![0; i as usize]);
}: {
assert!(Map1M::<T>::get(i).is_some());
assert!(UnboundedMap::<T>::get(i).is_some());
#[block]
{
assert!(Map1M::<T>::get(i).is_some());
assert!(UnboundedMap::<T>::get(i).is_some());
}
}
// Emitting an event will not incur any PoV.
emit_event {
#[benchmark]
fn emit_event() {
// Emit a single event.
let call = Call::<T>::emit_event { };
}: { call.dispatch_bypass_filter(RawOrigin::Root.into()).unwrap(); }
verify {
let call = Call::<T>::emit_event {};
#[block]
{
call.dispatch_bypass_filter(RawOrigin::Root.into()).unwrap();
}
assert_eq!(System::<T>::events().len(), 1);
}
// A No-OP will not incur any PoV.
noop {
let call = Call::<T>::noop { };
}: {
call.dispatch_bypass_filter(RawOrigin::Root.into()).unwrap();
#[benchmark]
fn noop() {
let call = Call::<T>::noop {};
#[block]
{
call.dispatch_bypass_filter(RawOrigin::Root.into()).unwrap();
}
}
storage_iteration {
#[benchmark]
fn storage_iteration() {
for i in 0..65000 {
UnboundedMapTwox::<T>::insert(i, sp_std::vec![0; 64]);
}
}: {
for (key, value) in UnboundedMapTwox::<T>::iter() {
unsafe {
core::ptr::read_volatile(&key);
core::ptr::read_volatile(value.as_ptr());
#[block]
{
for (key, value) in UnboundedMapTwox::<T>::iter() {
unsafe {
core::ptr::read_volatile(&key);
core::ptr::read_volatile(value.as_ptr());
}
}
}
}
impl_benchmark_test_suite!(
Pallet,
mock::new_test_ext(),
mock::Test,
);
impl_benchmark_test_suite!(Pallet, super::mock::new_test_ext(), super::mock::Test,);
}
#[cfg(test)]
File diff suppressed because it is too large Load Diff
@@ -40,10 +40,14 @@ mod keywords {
custom_keyword!(benchmarks);
custom_keyword!(block);
custom_keyword!(extra);
custom_keyword!(pov_mode);
custom_keyword!(extrinsic_call);
custom_keyword!(skip_meta);
custom_keyword!(BenchmarkError);
custom_keyword!(Result);
custom_keyword!(MaxEncodedLen);
custom_keyword!(Measured);
custom_keyword!(Ignored);
pub const BENCHMARK_TOKEN: &str = stringify!(benchmark);
pub const BENCHMARKS_TOKEN: &str = stringify!(benchmarks);
@@ -73,51 +77,158 @@ struct RangeArgs {
struct BenchmarkAttrs {
skip_meta: bool,
extra: bool,
pov_mode: Option<PovModeAttr>,
}
/// Represents a single benchmark option
enum BenchmarkAttrKeyword {
enum BenchmarkAttr {
Extra,
SkipMeta,
/// How the PoV should be measured.
PoV(PovModeAttr),
}
impl syn::parse::Parse for BenchmarkAttrKeyword {
impl syn::parse::Parse for PovModeAttr {
fn parse(input: ParseStream) -> Result<Self> {
let _pov: keywords::pov_mode = input.parse()?;
let _eq: Token![=] = input.parse()?;
let root = PovEstimationMode::parse(input)?;
let mut maybe_content = None;
let _ = || -> Result<()> {
let content;
syn::braced!(content in input);
maybe_content = Some(content);
Ok(())
}();
let per_key = match maybe_content {
Some(content) => {
let per_key = Punctuated::<PovModeKeyAttr, Token![,]>::parse_terminated(&content)?;
per_key.into_iter().collect()
},
None => Vec::new(),
};
Ok(Self { root, per_key })
}
}
impl syn::parse::Parse for BenchmarkAttr {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(keywords::extra) {
let _extra: keywords::extra = input.parse()?;
return Ok(BenchmarkAttrKeyword::Extra)
Ok(BenchmarkAttr::Extra)
} else if lookahead.peek(keywords::skip_meta) {
let _skip_meta: keywords::skip_meta = input.parse()?;
return Ok(BenchmarkAttrKeyword::SkipMeta)
Ok(BenchmarkAttr::SkipMeta)
} else if lookahead.peek(keywords::pov_mode) {
PovModeAttr::parse(input).map(BenchmarkAttr::PoV)
} else {
Err(lookahead.error())
}
}
}
/// A `#[pov_mode = .. { .. }]` attribute.
#[derive(Debug, Clone)]
struct PovModeAttr {
/// The root mode for this benchmarks.
root: PovEstimationMode,
/// The pov-mode for a specific key. This overwrites `root` for this key.
per_key: Vec<PovModeKeyAttr>,
}
/// A single key-value pair inside the `{}` of a `#[pov_mode = .. { .. }]` attribute.
#[derive(Debug, Clone, derive_syn_parse::Parse)]
struct PovModeKeyAttr {
/// A specific storage key for which to set the PoV mode.
key: Path,
_underscore: Token![:],
/// The PoV mode for this key.
mode: PovEstimationMode,
}
/// How the PoV should be estimated.
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum PovEstimationMode {
/// Use the maximal encoded length as provided by [`codec::MaxEncodedLen`].
MaxEncodedLen,
/// Measure the accessed value size in the pallet benchmarking and add some trie overhead.
Measured,
/// Do not estimate the PoV size for this storage item or benchmark.
Ignored,
}
impl syn::parse::Parse for PovEstimationMode {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(keywords::MaxEncodedLen) {
let _max_encoded_len: keywords::MaxEncodedLen = input.parse()?;
return Ok(PovEstimationMode::MaxEncodedLen)
} else if lookahead.peek(keywords::Measured) {
let _measured: keywords::Measured = input.parse()?;
return Ok(PovEstimationMode::Measured)
} else if lookahead.peek(keywords::Ignored) {
let _ignored: keywords::Ignored = input.parse()?;
return Ok(PovEstimationMode::Ignored)
} else {
return Err(lookahead.error())
}
}
}
impl ToString for PovEstimationMode {
fn to_string(&self) -> String {
match self {
PovEstimationMode::MaxEncodedLen => "MaxEncodedLen".into(),
PovEstimationMode::Measured => "Measured".into(),
PovEstimationMode::Ignored => "Ignored".into(),
}
}
}
impl quote::ToTokens for PovEstimationMode {
fn to_tokens(&self, tokens: &mut TokenStream2) {
match self {
PovEstimationMode::MaxEncodedLen => tokens.extend(quote!(MaxEncodedLen)),
PovEstimationMode::Measured => tokens.extend(quote!(Measured)),
PovEstimationMode::Ignored => tokens.extend(quote!(Ignored)),
}
}
}
impl syn::parse::Parse for BenchmarkAttrs {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut extra = false;
let mut skip_meta = false;
let args = Punctuated::<BenchmarkAttrKeyword, Token![,]>::parse_terminated(&input)?;
let mut pov_mode = None;
let args = Punctuated::<BenchmarkAttr, Token![,]>::parse_terminated(&input)?;
for arg in args.into_iter() {
match arg {
BenchmarkAttrKeyword::Extra => {
BenchmarkAttr::Extra => {
if extra {
return Err(input.error("`extra` can only be specified once"))
}
extra = true;
},
BenchmarkAttrKeyword::SkipMeta => {
BenchmarkAttr::SkipMeta => {
if skip_meta {
return Err(input.error("`skip_meta` can only be specified once"))
}
skip_meta = true;
},
BenchmarkAttr::PoV(mode) => {
if pov_mode.is_some() {
return Err(input.error("`pov_mode` can only be specified once"))
}
pov_mode = Some(mode);
},
}
}
Ok(BenchmarkAttrs { extra, skip_meta })
Ok(BenchmarkAttrs { extra, skip_meta, pov_mode })
}
}
@@ -344,6 +455,7 @@ pub fn benchmarks(
tokens: TokenStream,
instance: bool,
) -> syn::Result<TokenStream> {
let krate = generate_access_from_frame_or_crate("frame-benchmarking")?;
// gather module info
let module: ItemMod = syn::parse(tokens)?;
let mod_span = module.span();
@@ -364,6 +476,8 @@ pub fn benchmarks(
let mut benchmark_names: Vec<Ident> = Vec::new();
let mut extra_benchmark_names: Vec<Ident> = Vec::new();
let mut skip_meta_benchmark_names: Vec<Ident> = Vec::new();
// Map benchmarks to PoV modes.
let mut pov_modes = Vec::new();
let (_brace, mut content) =
module.content.ok_or(syn::Error::new(mod_span, "Module cannot be empty!"))?;
@@ -400,6 +514,25 @@ pub fn benchmarks(
} else if benchmark_attrs.skip_meta {
skip_meta_benchmark_names.push(name.clone());
}
if let Some(mode) = benchmark_attrs.pov_mode {
let mut modes = Vec::new();
// We cannot expand strings here since it is no-std, but syn does not expand bytes.
let name = name.to_string();
let m = mode.root.to_string();
modes.push(quote!(("ALL".as_bytes().to_vec(), #m.as_bytes().to_vec())));
for attr in mode.per_key.iter() {
// syn always puts spaces in quoted paths:
let key = attr.key.clone().into_token_stream().to_string().replace(" ", "");
let mode = attr.mode.to_string();
modes.push(quote!((#key.as_bytes().to_vec(), #mode.as_bytes().to_vec())));
}
pov_modes.push(
quote!((#name.as_bytes().to_vec(), #krate::__private::vec![#(#modes),*])),
);
}
}
// expand benchmark
@@ -419,7 +552,6 @@ pub fn benchmarks(
true => quote!(T: Config<I>, I: 'static),
};
let krate = generate_access_from_frame_or_crate("frame-benchmarking")?;
let frame_system = generate_access_from_frame_or_crate("frame-system")?;
// benchmark name variables
@@ -537,6 +669,16 @@ pub fn benchmarks(
];
all_names.retain(|x| !extra.contains(x));
}
let pov_modes:
#krate::__private::Vec<(
#krate::__private::Vec<u8>,
#krate::__private::Vec<(
#krate::__private::Vec<u8>,
#krate::__private::Vec<u8>
)>,
)> = #krate::__private::vec![
#( #pov_modes ),*
];
all_names.into_iter().map(|benchmark| {
let selected_benchmark = match benchmark {
#(#selected_benchmark_mappings),
@@ -544,12 +686,13 @@ pub fn benchmarks(
_ => panic!("all benchmarks should be selectable")
};
let components = <SelectedBenchmark as #krate::BenchmarkingSetup<#type_use_generics>>::components(&selected_benchmark);
let name = benchmark.as_bytes().to_vec();
let modes = pov_modes.iter().find(|p| p.0 == name).map(|p| p.1.clone());
#krate::BenchmarkMetadata {
name: benchmark.as_bytes().to_vec(),
components,
// TODO: Not supported by V2 syntax as of yet.
// https://github.com/paritytech/substrate/issues/13132
pov_modes: #krate::__private::vec![],
pov_modes: modes.unwrap_or_default(),
}
}).collect::<#krate::__private::Vec<_>>()
}
@@ -0,0 +1,31 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use frame_benchmarking::v2::*;
#[benchmarks]
mod benches {
use super::*;
#[benchmark(pov_mode = Wrong)]
fn bench() {
#[block]
{}
}
}
fn main() {}
@@ -0,0 +1,5 @@
error: expected one of: `MaxEncodedLen`, `Measured`, `Ignored`
--> tests/benchmark_ui/bad_attr_pov_mode_1.rs:24:25
|
24 | #[benchmark(pov_mode = Wrong)]
| ^^^^^
@@ -0,0 +1,33 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use frame_benchmarking::v2::*;
#[benchmarks]
mod benches {
use super::*;
#[benchmark(pov_mode = Measured {
Key: Wrong
})]
fn bench() {
#[block]
{}
}
}
fn main() {}
@@ -0,0 +1,5 @@
error: expected one of: `MaxEncodedLen`, `Measured`, `Ignored`
--> tests/benchmark_ui/bad_attr_pov_mode_2.rs:25:8
|
25 | Key: Wrong
| ^^^^^
@@ -0,0 +1,31 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use frame_benchmarking::v2::*;
#[benchmarks]
mod benches {
use super::*;
#[benchmark(pov_mode)]
fn bench() {
#[block]
{}
}
}
fn main() {}
@@ -0,0 +1,5 @@
error: expected `=`
--> tests/benchmark_ui/bad_attr_pov_mode_3.rs:24:22
|
24 | #[benchmark(pov_mode)]
| ^
@@ -0,0 +1,31 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use frame_benchmarking::v2::*;
#[benchmarks]
mod benches {
use super::*;
#[benchmark(pov_mode =)]
fn bench() {
#[block]
{}
}
}
fn main() {}
@@ -0,0 +1,5 @@
error: unexpected end of input, expected one of: `MaxEncodedLen`, `Measured`, `Ignored`
--> tests/benchmark_ui/bad_attr_pov_mode_4.rs:24:24
|
24 | #[benchmark(pov_mode =)]
| ^
@@ -0,0 +1,32 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use frame_benchmarking::v2::*;
use frame_support_test::Config;
#[benchmarks]
mod benches {
use super::*;
#[benchmark(pov_mode = Measured, pov_mode = MaxEncodedLen)]
fn bench() {
#[block]
{}
}
}
fn main() {}
@@ -0,0 +1,14 @@
error: unexpected end of input, `pov_mode` can only be specified once
--> tests/benchmark_ui/dup_attr_pov_mode.rs:25:59
|
25 | #[benchmark(pov_mode = Measured, pov_mode = MaxEncodedLen)]
| ^
error: unused import: `frame_support_test::Config`
--> tests/benchmark_ui/dup_attr_pov_mode.rs:19:5
|
19 | use frame_support_test::Config;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D unused-imports` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(unused_imports)]`
@@ -0,0 +1,74 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use frame_benchmarking::v2::*;
use frame_support_test::Config;
#[benchmarks]
mod benches {
use super::*;
#[benchmark(skip_meta, extra, pov_mode = Measured)]
fn bench1() {
#[block]
{}
}
#[benchmark(pov_mode = Measured, extra, skip_meta)]
fn bench2() {
#[block]
{}
}
#[benchmark(extra, pov_mode = Measured {
Pallet: Measured,
Pallet::Storage: MaxEncodedLen,
}, skip_meta)]
fn bench3() {
#[block]
{}
}
#[benchmark(skip_meta, extra, pov_mode = Measured {
Pallet::Storage: MaxEncodedLen,
Pallet::StorageSubKey: Measured,
})]
fn bench4() {
#[block]
{}
}
#[benchmark(pov_mode = MaxEncodedLen {
Pallet::Storage: Measured,
Pallet::StorageSubKey: Measured
}, extra, skip_meta)]
fn bench5() {
#[block]
{}
}
#[benchmark(pov_mode = MaxEncodedLen {
Pallet::Storage: Measured,
Pallet::Storage::Nested: Ignored
}, extra, skip_meta)]
fn bench6() {
#[block]
{}
}
}
fn main() {}
@@ -22,7 +22,7 @@ use frame_support_test::Config;
mod benches {
use super::*;
#[benchmark(skip_meta, extra)]
#[benchmark(skip_meta, pov_mode = Measured, extra)]
fn bench() {
let a = 2 + 2;
#[block]
@@ -1,4 +1,4 @@
error: expected `extra` or `skip_meta`
error: expected one of: `extra`, `skip_meta`, `pov_mode`
--> tests/benchmark_ui/unrecognized_option.rs:26:32
|
26 | #[benchmark(skip_meta, extra, bad)]
+45 -54
View File
@@ -20,58 +20,57 @@
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::v1::{benchmarks, BenchmarkError};
use frame_support::{ensure, traits::EnsureOrigin};
use frame_benchmarking::v2::*;
use frame_support::traits::EnsureOrigin;
#[cfg(test)]
use crate::Pallet as Whitelist;
benchmarks! {
whitelist_call {
#[benchmarks]
mod benchmarks {
use super::*;
#[benchmark]
fn whitelist_call() -> Result<(), BenchmarkError> {
let origin =
T::WhitelistOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let call_hash = Default::default();
}: _<T::RuntimeOrigin>(origin, call_hash)
verify {
ensure!(
WhitelistedCall::<T>::contains_key(call_hash),
"call not whitelisted"
);
ensure!(
T::Preimages::is_requested(&call_hash),
"preimage not requested"
);
#[extrinsic_call]
_(origin as T::RuntimeOrigin, call_hash);
ensure!(WhitelistedCall::<T>::contains_key(call_hash), "call not whitelisted");
ensure!(T::Preimages::is_requested(&call_hash), "preimage not requested");
Ok(())
}
remove_whitelisted_call {
#[benchmark]
fn remove_whitelisted_call() -> Result<(), BenchmarkError> {
let origin =
T::WhitelistOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let call_hash = Default::default();
Pallet::<T>::whitelist_call(origin.clone(), call_hash)
.expect("whitelisting call must be successful");
}: _<T::RuntimeOrigin>(origin, call_hash)
verify {
ensure!(
!WhitelistedCall::<T>::contains_key(call_hash),
"whitelist not removed"
);
ensure!(
!T::Preimages::is_requested(&call_hash),
"preimage still requested"
);
#[extrinsic_call]
_(origin as T::RuntimeOrigin, call_hash);
ensure!(!WhitelistedCall::<T>::contains_key(call_hash), "whitelist not removed");
ensure!(!T::Preimages::is_requested(&call_hash), "preimage still requested");
Ok(())
}
// We benchmark with the maximum possible size for a call.
// If the resulting weight is too big, maybe it worth having a weight which depends
// on the size of the call, with a new witness in parameter.
#[pov_mode = MaxEncodedLen {
#[benchmark(pov_mode = MaxEncodedLen {
// Use measured PoV size for the Preimages since we pass in a length witness.
Preimage::PreimageFor: Measured
}]
dispatch_whitelisted_call {
// NOTE: we remove `10` because we need some bytes to encode the variants and vec length
let n in 1 .. T::Preimages::MAX_LENGTH as u32 - 10;
})]
// NOTE: we remove `10` because we need some bytes to encode the variants and vec length
fn dispatch_whitelisted_call(
n: Linear<1, { T::Preimages::MAX_LENGTH as u32 - 10 }>,
) -> Result<(), BenchmarkError> {
let origin = T::DispatchWhitelistedOrigin::try_successful_origin()
.map_err(|_| BenchmarkError::Weightless)?;
let remark = sp_std::vec![1u8; n as usize];
@@ -86,21 +85,16 @@ benchmarks! {
T::Preimages::note(encoded_call.into()).unwrap();
}: _<T::RuntimeOrigin>(origin, call_hash, call_encoded_len, call_weight)
verify {
ensure!(
!WhitelistedCall::<T>::contains_key(call_hash),
"whitelist not removed"
);
ensure!(
!T::Preimages::is_requested(&call_hash),
"preimage still requested"
);
#[extrinsic_call]
_(origin as T::RuntimeOrigin, call_hash, call_encoded_len, call_weight);
ensure!(!WhitelistedCall::<T>::contains_key(call_hash), "whitelist not removed");
ensure!(!T::Preimages::is_requested(&call_hash), "preimage still requested");
Ok(())
}
dispatch_whitelisted_call_with_preimage {
let n in 1 .. 10_000;
#[benchmark]
fn dispatch_whitelisted_call_with_preimage(n: Linear<1, 10_000>) -> Result<(), BenchmarkError> {
let origin = T::DispatchWhitelistedOrigin::try_successful_origin()
.map_err(|_| BenchmarkError::Weightless)?;
let remark = sp_std::vec![1u8; n as usize];
@@ -110,16 +104,13 @@ benchmarks! {
Pallet::<T>::whitelist_call(origin.clone(), call_hash)
.expect("whitelisting call must be successful");
}: _<T::RuntimeOrigin>(origin, Box::new(call))
verify {
ensure!(
!WhitelistedCall::<T>::contains_key(call_hash),
"whitelist not removed"
);
ensure!(
!T::Preimages::is_requested(&call_hash),
"preimage still requested"
);
#[extrinsic_call]
_(origin as T::RuntimeOrigin, Box::new(call));
ensure!(!WhitelistedCall::<T>::contains_key(call_hash), "whitelist not removed");
ensure!(!T::Preimages::is_requested(&call_hash), "preimage still requested");
Ok(())
}
impl_benchmark_test_suite!(Whitelist, crate::mock::new_test_ext(), crate::mock::Test);