Skip to content

Commit

Permalink
added bench command
Browse files Browse the repository at this point in the history
  • Loading branch information
Bullrich committed Jun 10, 2024
1 parent c1a0666 commit 56e1566
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 0 deletions.
108 changes: 108 additions & 0 deletions .github/workflows/command-bench.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: Command Bench

on:
workflow_dispatch:
inputs:
pr:
description: Number of the Pull Request
required: true
benchmark:
description: Pallet benchmark
type: choice
required: true
options:
- substrate-pallet
- polkadot-pallet
- cumulus-assets
- cumulus-collectives
- cumulus-coretime
- cumulus-bridge-hubs
- cumulus-contracts
- cumulus-glutton
- cumulus-starters
- cumulus-people
- cumulus-testing
subcommand:
description: Subcommand
type: choice
required: true
options:
- pallet
- xcm
runtime:
description: Runtime
type: choice
options:
- dev
- rococo
- westend
- asset-hub-westend
- asset-hub-rococo
- collectives-westend
- coretime-rococo
- coretime-westend
- bridge-hub-rococo
- bridge-hub-westend
- contracts-rococo
- glutton-westend
- glutton-westend-dev-1300
- seedling
- shell
- people-westend
- people-rococo
- penpal
- rococo-parachain
pallet:
description: Pallet
type: string
default: pallet_name
target_dir:
description: Target directory
type: choice
options:
- substrate
- polkadot
- cumulus

jobs:
set-image:
runs-on: ubuntu-latest
outputs:
IMAGE: ${{ steps.set_image.outputs.IMAGE }}
steps:
- name: Checkout
uses: actions/checkout@v4
- id: set_image
run: cat .github/env >> $GITHUB_OUTPUT
cmd-bench:
needs: [set-image]
runs-on: ubuntu-latest
container:
image: ${{ needs.set-image.outputs.IMAGE }}
steps:
- name: Download repo
uses: actions/checkout@v4
- name: Install gh cli
id: gh
uses: ./.github/actions/set-up-gh
with:
pr-number: ${{ inputs.pr }}
GH_TOKEN: ${{ github.token }}
- name: Run bench
run: |
"./scripts/bench.sh" "${{ inputs.benchmark }}" --runtime "${{ inputs.runtime }}" --pallet "${{ inputs.pallet }}" --target_dir "${{ inputs.target_dir }}" --subcommand "${{ inputs.subcommand }}"
- name: Report failure
if: ${{ failure() }}
run: gh pr comment ${{ inputs.pr }} --body "## Command failed ❌\n\nRun by @${{ github.actor }} for <code>${{ github.workflow }}</code> failed. See logs <a href=\"$RUN\">here</a>."
env:
RUN: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
GH_TOKEN: ${{ github.token }}
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: cmd-action - ${{ github.workflow }}
branch: ${{ steps.gh.outputs.branch }}
- name: Report succeed
run: gh pr comment ${{ inputs.pr }} --body "## Action completed 🎉🎉\n\nRun by @${{ github.actor }} for <code>${{ github.workflow }}</code> completed 🎉. See logs <a href=\"$RUN\">here</a>."
env:
RUN: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
GH_TOKEN: ${{ github.token }}
116 changes: 116 additions & 0 deletions scripts/bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/bash
# Initially based on https://github.com/paritytech/bench-bot/blob/cd3b2943d911ae29e41fe6204788ef99c19412c3/bench.js

# Most external variables used in this script, such as $GH_CONTRIBUTOR, are
# related to https://github.com/paritytech/try-runtime-bot

# This script relies on $GITHUB_TOKEN which is probably a protected GitLab CI
# variable; if this assumption holds true, it is implied that this script should
# be ran only on protected pipelines

set -eu -o pipefail
shopt -s inherit_errexit

# realpath allows to reuse the current
BENCH_ROOT_DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")")

. "$(realpath "$(dirname "${BASH_SOURCE[0]}")/command-utils.sh")"

repository_name="$(basename "$PWD")"

get_arg optional --target_dir "$@"
target_dir="${out:-""}"

get_arg optional --noexit "$@"
noexit="${out:-""}"

output_path="."

profile="production"

if [[ "$repository_name" == "polkadot-sdk" ]]; then
output_path="./$target_dir"
fi

cargo_run_benchmarks="cargo run --quiet --profile=${profile}"

echo "Repository: $repository_name"
echo "Target Dir: $target_dir"
echo "Output Path: $output_path"

cargo_run() {
echo "Running $cargo_run_benchmarks" "${args[@]}"

# if not patched with PATCH_something=123 then use --locked
if [[ -z "${BENCH_PATCHED:-}" ]]; then
cargo_run_benchmarks+=" --locked"
fi

$cargo_run_benchmarks "${args[@]}"
}


main() {

# Remove the "github" remote since the same repository might be reused by a
# GitLab runner, therefore the remote might already exist from a previous run
# in case it was not cleaned up properly for some reason
&>/dev/null git remote remove github || :

tmp_dirs=()
cleanup() {
exit_code=$?
# Clean up the "github" remote at the end since it contains the
# $GITHUB_TOKEN secret, which is only available for protected pipelines on
# GitLab
&>/dev/null git remote remove github || :
rm -rf "${tmp_dirs[@]}"
echo "Done, exit: $exit_code"
exit $exit_code
}

# avoid exit if --noexit is passed
if [ -z "$noexit" ]; then
trap cleanup EXIT
fi

# set -x

get_arg required --subcommand "$@"
local subcommand="${out:-""}"

case "$subcommand" in
runtime|pallet|xcm)
echo 'Running bench_pallet'
. "$BENCH_ROOT_DIR/lib/bench-pallet.sh" "$@"
;;
overhead)
echo 'Running bench_overhead'
. "$BENCH_ROOT_DIR/lib/bench-overhead.sh" "$@"
;;
all)
echo "Running all-$target_dir"
. "$BENCH_ROOT_DIR/lib/bench-all-${target_dir}.sh" "$@"
;;
*)
die "Invalid subcommand $subcommand to process_args"
;;
esac

# set +x

# in case we used diener to patch some dependency during benchmark execution,
# revert the patches so that they're not included in the diff
git checkout --quiet HEAD Cargo.toml

# Save the generated weights to GitLab artifacts in case commit+push fails
echo "Showing weights diff for command"
git diff -P | tee -a "${ARTIFACTS_DIR}/weights.patch"
echo "Wrote weights patch to \"${ARTIFACTS_DIR}/weights.patch\""


# instead of using `cargo run --locked`, we allow the Cargo files to be updated
# but avoid committing them. It is so `cmd_runner_apply_patches` can work
git restore --staged Cargo.*

main "$@"

0 comments on commit 56e1566

Please sign in to comment.