Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CICD] Trigger workflow on label and dispatch #207

Merged
merged 39 commits into from
Jul 12, 2021
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
28cb85e
Trigger workflow on label and dispatch
sea212 Jun 28, 2021
d93f5ac
Distinguish between different outcomes
sea212 Jun 28, 2021
60ab466
Fix syntax error
sea212 Jun 28, 2021
d51bb2e
Fix invalid step condition
sea212 Jun 28, 2021
011cb8f
Fix invalid symbol
sea212 Jun 28, 2021
ee1d6bf
Add status comment
sea212 Jun 29, 2021
b9b52d5
Add error message (test)
sea212 Jun 29, 2021
f5d13fe
Move exit action at the end
sea212 Jun 29, 2021
ac22f24
Fix invalid GH token
sea212 Jun 29, 2021
9a16fbe
Integrate label mechanism into actual benchmarks
sea212 Jun 29, 2021
75b69d0
Add proper benchmark result message
sea212 Jun 29, 2021
af6dd88
Test new ref fetch on PR
sea212 Jun 29, 2021
299fc6f
Removed comments
sea212 Jun 29, 2021
f67b2d0
Replace local chain with dev chain
sea212 Jun 29, 2021
3f2f14d
Update weights
sea212 Jun 29, 2021
c888217
Include complete benchmark output
sea212 Jun 29, 2021
efc2520
Test multiline error output
sea212 Jun 29, 2021
d7c4699
Update weights
sea212 Jun 29, 2021
e74e78e
Remove invalid local keyword
sea212 Jun 29, 2021
bffe112
Remove invalid local keyword
sea212 Jun 29, 2021
1ff2436
Test successful run output
sea212 Jun 29, 2021
7ed41d0
Fix output formatting error
sea212 Jun 30, 2021
19ce03a
Update weights
sea212 Jun 29, 2021
a08805a
Update weights
sea212 Jun 30, 2021
cf84580
Test multiline result text
sea212 Jun 30, 2021
01c133c
Test multiline result text
sea212 Jun 30, 2021
518e514
Test multiline result text
sea212 Jun 30, 2021
9f60ce2
Test multiline result text
sea212 Jun 30, 2021
e8c936d
Test multiline result text
sea212 Jun 30, 2021
88cbcde
Shorten test text
sea212 Jun 30, 2021
853a526
Move HTML into output variable
sea212 Jun 30, 2021
d915c51
Add function to construct html message
sea212 Jun 30, 2021
583f2c2
Comment out line
sea212 Jun 30, 2021
4716d8b
Replace test output with real output
sea212 Jun 30, 2021
8b64488
Uncomment actual benchmark command
sea212 Jun 30, 2021
65e910c
Activate benchmark error detection
sea212 Jun 30, 2021
ae33782
Update weights
sea212 Jun 30, 2021
1ab6bd7
Reset weights
sea212 Jun 30, 2021
6d344b9
Minor changes in step names
sea212 Jun 30, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 123 additions & 14 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
name: Benchmark weights

on:
push:
branches: [ stage-benchmark ]
workflow_dispatch:
pull_request:
types: [ labeled ]

env:
CARGO_TERM_COLOR: always

jobs:
benchmark:
benchmark:
name: Benchmark weights
if: (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 's:benchmark-required')) || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- name: Checkout repository
# Ensure that at most one benchmark worklow runs across all workflows
- name: Turnstyle
uses: softprops/turnstyle@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Checkout repository (PR)
uses: actions/checkout@v2
if: (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 's:benchmark-required'))
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Checkout repository (branch)
uses: actions/checkout@v2
if: github.event_name == 'workflow_dispatch'

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
Expand All @@ -34,34 +50,64 @@ jobs:
run: cargo build --release --features runtime-benchmarks

- name: Run benchmarks
id: run_benchmarks
run: |
# define benchmark commands
ZG_BENCHMARK_COMMANDS=(
"./target/release/zeitgeist benchmark --chain local --execution wasm --wasm-execution compiled --pallet zrml-orderbook-v1 --extrinsic '*' --steps 0 --repeat 1000 --template ./misc/weight_template.hbs --output ./zrml/orderbook-v1/src/weights.rs"
"./target/release/zeitgeist benchmark --chain local --execution wasm --wasm-execution compiled --pallet zrml-prediction-markets --extrinsic '*' --steps 10 --repeat 1000 --template ./misc/weight_template.hbs --output ./zrml/prediction-markets/src/weights.rs"
"./target/release/zeitgeist benchmark --chain local --execution wasm --wasm-execution compiled --pallet zrml-swaps --extrinsic '*' --steps 9 --repeat 1000 --template ./misc/weight_template.hbs --output ./zrml/swaps/src/weights.rs"
"./target/release/zeitgeist benchmark --dev --execution wasm --wasm-execution compiled --heap-pages=4096 --pallet zrml-orderbook-v1 --extrinsic '*' --steps 0 --repeat 1000 --template ./misc/weight_template.hbs --output ./zrml/orderbook-v1/src/weights.rs"
"./target/release/zeitgeist benchmark --dev --execution wasm --wasm-execution compiled --heap-pages=4096 --pallet zrml-prediction-markets --extrinsic '*' --steps 10 --repeat 1000 --template ./misc/weight_template.hbs --output ./zrml/prediction-markets/src/weights.rs"
"./target/release/zeitgeist benchmark --dev --execution wasm --wasm-execution compiled --heap-pages=4096 --pallet zrml-swaps --extrinsic '*' --steps 9 --repeat 1000 --template ./misc/weight_template.hbs --output ./zrml/swaps/src/weights.rs"
)

