Skip to content

Commit

Permalink
fix benchmarking (polkadot-evm#605)
Browse files Browse the repository at this point in the history
* fix benchmarking

* remove old file

* rename variable back
  • Loading branch information
nbaztec committed Mar 17, 2022
1 parent 088f261 commit b9f2f33
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion frame/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
evm = { version = "0.33.1", default-features = false, features = ["with-codec"] }
hex = { version = "0.4", default-features = false }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
log = { version = "0.4", default-features = false }
primitive-types = { version = "0.10.0", default-features = false, features = ["rlp", "byteorder"] }
rlp = { version = "0.5", default-features = false }
Expand Down
29 changes: 11 additions & 18 deletions frame/evm/src/benchmarks.rs → frame/evm/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,26 @@

#![cfg(feature = "runtime-benchmarks")]

//! Benchmarking
use frame_benchmarking::benchmarks;
use rlp::RlpStream;
use sha3::{Digest, Keccak256};
use sp_core::{H160, U256};
use sp_std::prelude::*;

use crate::{runner::Runner, Config, Pallet};

#[cfg(test)]
fn new_test_ext() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::default()
.build_storage::<crate::mock::Test>()
.unwrap();
sp_io::TestExternalities::new(t)
}
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};

use super::*;

benchmarks! {

runner_execute {
// This benchmark tests the relationship between gas and weight. It deploys a contract which
// has an infinite loop in a public function. We then call this function with varying amounts
// of gas, expecting it to OOG. The benchmarking framework measures the amount of time (aka
// weight) it takes before OOGing and relates that to the amount of gas provided, leaving us
// with an estimate for gas-to-weight mapping.
runner_execute {

let x in 1..10000000;

use frame_benchmarking::vec;
use rlp::RlpStream;
use sha3::{Digest, Keccak256};
use sp_core::{H160, U256};

// contract bytecode below is for:
//
// pragma solidity >=0.8.0;
Expand Down Expand Up @@ -128,5 +120,6 @@ benchmarks! {
);
assert_eq!(call_runner_results.is_ok(), true, "call() failed");
}
impl_benchmark_test_suite!(Pallet, self::new_test_ext(), crate::mock::Test);
}

impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test);
6 changes: 3 additions & 3 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(any(test, feature = "runtime-benchmarks"))]
pub mod benchmarks;
#[cfg(any(test, feature = "runtime-benchmarks"))]
pub mod benchmarking;

#[cfg(test)]
mod mock;
pub mod runner;
#[cfg(test)]
Expand Down
27 changes: 25 additions & 2 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ pub type Executive = frame_executive::Executive<
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPallets,
AllPalletsWithSystem,
>;

impl fp_self_contained::SelfContainedCall for Call {
Expand Down Expand Up @@ -490,6 +490,15 @@ impl fp_self_contained::SelfContainedCall for Call {
}
}

#[cfg(feature = "runtime-benchmarks")]
#[macro_use]
extern crate frame_benchmarking;

#[cfg(feature = "runtime-benchmarks")]
mod benches {
define_benchmarks!([pallet_evm, EVM]);
}

impl_runtime_apis! {
impl sp_api::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
Expand Down Expand Up @@ -766,11 +775,25 @@ impl_runtime_apis! {

#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn benchmark_metadata(extra: bool) -> (
Vec<frame_benchmarking::BenchmarkList>,
Vec<frame_support::traits::StorageInfo>,
) {
use frame_benchmarking::{Benchmarking, BenchmarkList};
use frame_support::traits::StorageInfoTrait;

let mut list = Vec::<BenchmarkList>::new();
list_benchmarks!(list, extra);

let storage_info = AllPalletsWithSystem::storage_info();
return (list, storage_info)
}

fn dispatch_benchmark(
config: frame_benchmarking::BenchmarkConfig
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};
use pallet_evm::Module as PalletEvmBench;
use pallet_evm::Pallet as PalletEvmBench;
impl frame_system_benchmarking::Config for Runtime {}

let whitelist: Vec<TrackedStorageKey> = vec![];
Expand Down

0 comments on commit b9f2f33

Please sign in to comment.