From fc5dc072345b5f43304ff48cef375347e787424f Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Mon, 16 Jan 2023 10:06:34 +0100 Subject: [PATCH 1/2] update deps --- Cargo.dev.toml | 3 +++ Makefile | 4 +++- bencher/Cargo.toml | 10 +++++----- bencher/src/bench_runner.rs | 8 ++------ bencher/src/bencher.rs | 10 +++++----- bencher/src/handler.rs | 11 +++++------ bencher/src/lib.rs | 2 +- bencher/src/macros.rs | 2 +- bencher/src/utils.rs | 12 ++++++++++++ bencher/test/Cargo.toml | 7 ++----- bencher/test/src/benches.rs | 8 +------- bencher/test/src/lib.rs | 6 +++--- bencher/test/src/mock.rs | 2 +- bencher/test/src/weights.rs | 29 ++++++++++++++++++++++------- weight-meter/Cargo.toml | 2 ++ 15 files changed, 68 insertions(+), 48 deletions(-) diff --git a/Cargo.dev.toml b/Cargo.dev.toml index e8d806294..8b612d7f2 100644 --- a/Cargo.dev.toml +++ b/Cargo.dev.toml @@ -4,6 +4,7 @@ members = [ "auction", "authority", "bencher", + "bencher/test", "benchmarking", "currencies", "gradually-update", @@ -26,6 +27,8 @@ members = [ "payments" ] +exclude = ["bencher/test"] + resolver = "2" [profile.dev] diff --git a/Makefile b/Makefile index 51dfe8fe8..c57206900 100644 --- a/Makefile +++ b/Makefile @@ -44,9 +44,11 @@ dev-check: Cargo.toml check dev-check-tests: Cargo.toml cargo check --tests --all + cargo check --tests --features=bench --package=orml-weight-meter --package=orml-bencher-test dev-test: Cargo.toml - cargo test --all --features runtime-benchmarks + cargo test --all --features=runtime-benchmarks + cargo test --features=bench --package=orml-weight-meter --package=orml-bencher-test # run benchmarks via Acala node benchmark-all: diff --git a/bencher/Cargo.toml b/bencher/Cargo.toml index c18a02313..e61bf3fc1 100644 --- a/bencher/Cargo.toml +++ b/bencher/Cargo.toml @@ -10,15 +10,15 @@ edition = "2021" [dependencies] paste = "1.0.7" build-helper = { version = "0.1.1", optional = true } -cargo_metadata = { version = "0.14.1", optional = true } +cargo_metadata = { version = "0.15.2", optional = true } tempfile = { version = "3.2.0", optional = true } toml = { version = "0.5.8", optional = true } walkdir = { version = "2.3.1", optional = true } ansi_term = { version = "0.12.1", optional = true } wasm-gc-api = { version = "0.1.11", optional = true } rand = {version = "0.8.3", optional = true } -linregress = { version = "0.4.4", optional = true } -parking_lot = { version = "0.12.0", optional = true } +linregress = { version = "0.5.0", optional = true } +parking_lot = { version = "0.12.1", optional = true } thiserror = { version = "1.0", optional = true } serde = { version = "1.0.136", optional = true, features = ['derive'] } serde_json = {version = "1.0.68", optional = true } @@ -35,7 +35,7 @@ sc-executor = { git = "https://github.com/paritytech/substrate", default-feature sc-executor-common = { git = "https://github.com/paritytech/substrate", optional = true , branch = "polkadot-v0.9.36" } sc-client-db = { git = "https://github.com/paritytech/substrate", default-features = false, features = ["rocksdb"], optional = true , branch = "polkadot-v0.9.36" } sp-maybe-compressed-blob = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true , branch = "polkadot-v0.9.36" } -frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.36" } +frame-support = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.36" } sp-externalities = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.36" } sp-storage = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true , branch = "polkadot-v0.9.36" } @@ -67,7 +67,7 @@ std = [ "sc-executor-common", "sc-client-db", "sp-maybe-compressed-blob", - "frame-benchmarking/std", + "frame-support/std", "sp-externalities/std", "sp-storage/std", ] diff --git a/bencher/src/bench_runner.rs b/bencher/src/bench_runner.rs index b48246515..1906c1494 100644 --- a/bencher/src/bench_runner.rs +++ b/bencher/src/bench_runner.rs @@ -2,18 +2,14 @@ use super::{ bench_ext::BenchExt, tracker::{BenchTracker, BenchTrackerExt}, }; -use frame_benchmarking::frame_support::sp_runtime::traits::Block; +use frame_support::sp_runtime::traits::Block; use sc_executor::{WasmExecutionMethod, WasmExecutor, WasmtimeInstantiationStrategy}; use sc_executor_common::runtime_blob::RuntimeBlob; use sp_externalities::Extensions; use sp_state_machine::{Ext, OverlayedChanges, StorageTransactionCache}; use sp_std::sync::Arc; -type ComposeHostFunctions = ( - sp_io::SubstrateHostFunctions, - frame_benchmarking::benchmarking::HostFunctions, - super::bench::HostFunctions, -); +type ComposeHostFunctions = (sp_io::SubstrateHostFunctions, super::bench::HostFunctions); /// Run benches pub fn run(wasm_code: Vec) -> std::result::Result, sc_executor_common::error::Error> { diff --git a/bencher/src/bencher.rs b/bencher/src/bencher.rs index bfbc210a4..3ac197f2b 100644 --- a/bencher/src/bencher.rs +++ b/bencher/src/bencher.rs @@ -40,8 +40,8 @@ impl Bencher { pub fn before_run(&self) { #[cfg(not(feature = "std"))] { - frame_benchmarking::benchmarking::commit_db(); - frame_benchmarking::benchmarking::wipe_db(); + crate::bench::commit_db(); + crate::bench::wipe_db(); } } @@ -56,8 +56,8 @@ impl Bencher { { #[cfg(not(feature = "std"))] { - frame_benchmarking::benchmarking::commit_db(); - frame_benchmarking::benchmarking::reset_read_write_count(); + crate::bench::commit_db(); + crate::bench::reset_read_write_count(); crate::bench::start_timer(); } @@ -68,7 +68,7 @@ impl Bencher { let elapsed = crate::bench::end_timer().saturating_sub(crate::bench::redundant_time()); self.current.elapses.push(elapsed); - frame_benchmarking::benchmarking::commit_db(); + crate::bench::commit_db(); // changed keys self.current.keys = crate::bench::read_written_keys(); diff --git a/bencher/src/handler.rs b/bencher/src/handler.rs index f754b947d..0e7f3b49f 100644 --- a/bencher/src/handler.rs +++ b/bencher/src/handler.rs @@ -3,7 +3,7 @@ use crate::{ BenchResult, }; use codec::Decode; -use frame_benchmarking::frame_support::traits::StorageInfo; +use frame_support::traits::StorageInfo; use linregress::{FormulaRegressionBuilder, RegressionDataBuilder}; use serde::{Deserialize, Serialize}; use sp_core::hexdisplay::HexDisplay; @@ -66,14 +66,13 @@ pub fn handle(output: Vec, storage_infos: Vec) { comments.sort(); + let intercepted_value = model.parameters()[0] as u64; + println!( "{} {:<40} {:>20} storage: {:<20}", green_bold("Bench"), cyan(&name), - green_bold(&format!( - "{:?}", - Duration::from_nanos(model.parameters.intercept_value as u64) - )), + green_bold(&format!("{:?}", Duration::from_nanos(intercepted_value))), green_bold(&format!( "[r: {}, w: {}]", &total_reads.to_string(), @@ -83,7 +82,7 @@ pub fn handle(output: Vec, storage_infos: Vec) { BenchData { name, - weight: model.parameters.intercept_value as u64 * 1_000, + weight: intercepted_value * 1_000, reads: total_reads, writes: total_writes, comments, diff --git a/bencher/src/lib.rs b/bencher/src/lib.rs index e7c18d486..824248fa6 100644 --- a/bencher/src/lib.rs +++ b/bencher/src/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] #[doc(hidden)] -pub extern crate frame_benchmarking; +pub extern crate frame_support; #[doc(hidden)] pub extern crate paste; #[doc(hidden)] diff --git a/bencher/src/macros.rs b/bencher/src/macros.rs index f307e7617..4945b0046 100644 --- a/bencher/src/macros.rs +++ b/bencher/src/macros.rs @@ -125,7 +125,7 @@ macro_rules! run_benches { ) => { #[cfg(all(feature = "std", feature = "bench"))] pub fn main() -> std::io::Result<()> { - use $crate::frame_benchmarking::frame_support::traits::StorageInfoTrait; + use $crate::frame_support::traits::StorageInfoTrait; let wasm = $crate::build_wasm::build()?; let storage_info = $all_pallets_with_system::storage_info(); match $crate::bench_runner::run::<$block>(wasm) { diff --git a/bencher/src/utils.rs b/bencher/src/utils.rs index 4b0ee5379..f46f58e17 100644 --- a/bencher/src/utils.rs +++ b/bencher/src/utils.rs @@ -36,6 +36,18 @@ pub trait Bench { println!("{}", msg); } + fn commit_db(&mut self) { + self.commit() + } + + fn wipe_db(&mut self) { + self.wipe() + } + + fn reset_read_write_count(&mut self) { + self.reset_read_write_count() + } + fn start_timer(&mut self) { let tracker = &***self .extension::() diff --git a/bencher/test/Cargo.toml b/bencher/test/Cargo.toml index af39d8675..f4705f3b6 100644 --- a/bencher/test/Cargo.toml +++ b/bencher/test/Cargo.toml @@ -32,18 +32,15 @@ std = [ "serde", "scale-info/std", "codec/std", - "orml-bencher/std", "frame-support/std", "frame-system/std", "sp-runtime/std", - "sp-std/std", "sp-core/std", + "sp-std/std", + "orml-bencher/std", "orml-weight-meter/std", ] bench = [ "orml-bencher/bench", "orml-weight-meter/bench", - "frame-support/runtime-benchmarks", - "sp-runtime/runtime-benchmarks", - "frame-system/runtime-benchmarks", ] diff --git a/bencher/test/src/benches.rs b/bencher/test/src/benches.rs index dbd98cc7e..62ac88231 100644 --- a/bencher/test/src/benches.rs +++ b/bencher/test/src/benches.rs @@ -42,10 +42,4 @@ fn whitelist(b: &mut Bencher) { }); } -benches!( - whitelist, - set_value, - set_foo, - remove_all_bar, - remove_all_bar_with_limit -); +benches!(whitelist, set_value, set_foo, remove_all_bar, remove_all_bar_with_limit); diff --git a/bencher/test/src/lib.rs b/bencher/test/src/lib.rs index 7881e2ea7..16c6ff7db 100644 --- a/bencher/test/src/lib.rs +++ b/bencher/test/src/lib.rs @@ -40,7 +40,7 @@ pub mod pallet { impl Pallet { #[pallet::call_index(0)] #[pallet::weight(0)] - #[orml_weight_meter::start(ModuleWeights::::set_value())] + #[orml_weight_meter::start(ModuleWeights::::set_value().ref_time())] pub fn set_value(origin: OriginFor, n: u32) -> DispatchResultWithPostInfo { let _sender = frame_system::ensure_signed(origin)?; Value::::get(); @@ -60,7 +60,7 @@ pub mod pallet { } impl Pallet { - #[orml_weight_meter::weight(ModuleWeights::::set_foo())] + #[orml_weight_meter::weight(ModuleWeights::::set_foo().ref_time())] pub(crate) fn set_foo() -> frame_support::dispatch::DispatchResult { Value::::put(2); @@ -87,4 +87,4 @@ pub mod pallet { _ = Bar::::clear(10, None); } } -} \ No newline at end of file +} diff --git a/bencher/test/src/mock.rs b/bencher/test/src/mock.rs index 54d84fcd2..af2ae2532 100644 --- a/bencher/test/src/mock.rs +++ b/bencher/test/src/mock.rs @@ -16,7 +16,7 @@ pub type Header = sp_runtime::generic::Header; pub type SignedExtra = (frame_system::CheckWeight,); -pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic; +pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic; pub type Block = sp_runtime::generic::Block; diff --git a/bencher/test/src/weights.rs b/bencher/test/src/weights.rs index bc946b082..8882be9cf 100644 --- a/bencher/test/src/weights.rs +++ b/bencher/test/src/weights.rs @@ -10,13 +10,21 @@ use sp_std::marker::PhantomData; pub struct ModuleWeights(PhantomData); impl ModuleWeights { + // Storage access info + // + // Test::Bar (r: 0, w: 1) + pub fn whitelist() -> Weight { + Weight::from_ref_time(5_356_000) + .saturating_add(T::DbWeight::get().writes(1)) + } // Storage access info // // Test::Value (r: 1, w: 1) + // Unknown 0x3a7472616e73616374696f6e5f6c6576656c3a (r: 1, w: 1) pub fn set_value() -> Weight { - (5_236_000 as Weight) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().writes(1 as Weight)) + Weight::from_ref_time(3_919_000) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(2)) } // Storage access info // @@ -24,13 +32,20 @@ impl ModuleWeights { // Test::Foo (r: 0, w: 1) // Test::Value (r: 0, w: 1) pub fn set_foo() -> Weight { - (13_274_000 as Weight) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().writes(4 as Weight)) + Weight::from_ref_time(5_133_000) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(4)) } // Storage access info // pub fn remove_all_bar() -> Weight { - (3_449_000 as Weight) + Weight::from_ref_time(1_533_000) + } + // Storage access info + // + // Test::Bar (r: 0, w: 10) + pub fn remove_all_bar_with_limit() -> Weight { + Weight::from_ref_time(1_600_000) + .saturating_add(T::DbWeight::get().writes(10)) } } diff --git a/weight-meter/Cargo.toml b/weight-meter/Cargo.toml index a6b76278c..7f9d38ca7 100644 --- a/weight-meter/Cargo.toml +++ b/weight-meter/Cargo.toml @@ -24,6 +24,8 @@ frame-support = { git = "https://github.com/paritytech/substrate", branch = "pol frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } +orml-bencher = { path = "../bencher" } + [features] default = ["std"] std = [ From aa7bc0cfd2f21680c8dc702389498cb9b842dcf6 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Mon, 16 Jan 2023 10:33:54 +0100 Subject: [PATCH 2/2] clippy --- bencher/test/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bencher/test/src/lib.rs b/bencher/test/src/lib.rs index 16c6ff7db..892e9231f 100644 --- a/bencher/test/src/lib.rs +++ b/bencher/test/src/lib.rs @@ -42,7 +42,7 @@ pub mod pallet { #[pallet::weight(0)] #[orml_weight_meter::start(ModuleWeights::::set_value().ref_time())] pub fn set_value(origin: OriginFor, n: u32) -> DispatchResultWithPostInfo { - let _sender = frame_system::ensure_signed(origin)?; + frame_system::ensure_signed(origin)?; Value::::get(); Value::::put(n); Value::::put(n + 1); @@ -53,7 +53,7 @@ pub mod pallet { #[pallet::call_index(1)] #[pallet::weight(0)] pub fn dummy(origin: OriginFor, _n: u32) -> DispatchResult { - let _sender = frame_system::ensure_none(origin)?; + frame_system::ensure_none(origin)?; Foo::::put(1); Ok(()) }