diff --git a/Cargo.lock b/Cargo.lock index 1c0cd9e7469a48fbca897569237ef26268cc42b9..0c68f2968b86f3d435c7feab4a5b77cc966a0397 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -909,6 +909,7 @@ dependencies = [ "sp-runtime", "sp-staking", "sp-std", + "sp-weights", ] [[package]] diff --git a/docs/dev/weights-benchmarking.md b/docs/dev/weights-benchmarking.md index 31eca28cb741c238f75c9b725a0ff3d42d66d2a9..b378b0f0196b5704006bf417cffbe971436d241b 100644 --- a/docs/dev/weights-benchmarking.md +++ b/docs/dev/weights-benchmarking.md @@ -24,16 +24,16 @@ cargo test -p <pallet> --features runtime-benchmarks ``` 3. If the benchmark tests compiles and pass, compile the binary with benchmarks on your local -machine: +machine: ``` cargo build --release --features runtime-benchmarks ``` -4. Run the benchmarks on your local machine (to test if it work with a real runtime). See 0d1232cd0d8b5809e1586b48376f8952cebc0d27 for a complete real example. The command is: +4. Run the benchmarks on your local machine (to test if it work with a real runtime). See 0d1232cd0d8b5809e1586b48376f8952cebc0d27 for a complete real example. The command is: ``` -duniter benchmark pallet --chain=CHAINSPEC --steps=50 --repeat=20 --pallet=<pallet> --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --output=./runtime/common/src/weights/ +duniter benchmark pallet --chain=CHAINSPEC --steps=50 --repeat=20 --pallet=<pallet> --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --output=./runtime/common/src/weights/ ``` 5. Use the generated file content to create the `WeightInfo` trait and the `()` dummy implementation in `pallets/<pallet>/src/weights.rs`. Then use the `WeightInfo` trait in the real code of the pallet. See 62dcc17f2c0b922e883fbc6337a9e7da97fc3218 for a complete real example. @@ -42,7 +42,7 @@ duniter benchmark pallet --chain=CHAINSPEC --steps=50 --repeat=20 --pallet=<pall 7. Use the `runtime/common/src/weights/pallet_<pallet>.rs` generated on the reference machine in the runtimes configuration. See af62a3b9cfc42d6653b3a957836f58540c18e65a for a complete real example. -Note 1: Use relevant chainspec for the benchmarks in place of `CHAINSPEC`. For example `--chain=gdev-benchmark` has already created identities that can be confirmed by pallet identity +Note 1: Use relevant chainspec for the benchmarks in place of `CHAINSPEC`. For example `--chain=gdev-benchmark` has already created identities that can be confirmed by pallet identity Note 2: If the reference machine does not support wasmtime, you should replace `--wasm-execution=compiled` by `--wasm-execution=interpreted-i-know-what-i-do`. @@ -53,11 +53,10 @@ by `--wasm-execution=interpreted-i-know-what-i-do`. 2. Run base block benchmarks command: ``` -duniter benchmark overhead --chain=gdev --execution=wasm --wasm-execution=interpreted-i-know-what-i-do --weight-path=. --warmup=10 --repeat=100 +duniter benchmark overhead --chain=gdev-benchmark --execution=wasm --wasm-execution=compiled --weight-path=./runtime/common/src/weights/ --warmup=10 --repeat=100 ``` -3. Copy the generated file `block_weights.rs` in the codebase in folder `runtime/common/src/weights/`. -4. Commit changes and open an MR. +3. Commit changes and open an MR. ## Generate storage benchmarking diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index 203a9fd6c5eb514d3ec9c45f5f4d0d59d8d817f8..4a7412e77c974df237bface000cb085222d21c18 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -58,6 +58,7 @@ std = [ 'sp-membership/std', 'sp-runtime/std', 'sp-std/std', + 'sp-weights/std', ] try-runtime = [ "frame-support/try-runtime", @@ -110,6 +111,7 @@ sp-consensus-babe = { git = 'https://github.com/duniter/substrate', branch = 'du sp-core = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.32', default-features = false } sp-runtime = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.32', default-features = false } sp-std = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.32', default-features = false } +sp-weights = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.32', default-features = false } # substrate benchmarks diff --git a/runtime/common/src/constants.rs b/runtime/common/src/constants.rs index fc3037a88beebd4757db116a097822ff588a345f..135e5bad62db4acbad6a182b07e128ca31d5aa24 100644 --- a/runtime/common/src/constants.rs +++ b/runtime/common/src/constants.rs @@ -69,7 +69,7 @@ pub fn block_weights( expected_block_weight: Weight, normal_ratio: sp_arithmetic::Perbill, ) -> frame_system::limits::BlockWeights { - let base_weight = DbWeight::get().reads(1) + DbWeight::get().writes(1); + let base_weight = crate::weights::extrinsic_weights::ExtrinsicBaseWeight::get(); let normal_weight = normal_ratio * expected_block_weight; frame_system::limits::BlockWeights::builder() .base_block(crate::weights::block_weights::BlockExecutionWeight::get()) diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index 3aa82401a4d6241deb34ac7b58c0505987abe48d..bb1315bd699dc765b0e4572c1ea37ba9c2e4c4ae 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -177,7 +177,10 @@ macro_rules! pallets_config { impl pallet_transaction_payment::Config for Runtime { type OnChargeTransaction = OneshotAccount; type OperationalFeeMultiplier = frame_support::traits::ConstU8<5>; + #[cfg(not(feature = "runtime-benchmarks"))] type WeightToFee = common_runtime::fees::WeightToFeeImpl<Balance>; + #[cfg(feature = "runtime-benchmarks")] + type WeightToFee = frame_support::weights::ConstantMultiplier::<u64, sp_core::ConstU64<0u64>>; type LengthToFee = common_runtime::fees::LengthToFeeImpl<Balance>; type FeeMultiplierUpdate = (); type RuntimeEvent = RuntimeEvent; diff --git a/runtime/common/src/weights.rs b/runtime/common/src/weights.rs index 4ba7c758564d5a17cd3c2a1a9cd81176196e8262..952423d90b95fdfacfd4f13e4faa6432b0b1e67c 100644 --- a/runtime/common/src/weights.rs +++ b/runtime/common/src/weights.rs @@ -20,6 +20,7 @@ #![allow(clippy::unnecessary_cast)] pub mod block_weights; +pub mod extrinsic_weights; pub mod frame_system; pub mod pallet_babe; pub mod pallet_balances; diff --git a/runtime/common/src/weights/block_weights.rs b/runtime/common/src/weights/block_weights.rs index 3d2ca644dd187195e6e0edcc2a8199e495f88e39..b94d3347b7bcb661440b5f58c490db835635b32c 100644 --- a/runtime/common/src/weights/block_weights.rs +++ b/runtime/common/src/weights/block_weights.rs @@ -1,81 +1,63 @@ -// Copyright 2021 Axiom-Team -// -// This file is part of Duniter-v2S. -// -// Duniter-v2S is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, version 3 of the License. -// -// Duniter-v2S is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>. //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-11-18 (Y/M/D) -//! HOSTNAME: `raspberrypi`, CPU: `ARMv7 Processor rev 3 (v7l)` +//! DATE: 2023-05-19 (Y/M/D) +//! HOSTNAME: `benjamin-xps139380`, CPU: `Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz` //! -//! SHORT-NAME: `block`, LONG-NAME: `BlockExecution`, RUNTIME: `Ğdev` +//! SHORT-NAME: `block`, LONG-NAME: `BlockExecution`, RUNTIME: `Development` //! WARMUPS: `10`, REPEAT: `100` -//! WEIGHT-PATH: `.` -//! WEIGHT-METRIC: `Average`, WEIGHT-MUL: `1`, WEIGHT-ADD: `0` +//! WEIGHT-PATH: `runtime/common/src/weights/` +//! WEIGHT-METRIC: `Average`, WEIGHT-MUL: `1.0`, WEIGHT-ADD: `0` // Executed Command: -// ./duniter +// target/release/duniter // benchmark // overhead -// --chain=gdev +// --chain=gdev-benchmark // --execution=wasm -// --wasm-execution=interpreted-i-know-what-i-do -// --weight-path=. +// --wasm-execution=compiled +// --weight-path=runtime/common/src/weights/ // --warmup=10 -// --repeat=100 -use frame_support::{ - parameter_types, - weights::{constants::WEIGHT_PER_NANOS, Weight}, -}; +use sp_core::parameter_types; +use sp_weights::{constants::WEIGHT_PER_NANOS, Weight}; parameter_types! { - /// Time to execute an empty block. - /// Calculated by multiplying the *Average* with `1` and adding `0`. - /// - /// Stats nanoseconds: - /// Min, Max: 23_866_638, 90_077_105 - /// Average: 24_871_527 - /// Median: 23_915_377 - /// Std-Dev: 6645558.32 - /// - /// Percentiles nanoseconds: - /// 99th: 30_529_787 - /// 95th: 27_134_555 - /// 75th: 23_951_395 - pub const BlockExecutionWeight: Weight = WEIGHT_PER_NANOS.saturating_mul(24_871_527); + /// Time to execute an empty block. + /// Calculated by multiplying the *Average* with `1.0` and adding `0`. + /// + /// Stats nanoseconds: + /// Min, Max: 698_202, 838_988 + /// Average: 709_232 + /// Median: 704_251 + /// Std-Dev: 15664.54 + /// + /// Percentiles nanoseconds: + /// 99th: 738_574 + /// 95th: 729_596 + /// 75th: 709_823 + pub const BlockExecutionWeight: Weight = WEIGHT_PER_NANOS.saturating_mul(709_232); } #[cfg(test)] mod test_weights { - use frame_support::weights::constants; + use sp_weights::constants; - /// Checks that the weight exists and is sane. - // NOTE: If this test fails but you are sure that the generated values are fine, - // you can delete it. - #[test] - fn sane() { - let w = super::BlockExecutionWeight::get(); + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::BlockExecutionWeight::get(); - // At least 100 µs. - assert!( - w.ref_time() >= 100u64 * constants::WEIGHT_PER_MICROS.ref_time(), - "Weight should be at least 100 µs." - ); - // At most 50 ms. - assert!( - w.ref_time() <= 50u64 * constants::WEIGHT_PER_MILLIS.ref_time(), - "Weight should be at most 50 ms." - ); - } + // At least 100 µs. + assert!( + w.ref_time() >= 100u64 * constants::WEIGHT_PER_MICROS.ref_time(), + "Weight should be at least 100 µs." + ); + // At most 50 ms. + assert!( + w.ref_time() <= 50u64 * constants::WEIGHT_PER_MILLIS.ref_time(), + "Weight should be at most 50 ms." + ); + } } diff --git a/runtime/common/src/weights/extrinsic_weights.rs b/runtime/common/src/weights/extrinsic_weights.rs new file mode 100644 index 0000000000000000000000000000000000000000..de39f7a43019778c932c81b4e6acf7c30aa4f4ac --- /dev/null +++ b/runtime/common/src/weights/extrinsic_weights.rs @@ -0,0 +1,63 @@ + +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-05-19 (Y/M/D) +//! HOSTNAME: `benjamin-xps139380`, CPU: `Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz` +//! +//! SHORT-NAME: `extrinsic`, LONG-NAME: `ExtrinsicBase`, RUNTIME: `Development` +//! WARMUPS: `10`, REPEAT: `100` +//! WEIGHT-PATH: `runtime/common/src/weights/` +//! WEIGHT-METRIC: `Average`, WEIGHT-MUL: `1.0`, WEIGHT-ADD: `0` + +// Executed Command: +// target/release/duniter +// benchmark +// overhead +// --chain=gdev-benchmark +// --execution=wasm +// --wasm-execution=compiled +// --weight-path=runtime/common/src/weights/ +// --warmup=10 + +use sp_core::parameter_types; +use sp_weights::{constants::WEIGHT_PER_NANOS, Weight}; + +parameter_types! { + /// Time to execute a NO-OP extrinsic, for example `System::remark`. + /// Calculated by multiplying the *Average* with `1.0` and adding `0`. + /// + /// Stats nanoseconds: + /// Min, Max: 203_855, 206_882 + /// Average: 204_580 + /// Median: 204_521 + /// Std-Dev: 405.03 + /// + /// Percentiles nanoseconds: + /// 99th: 205_581 + /// 95th: 205_130 + /// 75th: 204_772 + pub const ExtrinsicBaseWeight: Weight = WEIGHT_PER_NANOS.saturating_mul(204_580); +} + +#[cfg(test)] +mod test_weights { + use sp_weights::constants; + + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::ExtrinsicBaseWeight::get(); + + // At least 10 µs. + assert!( + w.ref_time() >= 10u64 * constants::WEIGHT_PER_MICROS.ref_time(), + "Weight should be at least 10 µs." + ); + // At most 1 ms. + assert!( + w.ref_time() <= constants::WEIGHT_PER_MILLIS.ref_time(), + "Weight should be at most 1 ms." + ); + } +}