Bench Mako PR #8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Bench Mako PR | |
on: | |
workflow_dispatch: | |
inputs: | |
prNumber: | |
description: "PR number" | |
required: true | |
type: string | |
jobs: | |
create-comment: | |
runs-on: ubuntu-latest | |
outputs: | |
comment-id: ${{ steps.create-comment.outputs.result }} | |
steps: | |
- id: create-comment | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.MAKO_BOT_ACCESS_TOKEN }} | |
result-encoding: string | |
script: | | |
const url = `${context.serverUrl}//${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}` | |
const urlLink = `[Open](${url})` | |
const { data: comment } = await github.rest.issues.createComment({ | |
issue_number: context.payload.inputs.prNumber, | |
owner: context.repo.owner, | |
repo: 'mako', | |
body: `⏳ Triggered benchmark: ${urlLink}` | |
}) | |
return comment.id | |
bench: | |
runs-on: ubuntu-latest | |
needs: [create-comment] | |
strategy: | |
matrix: | |
shardIndex: [1] | |
shardTotal: [1] | |
fail-fast: false | |
outputs: | |
diff-result: ${{ steps.print-results.outputs.diff-result }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Init env | |
uses: ./.github/actions/env | |
- uses: ./.github/actions/build-mako | |
with: | |
path: ${{ env.MAKO_DIR }} | |
- name: Setup cargo cache | |
uses: actions/cache@v3 | |
with: | |
# ref: https://doc.rust-lang.org/cargo/guide/cargo-home.html#caching-the-cargo-home-in-ci | |
path: | | |
~/.cargo/.crates.toml | |
~/.cargo/.crates2.json | |
~/.cargo/bin/ | |
~/.cargo/registry/index/ | |
~/.cargo/registry/cache/ | |
~/.cargo/git/db/ | |
target/ | |
key: cargo-store-${{ hashFiles('**/Cargo.lock') }} | |
- name: Run benchmark | |
run: cargo bench --bench with_examples -- --output-format bencher | tee output.txt | |
- name: Store benchmark result | |
uses: benchmark-action/github-action-benchmark@v1 | |
with: | |
name: examples | |
tool: 'cargo' | |
output-file-path: output.txt | |
github-token: ${{ secrets.MAKO_BOT_ACCESS_TOKEN }} | |
alert-threshold: '150%' | |
external-data-json-path: ./benchmark-data/benchmark-data.json | |
summary-always: true | |
fail-on-alert: true | |
comment-on-alert: true | |
- name: Comment PR | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.MAKO_BOT_ACCESS_TOKEN }} | |
script: | | |
const { owner, repo } = context.repo; | |
const prNumber = context.payload.pull_request.number; | |
const cacheIdFile = `./comment-id`; | |
let data = ''; | |
await exec.exec('node', ['-e', ` | |
const fs = require('fs'); | |
const path = require('path'); | |
const summaryDir = path.dirname('${process.env.GITHUB_STEP_SUMMARY}'); | |
let summary; | |
// read benchmark summary from prev step | |
fs.readdirSync(summaryDir) | |
.filter(file => file.startsWith('step_summary_')) | |
.some(file => { | |
const content = fs.readFileSync(path.join(summaryDir, file), 'utf-8'); | |
if (content.includes('Benchmark')) { | |
summary = content | |
// add hyperlink for commit hash | |
.replace( | |
/((?:Current|Previous): )"(.{8})(.+?)"/g, | |
'$1<a href="../tree/$2$3"><code>$2</code></a>', | |
) | |
// convert ns to ms for duration time | |
.replace( | |
/\`(\\d+)\`\\s+ns|\`(±\\s+)(\\d+)\`/g, | |
(_, d1, s, d2) => \`\${s || ''}\${((d2 || d1)/10e6).toFixed(2)} ms\`, | |
); | |
return true; | |
} | |
}); | |
// read cached comment id | |
const commentId = fs.existsSync('${cacheIdFile}') ? fs.readFileSync('${cacheIdFile}', 'utf-8') : null; | |
console.log(JSON.stringify({ | |
summary, | |
commentId, | |
})); | |
`], { | |
listeners: { | |
stdout: (chunk) => { | |
data += chunk; | |
}, | |
}, | |
}); | |
data = JSON.parse(data); | |
const { summary, commentId } = data; | |
if (commentId) { | |
await github.rest.issues.updateComment({ | |
owner, | |
repo, | |
comment_id: commentId, | |
body: summary, | |
}); | |
} else { | |
const { data: { id } } = await github.rest.issues.createComment({ | |
owner, | |
repo, | |
issue_number: prNumber, | |
body: summary, | |
}); | |
await exec.exec('node', ['-e', `require('fs').writeFileSync('${cacheIdFile}', '${id}', 'utf-8')`]); | |
} |