echo "Fetching commit hash of HEAD"
zg_cmt=`git log -n 1 --format="%H"`
echo "Commit hash: ${zg_cmt}"

# Takes variable name as parameter $1
prepare_output() {
# Add result box that contains contents of parameter $1
local txt_proc="<details>
<summary>
Results
</summary>

${!1}

</details>"

# Prepare output for github output format
txt_proc="${txt_proc//'%'/'%25'}"
txt_proc="${txt_proc//$'\n'/'%0A'}"
txt_proc="${txt_proc//$'\r'/'%0D'}"

eval "$1=\"${txt_proc}\""
}

check_if_new_commits() {
echo "Fetching latest repository state"
git fetch

if [ $? -ne 0 ]; then
echo "ERROR: git fetch failed. Aborting benchmarks."
exit $?
ERROR="ERROR: git fetch failed. Aborting benchmarks."
prepare_output ERROR
echo "::set-output name=ERROR::$ERROR"
exit 0
fi

echo "Determining if the branch contains new updates."
local zg_cur_cmt=`git log -n 1 --format="%H" origin/${GITHUB_REF##*/}`

if [ ${{ github.event_name }} == 'pull_request' ]; then
local zg_cur_cmt=`git log -n 1 --format="%H" origin/${{ github.event.pull_request.head.ref }}`
else
local zg_cur_cmt=`git log -n 1 --format="%H" origin/${GITHUB_REF##*/}`
fi

if [ "$zg_cmt" != "$zg_cur_cmt" ]; then
echo "${zg_cmt} != ${zg_cur_cmt}"
echo "ERROR: Branch contains new commits. Aborting benchmarks."
exit 31
ERROR="ERROR: Branch contains new commits. Aborting benchmarks."
prepare_output ERROR
echo "::set-output name=ERROR::$ERROR"
exit 0
fi
}

Expand All @@ -72,15 +118,78 @@ jobs:
for zg_bench_cmd in "${ZG_BENCHMARK_COMMANDS[@]}"; do
# Execute benchmark
echo "Executing: ${zg_bench_cmd}"
echo "$zg_bench_cmd" | sh
echo "${zg_bench_cmd} | tee --append benchmark_log.txt" | sh 2> benchmark_errors.txt
ERROR=$(<benchmark_errors.txt)

if [ -n "$ERROR" ]; then
# Prepare error for github output format
prepare_output ERROR
echo "::set-output name=ERROR::$ERROR"
exit 0
fi

check_if_new_commits
done

BENCHMARK_LOG=$(sed -E "s/(Writes\s=\s[0-9]+)/\1\n/g" benchmark_log.txt | sed "s/========/--------/g")
prepare_output BENCHMARK_LOG
echo "::set-output name=RESULT::$BENCHMARK_LOG"

- name: Commit updated weights
uses: stefanzweifel/git-auto-commit-action@v4
if: steps.run_benchmarks.outputs.ERROR == ''
with:
commit_message: Update weights
commit_options: '--no-verify'
file_pattern: zrml/*/src/weights.rs
commit_author: zeitgeist-benchmark-bot <zeitgeist-benchmark-bot@no-reply.zeitgeist.pm>
commit_user_name: Zeitgeist Benchmark Bot
commit_user_email: zeitgeist-benchmark-bot@no-reply.zeitgeist.pm
status_options: '--untracked-files=no'

- name: Remove label s:benchmark-required
if: github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 's:benchmark-required')
uses: buildsville/add-remove-label@v1
with:
token: ${{secrets.GITHUB_TOKEN}}
label: s:benchmark-required
type: remove

- name: Add label s:benchmark-done
if: (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 's:benchmark-required')) && steps.run_benchmarks.outputs.ERROR == ''
uses: buildsville/add-remove-label@v1
with:
token: ${{secrets.GITHUB_TOKEN}}
label: s:benchmark-done
type: add

- name: Add label s:benchmark-aborted
if: (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 's:benchmark-required')) && steps.run_benchmarks.outputs.ERROR != ''
uses: buildsville/add-remove-label@v1
with:
token: ${{secrets.GITHUB_TOKEN}}
label: s:benchmark-aborted
type: add

- name: Comment PR (on success)
uses: thollander/actions-comment-pull-request@master
if: (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 's:benchmark-required')) && steps.run_benchmarks.outputs.ERROR == ''
with:
message: 'Benchmark completed successfully.

${{ steps.run_benchmarks.outputs.RESULT }}'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Comment PR (on failure)
uses: thollander/actions-comment-pull-request@master
if: (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 's:benchmark-required')) && steps.run_benchmarks.outputs.ERROR != ''
with:
message: 'Benchmark aborted due to an error.

${{ steps.run_benchmarks.outputs.ERROR }}'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Exit with error message
if: steps.run_benchmarks.outputs.ERROR != ''
run: |
echo "${{ steps.run_benchmarks.outputs.ERROR }}"
exit 1