From 2ddae27667c10b5ed52160b4e164413b6b4faab7 Mon Sep 17 00:00:00 2001
From: Benjamin Gallois <business@gallois.cc>
Date: Thu, 16 Nov 2023 11:43:37 +0100
Subject: [PATCH] Add benchmarks to CI (nodes/rust/duniter-v2s!188)

* fix pallet-certification benchmarks

* fix pallet-identity benchmarks

(cherry picked from commit c2dd6399a436d0dca17d5cad199e12d92826f2a1)

* ci: add benchmark tests

* fix benchmarks for g1
---
 .gitlab-ci.yml                            | 15 ++++++++++++++
 Cargo.lock                                |  1 +
 docker/Dockerfile                         | 13 +++++++++++-
 pallets/certification/src/benchmarking.rs |  1 +
 pallets/identity/src/benchmarking.rs      |  2 +-
 runtime/g1/Cargo.toml                     | 25 +++++++++++++++++++++--
 runtime/g1/src/lib.rs                     |  4 ++++
 7 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index baf3e9bc1..d9986b9df 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -185,6 +185,21 @@ build_release_tag:
     IMAGE_TAG: "$CI_COMMIT_TAG"
     PODMAN_BUILD_OPTIONS: "--platform linux/amd64"
 
+run_benchmarks:
+  stage: tests
+  extends: .docker_build
+  rules:
+    - if: $CI_COMMIT_REF_NAME =~ /^wip*$/
+      when: manual
+    - if: $CI_COMMIT_TAG
+      when: never
+    - if: '$CI_MERGE_REQUEST_ID || $CI_COMMIT_BRANCH == "master"'
+    - when: manual
+  variables:
+    IMAGE_NAME: "duniter/duniter-v2s-test"
+    IMAGE_TAG: "debug-sha-$CI_COMMIT_SHORT_SHA"
+    PODMAN_BUILD_OPTIONS: "--target build --build-arg benchmarks=1"
+
 tests:
   stage: tests
   image: rust:1-bullseye
