Skip to content

Commit

Permalink
fix: posting canbench results from forked repos (#209)
Browse files Browse the repository at this point in the history
Note that canbench didn't comment with the benchmark results in this PR.
Due to a quirk with Github actions, that will happen for subsequent PRs
once this is merged.
  • Loading branch information
ielashi authored Mar 27, 2024
1 parent fba63ad commit 5aa5ffb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
32 changes: 16 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ jobs:
benchmark:
runs-on: ubuntu-latest
needs: build
permissions:
# To allow writing comments to the PR in forked repositorites.
# See https://github.com/thollander/actions-comment-pull-request/issues/181
pull-requests: write

env:
PROJECT_DIR: .
steps:
Expand All @@ -93,16 +88,13 @@ jobs:
ref: main
path: _canbench_main_branch

- name: Cache Cargo
uses: actions/cache@v2
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ matrix.build }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ matrix.build }}-cargo-
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-1

- name: Install Rust
run: |
Expand All @@ -112,17 +104,25 @@ jobs:
- name: Benchmark
run: |
bash .github/workflows/canbench_ci_run_benchmark.sh $PROJECT_DIR
bash ./scripts/ci_run_benchmark.sh $PROJECT_DIR ${{ github.job }}
- uses: actions/upload-artifact@v4
with:
name: canbench_result_${{github.job}}
path: /tmp/canbench_result_${{ github.job }}

- name: Save PR number
run: |
echo ${{ github.event.number }} > /tmp/pr_number
- name: Post comment
uses: thollander/actions-comment-pull-request@v2
- uses: actions/upload-artifact@v4
with:
filePath: /tmp/canbench_comment_message.txt
comment_tag: ${{ env.PROJECT_DIR }}
name: pr_number
path: /tmp/pr_number

- name: Pass or fail
run: |
bash .github/workflows/canbench_ci_post_run_benchmark.sh
bash ./scripts/ci_post_run_benchmark.sh
checks-pass:
# Always run this job!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ json_array+="]"
# Output the benchmarks and PR number to be used by the next job.
echo "matrix={\"benchmark\": $json_array}" >> "$GITHUB_OUTPUT"
echo "pr_number=$(cat ./pr_number/pr_number)" >> "$GITHUB_OUTPUT"

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ set -Eexuo pipefail
# Path to run `canbench` from.
CANISTER_PATH=$1

# The name of the job in CI
CANBENCH_JOB_NAME=$2

# Must match the file specified in the github action.
COMMENT_MESSAGE_PATH=/tmp/canbench_comment_message.txt
COMMENT_MESSAGE_PATH=/tmp/canbench_result_${CANBENCH_JOB_NAME}

# Github CI is expected to have the main branch checked out in this folder.
MAIN_BRANCH_DIR=_canbench_main_branch
Expand Down Expand Up @@ -45,7 +48,7 @@ fi
popd


echo "# \`canbench\` 🏋" > $COMMENT_MESSAGE_PATH
echo "# \`canbench\` 🏋 (dir: $CANISTER_PATH)" > "$COMMENT_MESSAGE_PATH"

# Detect if there are performance changes relative to the main branch.
if [ -f "$MAIN_BRANCH_RESULTS_FILE" ]; then
Expand All @@ -54,23 +57,15 @@ if [ -f "$MAIN_BRANCH_RESULTS_FILE" ]; then

# Run canbench to compare result to main branch.
pushd "$CANISTER_PATH"
set +e
canbench --less-verbose > $CANBENCH_OUTPUT
RES=$?
set -e
canbench --less-verbose > "$CANBENCH_OUTPUT"
popd

if [ "$RES" -eq 0 ]; then
if grep -q "(regress\|(improved by" "${CANBENCH_OUTPUT}"; then
echo "**Significant performance change detected! ⚠️**
" >> $COMMENT_MESSAGE_PATH;
else
echo "**No significant performance changes detected ✅**
" >> $COMMENT_MESSAGE_PATH
fi
if grep -q "(regress\|(improved by" "${CANBENCH_OUTPUT}"; then
echo "**Significant performance change detected! ⚠️**
" >> "$COMMENT_MESSAGE_PATH"
else
echo "Failed to run \`canbench\` against main branch ⚠️
" >> $COMMENT_MESSAGE_PATH
echo "**No significant performance changes detected ✅**
" >> "$COMMENT_MESSAGE_PATH"
fi
fi

Expand All @@ -81,7 +76,7 @@ fi
echo "\`\`\`"
cat "$CANBENCH_OUTPUT"
echo "\`\`\`"
} >> $COMMENT_MESSAGE_PATH
} >> "$COMMENT_MESSAGE_PATH"

# Output the comment to stdout.
cat $COMMENT_MESSAGE_PATH
cat "$COMMENT_MESSAGE_PATH"

0 comments on commit 5aa5ffb

Please sign in to comment.