Skip to content

Commit

Permalink
feat(benchmark): create workflow for generating benchmark weights (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
renauter authored Feb 22, 2024
1 parent f5262a5 commit a0d30d0
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 11 deletions.
74 changes: 74 additions & 0 deletions .github/workflows/060_generate_benchmark_weights.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Generate benchmark weights

on:
workflow_dispatch:

jobs:
generate-benchmark-weights:
runs-on: [self-hosted, tfchainrunner01]
container:
image: threefolddev/tfchain:4
env:
DEBIAN_FRONTEND: noninteractive
PATH: /root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin
RUSTUP_HOME: /root/.rustup
CARGO_HOME: /root/.cargo

steps:
- name: Fail if branch is main
if: github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/development'
run: |
echo "This workflow should not be triggered with workflow_dispatch on main branch"
exit 1
- name: Checkout the repo
uses: actions/checkout@v3

- name: Chown user
run: |
chown -R $USER:$USER $GITHUB_WORKSPACE
- name: Cache build
uses: actions/cache@v3
timeout-minutes: 6
continue-on-error: true
with:
path: |
/root/.cargo/bin/
/root/.cargo/registry/index/
/root/.cargo/registry/cache/
/root/.cargo/git/db/
substrate-node/target/
key: ${{ runner.os }}-tfchain-build-cache-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-tfchain-build-cache

- name: Build
run: |
cd substrate-node
cargo build --profile=production --features runtime-benchmarks
- name: Run benchmarking
run: |
cd substrate-node
for weights_rs_file in ./pallets/*/src/weights.rs
do
rm $weights_rs_file
pal_name=$(awk -F'pallets/|/src' '{print $2}' <<< $weights_rs_file)
./target/production/tfchain benchmark pallet \
--chain=dev \
--wasm-execution=compiled \
--pallet="$pal_name" \
--extrinsic="*" \
--steps=50 \
--repeat=20 \
--heap-pages=409 \
--output ./pallets/"$pal_name"/src/weights.rs \
--template ./.maintain/frame-weight-template.hbs
done
- name: Commit & Push changes
uses: actions-js/push@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
message: 'chore(pallets): update benchmark `weights.rs` files ${date}'
branch: ${{ github.ref_name }}
77 changes: 77 additions & 0 deletions .github/workflows/080_check_benchmark_weights.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Check benchmark weights

on:
push:
branches-ignore:
- development
paths:
- '**.rs'
- 'substrate-node/pallets/**'

jobs:
check-benchmark-weights:
runs-on: ubuntu-22.04

steps:
- name: Checkout the repo
uses: actions/checkout@v3

- name: Get all rust files that have changed in pallets
id: pallets-changed-rust-files
uses: tj-actions/changed-files@v42
with:
files: |
substrate-node/pallets/**/src/*.rs
- name: Get all pallets with changes in src dir
id: pallets-changed-src-dir
uses: tj-actions/changed-files@v42
with:
dir_names: "true"
files: |
substrate-node/pallets/**/src/*.rs
- name: List all changed files
env:
ALL_CHANGED_FILES: ${{ steps.pallets-changed-rust-files.outputs.all_changed_files }}
run: |
for file in ${ALL_CHANGED_FILES}; do
echo "$file file was changed"
done
- name: List all changed dir
env:
ALL_CHANGED_DIR: ${{ steps.pallets-changed-src-dir.outputs.all_changed_files }}
run: |
for dir in ${ALL_CHANGED_DIR}; do
echo "$dir has changes"
done
- name: Run benchmarking
env:
ALL_CHANGED_PALLETS_SRC_DIR: ${{ steps.pallets-changed-src-dir.outputs.all_changed_files }}
ALL_CHANGED_PALLETS_FILES: ${{ steps.pallets-changed-rust-files.outputs.all_changed_files }}
run: |
count=0
for pallet_src_dir in ${ALL_CHANGED_PALLETS_SRC_DIR}; do
echo "pallet src dir: $pallet_src_dir"
weights_file="$pallet_src_dir"/weights.rs
echo "weights file: $weights_file"
updated_weights=false
for changed_file in ${ALL_CHANGED_PALLETS_FILES}; do
if [ "$changed_file" = "$weights_file" ]; then
updated_weights=true
break
fi
done
if [ "$updated_weights" = false ] ; then
let "count=count+1"
fi
done
if [ "$count" -gt 0 ]; then
echo "Found changes on src rust file(s) for $count pallet(s) and respective weights.rs file(s) was not updated."
echo "Make sure to generate these files again if pallet logic has changed by running generate_benchmark_weights workflow on branch."
exit 1
else
echo "Found changes on src rust file(s) and respective weights.rs file(s) was updated."
fi
18 changes: 9 additions & 9 deletions substrate-node/.maintain/benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@
echo "starting benchmark"

echo "generating pallet-burning"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_burning --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-burning/src/weights.rs --template ./frame-weight-template.hbs"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_burning --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-burning/src/weights.rs --template ./frame-weight-template.hbs"
$cmd

echo "generating pallet-dao"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_dao --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-dao/src/weights.rs --template ./frame-weight-template.hbs"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_dao --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-dao/src/weights.rs --template ./frame-weight-template.hbs"
$cmd

echo "generating pallet-kvstore"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_kvstore --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-kvstore/src/weights.rs --template ./frame-weight-template.hbs"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_kvstore --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-kvstore/src/weights.rs --template ./frame-weight-template.hbs"
$cmd

echo "generating pallet-smart-contract"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_smart_contract --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-smart-contract/src/weights.rs --template ./frame-weight-template.hbs"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_smart_contract --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-smart-contract/src/weights.rs --template ./frame-weight-template.hbs"
$cmd

echo "generating pallet-tfgrid weights"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tfgrid --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-tfgrid/src/weights.rs --template ./frame-weight-template.hbs"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tfgrid --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-tfgrid/src/weights.rs --template ./frame-weight-template.hbs"
$cmd

echo "generating pallet-tft-bridge"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tft_bridge --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-tft-bridge/src/weights.rs --template ./frame-weight-template.hbs"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tft_bridge --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-tft-bridge/src/weights.rs --template ./frame-weight-template.hbs"
$cmd

echo "generating pallet-tft-price"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tft_price --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-tft-price/src/weights.rs --template ./frame-weight-template.hbs"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tft_price --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-tft-price/src/weights.rs --template ./frame-weight-template.hbs"
$cmd

echo "generating pallet-validator"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_validator --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-validator/src/weights.rs --template ./frame-weight-template.hbs"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_validator --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-validator/src/weights.rs --template ./frame-weight-template.hbs"
$cmd

echo "generating substrate-validator-set"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=validatorset --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/substrate-validator-set/src/weights.rs --template ./frame-weight-template.hbs"
cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=substrate_validator_set --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/substrate-validator-set/src/weights.rs --template ./frame-weight-template.hbs"
$cmd
10 changes: 10 additions & 0 deletions substrate-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,13 @@ sp-timestamp = {git = "https://github.com/paritytech/substrate", branch = "polka
substrate-build-script-utils = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0"}
substrate-frame-rpc-system = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0"}
substrate-prometheus-endpoint = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0"}

[profile.production]
inherits = "release"

# Sacrifice compile speed for execution speed by using optimization flags:

# https://doc.rust-lang.org/rustc/linker-plugin-lto.html
lto = "fat"
# https://doc.rust-lang.org/rustc/codegen-options/index.html#codegen-units
codegen-units = 1
4 changes: 3 additions & 1 deletion substrate-node/pallets/pallet-runtime-upgrade/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use pallet::*;
pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use frame_system::weights::WeightInfo;
use sp_std::vec::Vec;

#[pallet::pallet]
Expand All @@ -16,13 +17,14 @@ pub mod pallet {
pub trait Config: frame_system::Config {
/// Origin for runtime upgrades
type SetCodeOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type WeightInfo: WeightInfo;
}

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
// Give same weight as set_code() wrapped extrinsic from frame_system
#[pallet::weight((T::BlockWeights::get().base_block, DispatchClass::Operational))]
#[pallet::weight((<T as Config>::WeightInfo::set_code(), DispatchClass::Operational))]
pub fn set_code(origin: OriginFor<T>, code: Vec<u8>) -> DispatchResultWithPostInfo {
T::SetCodeOrigin::ensure_origin(origin)?;
frame_system::Pallet::<T>::set_code(frame_system::RawOrigin::Root.into(), code)?;
Expand Down
3 changes: 2 additions & 1 deletion substrate-node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ type EnsureRootOrCouncilApproval = EitherOfDiverse<

impl pallet_runtime_upgrade::Config for Runtime {
type SetCodeOrigin = EnsureRootOrCouncilApproval;
type WeightInfo = ();
}

pub struct AuraAccountAdapter;
Expand Down Expand Up @@ -805,7 +806,7 @@ mod benches {
[pallet_burning, BurningModule]
[pallet_dao, Dao]
[pallet_kvstore, TFKVStore]
[validatorset, ValidatorSet]
[substrate_validator_set, ValidatorSet]
[pallet_validator, Validator]
[pallet_tft_bridge, TFTBridgeModule]
// Substrate
Expand Down

0 comments on commit a0d30d0

Please sign in to comment.