contracts: Simplify benchmarks (#13595)

* Remove batching

* Benchmark in bytes not kilobytes

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts

* Add rationale for picking batch numbers

---------

Co-authored-by: command-bot <>
This commit is contained in:
Alexander Theißen
2023-03-16 20:19:48 +01:00
committed by GitHub
parent 3a3d93afee
commit 91bb2d29ca
5 changed files with 2062 additions and 2276 deletions
+72 -150
View File
@@ -27,18 +27,9 @@ use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::RuntimeDebug;
use sp_std::{marker::PhantomData, vec::Vec};
use sp_std::marker::PhantomData;
use wasm_instrument::{gas_metering, parity_wasm::elements};
/// How many API calls are executed in a single batch. The reason for increasing the amount
/// of API calls in batches (per benchmark component increase) is so that the linear regression
/// has an easier time determining the contribution of that component.
pub const API_BENCHMARK_BATCH_SIZE: u32 = 80;
/// How many instructions are executed in a single batch. The reasoning is the same
/// as for `API_BENCHMARK_BATCH_SIZE`.
pub const INSTR_BENCHMARK_BATCH_SIZE: u32 = 100;
/// Definition of the cost schedule and other parameterizations for the wasm vm.
///
/// Its [`Default`] implementation is the designated way to initialize this type. It uses
@@ -200,7 +191,6 @@ pub struct InstructionWeights<T: Config> {
pub br_table_per_entry: u32,
pub call: u32,
pub call_indirect: u32,
pub call_indirect_per_param: u32,
pub call_per_local: u32,
pub local_get: u32,
pub local_set: u32,
@@ -451,66 +441,22 @@ macro_rules! cost_args {
}
}
macro_rules! cost_batched_args {
($name:ident, $( $arg: expr ),+) => {
cost_args!($name, $( $arg ),+) / u64::from(API_BENCHMARK_BATCH_SIZE)
}
}
macro_rules! cost_instr_no_params_with_batch_size {
($name:ident, $batch_size:expr) => {
(cost_args!($name, 1).ref_time() / u64::from($batch_size)) as u32
macro_rules! cost_instr_no_params {
($name:ident) => {
cost_args!($name, 1).ref_time() as u32
};
}
macro_rules! cost_instr_with_batch_size {
($name:ident, $num_params:expr, $batch_size:expr) => {
cost_instr_no_params_with_batch_size!($name, $batch_size).saturating_sub(
(cost_instr_no_params_with_batch_size!(instr_i64const, $batch_size) / 2)
.saturating_mul($num_params),
)
};
}
macro_rules! cost_instr {
($name:ident, $num_params:expr) => {
cost_instr_with_batch_size!($name, $num_params, INSTR_BENCHMARK_BATCH_SIZE)
};
}
macro_rules! cost_byte_args {
($name:ident, $( $arg: expr ),+) => {
cost_args!($name, $( $arg ),+) / 1024
}
}
macro_rules! cost_byte_batched_args {
($name:ident, $( $arg: expr ),+) => {
cost_batched_args!($name, $( $arg ),+) / 1024
}
}
macro_rules! cost {
($name:ident) => {
cost_args!($name, 1)
};
}
macro_rules! cost_batched {
($name:ident) => {
cost_batched_args!($name, 1)
};
}
macro_rules! cost_byte {
($name:ident) => {
cost_byte_args!($name, 1)
};
}
macro_rules! cost_byte_batched {
($name:ident) => {
cost_byte_batched_args!($name, 1)
macro_rules! cost_instr {
($name:ident, $num_params:expr) => {
cost_instr_no_params!($name)
.saturating_sub((cost_instr_no_params!(instr_i64const) / 2).saturating_mul($num_params))
};
}
@@ -533,7 +479,6 @@ impl Default for Limits {
impl<T: Config> Default for InstructionWeights<T> {
fn default() -> Self {
let max_pages = Limits::default().memory_pages;
Self {
version: 4,
fallback: 0,
@@ -548,7 +493,6 @@ impl<T: Config> Default for InstructionWeights<T> {
br_table_per_entry: cost_instr!(instr_br_table_per_entry, 0),
call: cost_instr!(instr_call, 2),
call_indirect: cost_instr!(instr_call_indirect, 3),
call_indirect_per_param: cost_instr!(instr_call_indirect_per_param, 0),
call_per_local: cost_instr!(instr_call_per_local, 0),
local_get: cost_instr!(instr_local_get, 1),
local_set: cost_instr!(instr_local_set, 1),
@@ -556,7 +500,7 @@ impl<T: Config> Default for InstructionWeights<T> {
global_get: cost_instr!(instr_global_get, 1),
global_set: cost_instr!(instr_global_set, 1),
memory_current: cost_instr!(instr_memory_current, 1),
memory_grow: cost_instr_with_batch_size!(instr_memory_grow, 1, max_pages),
memory_grow: cost_instr!(instr_memory_grow, 1),
i64clz: cost_instr!(instr_i64clz, 2),
i64ctz: cost_instr!(instr_i64ctz, 2),
i64popcnt: cost_instr!(instr_i64popcnt, 2),
@@ -597,89 +541,85 @@ impl<T: Config> Default for InstructionWeights<T> {
impl<T: Config> Default for HostFnWeights<T> {
fn default() -> Self {
Self {
caller: cost_batched!(seal_caller),
is_contract: cost_batched!(seal_is_contract),
code_hash: cost_batched!(seal_code_hash),
own_code_hash: cost_batched!(seal_own_code_hash),
caller_is_origin: cost_batched!(seal_caller_is_origin),
address: cost_batched!(seal_address),
gas_left: cost_batched!(seal_gas_left),
balance: cost_batched!(seal_balance),
value_transferred: cost_batched!(seal_value_transferred),
minimum_balance: cost_batched!(seal_minimum_balance),
block_number: cost_batched!(seal_block_number),
now: cost_batched!(seal_now),
weight_to_fee: cost_batched!(seal_weight_to_fee),
caller: cost!(seal_caller),
is_contract: cost!(seal_is_contract),
code_hash: cost!(seal_code_hash),
own_code_hash: cost!(seal_own_code_hash),
caller_is_origin: cost!(seal_caller_is_origin),
address: cost!(seal_address),
gas_left: cost!(seal_gas_left),
balance: cost!(seal_balance),
value_transferred: cost!(seal_value_transferred),
minimum_balance: cost!(seal_minimum_balance),
block_number: cost!(seal_block_number),
now: cost!(seal_now),
weight_to_fee: cost!(seal_weight_to_fee),
// Manually remove proof size from basic block cost.
//
// Due to imperfect benchmarking some host functions incur a small
// amount of proof size. Usually this is ok. However, charging a basic block is such
// a frequent operation that this would be a vast overestimation.
gas: cost_batched!(seal_gas).set_proof_size(0),
input: cost_batched!(seal_input),
input_per_byte: cost_byte_batched!(seal_input_per_kb),
gas: cost!(seal_gas).set_proof_size(0),
input: cost!(seal_input),
input_per_byte: cost!(seal_input_per_byte),
r#return: cost!(seal_return),
return_per_byte: cost_byte!(seal_return_per_kb),
return_per_byte: cost!(seal_return_per_byte),
terminate: cost!(seal_terminate),
random: cost_batched!(seal_random),
deposit_event: cost_batched!(seal_deposit_event),
deposit_event_per_topic: cost_batched_args!(seal_deposit_event_per_topic_and_kb, 1, 0),
deposit_event_per_byte: cost_byte_batched_args!(
seal_deposit_event_per_topic_and_kb,
0,
1
),
debug_message: cost_batched!(seal_debug_message),
debug_message_per_byte: cost_byte!(seal_debug_message_per_kb),
set_storage: cost_batched!(seal_set_storage),
set_code_hash: cost_batched!(seal_set_code_hash),
set_storage_per_new_byte: cost_byte_batched!(seal_set_storage_per_new_kb),
set_storage_per_old_byte: cost_byte_batched!(seal_set_storage_per_old_kb),
clear_storage: cost_batched!(seal_clear_storage),
clear_storage_per_byte: cost_byte_batched!(seal_clear_storage_per_kb),
contains_storage: cost_batched!(seal_contains_storage),
contains_storage_per_byte: cost_byte_batched!(seal_contains_storage_per_kb),
get_storage: cost_batched!(seal_get_storage),
get_storage_per_byte: cost_byte_batched!(seal_get_storage_per_kb),
take_storage: cost_batched!(seal_take_storage),
take_storage_per_byte: cost_byte_batched!(seal_take_storage_per_kb),
transfer: cost_batched!(seal_transfer),
call: cost_batched!(seal_call),
delegate_call: cost_batched!(seal_delegate_call),
call_transfer_surcharge: cost_batched_args!(seal_call_per_transfer_clone_kb, 1, 0),
call_per_cloned_byte: cost_byte_batched_args!(seal_call_per_transfer_clone_kb, 0, 1),
instantiate: cost_batched!(seal_instantiate),
instantiate_transfer_surcharge: cost_batched_args!(
seal_instantiate_per_transfer_input_salt_kb,
random: cost!(seal_random),
deposit_event: cost!(seal_deposit_event),
deposit_event_per_topic: cost_args!(seal_deposit_event_per_topic_and_byte, 1, 0),
deposit_event_per_byte: cost_args!(seal_deposit_event_per_topic_and_byte, 0, 1),
debug_message: cost!(seal_debug_message),
debug_message_per_byte: cost!(seal_debug_message_per_byte),
set_storage: cost!(seal_set_storage),
set_code_hash: cost!(seal_set_code_hash),
set_storage_per_new_byte: cost!(seal_set_storage_per_new_byte),
set_storage_per_old_byte: cost!(seal_set_storage_per_old_byte),
clear_storage: cost!(seal_clear_storage),
clear_storage_per_byte: cost!(seal_clear_storage_per_byte),
contains_storage: cost!(seal_contains_storage),
contains_storage_per_byte: cost!(seal_contains_storage_per_byte),
get_storage: cost!(seal_get_storage),
get_storage_per_byte: cost!(seal_get_storage_per_byte),
take_storage: cost!(seal_take_storage),
take_storage_per_byte: cost!(seal_take_storage_per_byte),
transfer: cost!(seal_transfer),
call: cost!(seal_call),
delegate_call: cost!(seal_delegate_call),
call_transfer_surcharge: cost_args!(seal_call_per_transfer_clone_byte, 1, 0),
call_per_cloned_byte: cost_args!(seal_call_per_transfer_clone_byte, 0, 1),
instantiate: cost!(seal_instantiate),
instantiate_transfer_surcharge: cost_args!(
seal_instantiate_per_transfer_input_salt_byte,
1,
0,
0
),
instantiate_per_input_byte: cost_byte_batched_args!(
seal_instantiate_per_transfer_input_salt_kb,
instantiate_per_input_byte: cost_args!(
seal_instantiate_per_transfer_input_salt_byte,
0,
1,
0
),
instantiate_per_salt_byte: cost_byte_batched_args!(
seal_instantiate_per_transfer_input_salt_kb,
instantiate_per_salt_byte: cost_args!(
seal_instantiate_per_transfer_input_salt_byte,
0,
0,
1
),
hash_sha2_256: cost_batched!(seal_hash_sha2_256),
hash_sha2_256_per_byte: cost_byte_batched!(seal_hash_sha2_256_per_kb),
hash_keccak_256: cost_batched!(seal_hash_keccak_256),
hash_keccak_256_per_byte: cost_byte_batched!(seal_hash_keccak_256_per_kb),
hash_blake2_256: cost_batched!(seal_hash_blake2_256),
hash_blake2_256_per_byte: cost_byte_batched!(seal_hash_blake2_256_per_kb),
hash_blake2_128: cost_batched!(seal_hash_blake2_128),
hash_blake2_128_per_byte: cost_byte_batched!(seal_hash_blake2_128_per_kb),
ecdsa_recover: cost_batched!(seal_ecdsa_recover),
ecdsa_to_eth_address: cost_batched!(seal_ecdsa_to_eth_address),
reentrance_count: cost_batched!(seal_reentrance_count),
account_reentrance_count: cost_batched!(seal_account_reentrance_count),
instantiation_nonce: cost_batched!(seal_instantiation_nonce),
hash_sha2_256: cost!(seal_hash_sha2_256),
hash_sha2_256_per_byte: cost!(seal_hash_sha2_256_per_byte),
hash_keccak_256: cost!(seal_hash_keccak_256),
hash_keccak_256_per_byte: cost!(seal_hash_keccak_256_per_byte),
hash_blake2_256: cost!(seal_hash_blake2_256),
hash_blake2_256_per_byte: cost!(seal_hash_blake2_256_per_byte),
hash_blake2_128: cost!(seal_hash_blake2_128),
hash_blake2_128_per_byte: cost!(seal_hash_blake2_128_per_byte),
ecdsa_recover: cost!(seal_ecdsa_recover),
ecdsa_to_eth_address: cost!(seal_ecdsa_to_eth_address),
reentrance_count: cost!(seal_reentrance_count),
account_reentrance_count: cost!(seal_account_reentrance_count),
instantiation_nonce: cost!(seal_instantiation_nonce),
_phantom: PhantomData,
}
}
@@ -687,29 +627,12 @@ impl<T: Config> Default for HostFnWeights<T> {
struct ScheduleRules<'a, T: Config> {
schedule: &'a Schedule<T>,
params: Vec<u32>,
determinism: Determinism,
}
impl<T: Config> Schedule<T> {
pub(crate) fn rules(
&self,
module: &elements::Module,
determinism: Determinism,
) -> impl gas_metering::Rules + '_ {
ScheduleRules {
schedule: self,
params: module
.type_section()
.iter()
.flat_map(|section| section.types())
.map(|func| {
let elements::Type::Function(func) = func;
func.params().len() as u32
})
.collect(),
determinism,
}
pub(crate) fn rules(&self, determinism: Determinism) -> impl gas_metering::Rules + '_ {
ScheduleRules { schedule: self, determinism }
}
}
@@ -717,7 +640,6 @@ impl<'a, T: Config> gas_metering::Rules for ScheduleRules<'a, T> {
fn instruction_cost(&self, instruction: &elements::Instruction) -> Option<u32> {
use self::elements::Instruction::*;
let w = &self.schedule.instruction_weights;
let max_params = self.schedule.limits.parameters;
let weight = match *instruction {
End | Unreachable | Return | Else => 0,
@@ -753,7 +675,7 @@ impl<'a, T: Config> gas_metering::Rules for ScheduleRules<'a, T> {
SetGlobal(_) => w.global_set,
CurrentMemory(_) => w.memory_current,
GrowMemory(_) => w.memory_grow,
CallIndirect(idx, _) => *self.params.get(idx as usize).unwrap_or(&max_params),
CallIndirect(_, _) => w.call_indirect,
BrTable(ref data) => w
.br_table
.saturating_add(w.br_table_per_entry.saturating_mul(data.table.len() as u32)),