diff --git a/Cargo.lock b/Cargo.lock
index 39852d445..18ca3f75c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3008,6 +3008,7 @@ dependencies = [
  "pallet-quota",
  "pallet-scheduler",
  "pallet-session",
+ "pallet-session-benchmarking",
  "pallet-sudo",
  "pallet-timestamp",
  "pallet-transaction-payment",
diff --git a/docker/Dockerfile b/docker/Dockerfile
index c30f042aa..bba66e6d1 100644
--- a/docker/Dockerfile
+++ b/docker/Dockerfile
@@ -33,6 +33,11 @@ RUN if [ "$debug" = 0 ]; then \
         echo "TARGET_FOLDER=debug" >>/root/dynenv; \
     fi
 
+ARG benchmarks=0
+RUN if [ "$benchmarks" != 0 ]; then \
+        echo "BENCH_OPTIONS=--features\ runtime-benchmarks" >>/root/dynenv; \
+    fi
+
 # Configure cross-build environment if need be
 RUN set -x && \
     if [ "$TARGETPLATFORM" != "$BUILDPLATFORM" ]; then \
@@ -48,7 +53,7 @@ ARG chain="gdev"
 RUN set -x && \
     cat /root/dynenv && \
     . /root/dynenv && \
-    cargo build --locked $CARGO_OPTIONS --no-default-features --features $chain --target "$RUST_ARCH_TRIPLET" && \
+    cargo build --locked $CARGO_OPTIONS --no-default-features $BENCH_OPTIONS --features $chain --target "$RUST_ARCH_TRIPLET" && \
     mkdir -p build && \
     mv target/$RUST_ARCH_TRIPLET/$TARGET_FOLDER/duniter build/
 
@@ -56,11 +61,17 @@ RUN set -x && \
 ARG cucumber=0
 RUN if [ "$cucumber" != 0 ] && [ "$TARGETPLATFORM" = "$BUILDPLATFORM" ]; then \
         cargo ta && \
+        cargo test --workspace --exclude duniter-end2end-tests --exclude duniter-live-tests --features runtime-benchmarks \
         cd target/debug/deps/ && \
         rm cucumber_tests-*.d && \
         mv cucumber_tests* ../../../build/duniter-cucumber; \
     fi
 
+# Run runtime benchmarks
+RUN if [ "$benchmarks" != 0 ]; then \
+      build/duniter benchmark pallet --chain dev --execution=wasm --wasm-execution=compiled --pallet "*" --extrinsic "*" --steps 2 --repeat 1; \
+    fi
+
 # ------------------------------------------------------------------------------
 # Final Stage
 # ------------------------------------------------------------------------------
diff --git a/pallets/certification/src/benchmarking.rs b/pallets/certification/src/benchmarking.rs
index 47fda2c72..574aaebaf 100644
--- a/pallets/certification/src/benchmarking.rs
+++ b/pallets/certification/src/benchmarking.rs
@@ -56,6 +56,7 @@ benchmarks_instance_pallet! {
         Pallet::<T, I>::del_cert(RawOrigin::Root.into(), issuer, receiver)?;
         let issuer_cert: u32 = StorageIdtyCertMeta::<T, I>::get(issuer).issued_count;
         let receiver_cert: u32 = StorageIdtyCertMeta::<T, I>::get(receiver).received_count;
+        frame_system::pallet::Pallet::<T>::set_block_number(T::CertPeriod::get());
     }: _<T::RuntimeOrigin>(caller_origin, issuer, receiver)
     verify {
         assert_has_event::<T, I>(Event::<T, I>::NewCert{ issuer: issuer, issuer_issued_count: issuer_cert + 1, receiver: receiver, receiver_received_count: receiver_cert + 1 }.into());
diff --git a/pallets/identity/src/benchmarking.rs b/pallets/identity/src/benchmarking.rs
index c3df1b542..f8c2e2665 100644
--- a/pallets/identity/src/benchmarking.rs
+++ b/pallets/identity/src/benchmarking.rs
@@ -87,7 +87,7 @@ fn create_dummy_identity<T: Config>(i: u32) -> Result<(), &'static str> {
     <Identities<T>>::insert(idty_index, value);
     IdentitiesRemovableOn::<T>::append(removable_on, (idty_index, IdtyStatus::Created));
     IdentityIndexOf::<T>::insert(owner_key.clone(), idty_index);
-    <IdentitiesNames<T>>::insert(idty_name.clone(), ());
+    <IdentitiesNames<T>>::insert(idty_name.clone(), idty_index);
     Ok(())
 }
 
diff --git a/runtime/g1/Cargo.toml b/runtime/g1/Cargo.toml
index b25a268da..db9a2377a 100644
--- a/runtime/g1/Cargo.toml
+++ b/runtime/g1/Cargo.toml
@@ -16,16 +16,36 @@ targets = ['x86_64-unknown-linux-gnu']
 [features]
 default = ['std']
 runtime-benchmarks = [
-    'frame-benchmarking',
+    'common-runtime/runtime-benchmarks',
+    'frame-benchmarking/runtime-benchmarks',
     'frame-support/runtime-benchmarks',
     'frame-system-benchmarking',
     'frame-system/runtime-benchmarks',
     'hex-literal',
+    'pallet-authority-members/runtime-benchmarks',
+    'pallet-babe/runtime-benchmarks',
     'pallet-balances/runtime-benchmarks',
+    'pallet-certification/runtime-benchmarks',
+    'pallet-collective/runtime-benchmarks',
+    'pallet-duniter-test-parameters/runtime-benchmarks',
+    'pallet-duniter-account/runtime-benchmarks',
+    'pallet-duniter-wot/runtime-benchmarks',
+    'pallet-grandpa/runtime-benchmarks',
     'pallet-identity/runtime-benchmarks',
+    'pallet-membership/runtime-benchmarks',
+    'pallet-provide-randomness/runtime-benchmarks',
+    'pallet-im-online/runtime-benchmarks',
+    'pallet-multisig/runtime-benchmarks',
+    'pallet-oneshot-account/runtime-benchmarks',
+    'pallet-preimage/runtime-benchmarks',
+    'pallet-session-benchmarking/runtime-benchmarks',
+    'pallet-proxy/runtime-benchmarks',
+    'pallet-scheduler/runtime-benchmarks',
+    'pallet-timestamp/runtime-benchmarks',
     'pallet-treasury/runtime-benchmarks',
     'pallet-universal-dividend/runtime-benchmarks',
-    'common-runtime/runtime-benchmarks',
+    'pallet-upgrade-origin/runtime-benchmarks',
+    'pallet-utility/runtime-benchmarks',
     'sp-runtime/runtime-benchmarks',
 ]
 std = [
@@ -155,6 +175,7 @@ pallet-preimage = { git = 'https://github.com/duniter/substrate', branch = 'duni
 pallet-proxy = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false }
 pallet-scheduler = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false }
 pallet-session = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false }
+pallet-session-benchmarking = { path = '../../pallets/session-benchmarking', default-features = false }
 pallet-sudo = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false }
 pallet-timestamp = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false }
 pallet-transaction-payment = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false }
diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs
index 321e5cd0f..d4315c779 100644
--- a/runtime/g1/src/lib.rs
+++ b/runtime/g1/src/lib.rs
@@ -22,6 +22,10 @@
 #[cfg(feature = "std")]
 include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
 
+#[cfg(feature = "runtime-benchmarks")]
+#[macro_use]
+extern crate frame_benchmarking;
+
 pub mod parameters;
 
 pub use self::parameters::*;
-- 
GitLab