-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modularise CI workflow and validate outputs for binary size checks
From detly/master in #549
- Loading branch information
Showing
3 changed files
with
279 additions
and
52 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Github composite action to build a single-source-file test binary with an | ||
# already-checked-out version of Rust's stdlib, that will be patched with a | ||
# given revision of the backtrace crate. | ||
|
||
name: Build with patched std | ||
description: > | ||
Build a binary with a version of std that's had a specific revision of | ||
backtrace patched in. | ||
inputs: | ||
backtrace-commit: | ||
description: The git commit of backtrace to patch in to std | ||
required: true | ||
main-rs: | ||
description: The (single) source code file to compile | ||
required: true | ||
rustc-dir: | ||
description: The root directory of the rustc repo | ||
required: true | ||
outputs: | ||
test-binary-size: | ||
description: The size in bytes of the built test binary | ||
value: ${{ steps.measure.outputs.test-binary-size }} | ||
runs: | ||
using: composite | ||
steps: | ||
- shell: bash | ||
id: measure | ||
env: | ||
RUSTC_FLAGS: -Copt-level=3 -Cstrip=symbols | ||
# This symlink is made by Build::new() in the bootstrap crate, using a | ||
# symlink on Linux and a junction on Windows, so it will exist on both | ||
# platforms. | ||
RUSTC_BUILD_DIR: build/host | ||
working-directory: ${{ inputs.rustc-dir }} | ||
run: | | ||
rm -rf "$RUSTC_BUILD_DIR/stage0-std" | ||
(cd library/backtrace && git checkout ${{ inputs.backtrace-commit }}) | ||
git add library/backtrace | ||
python3 x.py build library --stage 0 | ||
TEMP_BUILD_OUTPUT=$(mktemp test-binary-XXXXXXXX) | ||
"$RUSTC_BUILD_DIR/stage0-sysroot/bin/rustc" $RUSTC_FLAGS "${{ inputs.main-rs }}" -o "$TEMP_BUILD_OUTPUT" | ||
BINARY_SIZE=$(stat -c '%s' "$TEMP_BUILD_OUTPUT") | ||
rm "$TEMP_BUILD_OUTPUT" | ||
echo "test-binary-size=$BINARY_SIZE" >> "$GITHUB_OUTPUT" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Github composite action to report on code size changes across different | ||
# platforms. | ||
|
||
name: Report binary size changes on PR | ||
description: | | ||
Report on code size changes across different platforms resulting from a PR. | ||
The only input argument is the path to a directory containing a set of | ||
"*.json" files (extension required), each file containing the keys: | ||
- platform: the platform that the code size change was measured on | ||
- reference: the size in bytes of the reference binary (base of PR) | ||
- updated: the size in bytes of the updated binary (head of PR) | ||
The size is reported as a comment on the PR (accessed via context). | ||
inputs: | ||
data-directory: | ||
description: > | ||
Path to directory containing size data as a set of "*.json" files. | ||
required: true | ||
runs: | ||
using: composite | ||
steps: | ||
- name: Post a PR comment if the size has changed | ||
uses: actions/github-script@v6 | ||
env: | ||
DATA_DIRECTORY: ${{ inputs.data-directory }} | ||
with: | ||
script: | | ||
const fs = require("fs"); | ||
const size_dir = process.env.DATA_DIRECTORY; | ||
// Map the set of all the *.json files into an array of objects. | ||
const globber = await glob.create(`${size_dir}/*.json`); | ||
const files = await globber.glob(); | ||
const sizes = files.map(path => { | ||
const contents = fs.readFileSync(path); | ||
return JSON.parse(contents); | ||
}); | ||
// Map each object into some text, but only if it shows any difference | ||
// to report. | ||
const size_reports = sizes.flatMap(size_data => { | ||
const platform = size_data["platform"]; | ||
const reference = size_data["reference"]; | ||
const updated = size_data["updated"]; | ||
if (!(reference > 0)) { | ||
core.setFailed(`Reference size invalid: ${reference}`); | ||
return; | ||
} | ||
if (!(updated > 0)) { | ||
core.setFailed(`Updated size invalid: ${updated}`); | ||
return; | ||
} | ||
const formatter = Intl.NumberFormat("en", { | ||
useGrouping: "always" | ||
}); | ||
const updated_str = formatter.format(updated); | ||
const reference_str = formatter.format(reference); | ||
const diff = updated - reference; | ||
const diff_pct = (updated / reference) - 1; | ||
const diff_str = Intl.NumberFormat("en", { | ||
useGrouping: "always", | ||
sign: "exceptZero" | ||
}).format(diff); | ||
const diff_pct_str = Intl.NumberFormat("en", { | ||
style: "percent", | ||
useGrouping: "always", | ||
sign: "exceptZero", | ||
maximumFractionDigits: 2 | ||
}).format(diff_pct); | ||
if (diff !== 0) { | ||
// The body is created here and wrapped so "weirdly" to avoid whitespace at the start of the lines, | ||
// which is interpreted as a code block by Markdown. | ||
const report = `On platform \`${platform}\`: | ||
- Original binary size: **${reference_str} B** | ||
- Updated binary size: **${updated_str} B** | ||
- Difference: **${diff_str} B** (${diff_pct_str}) | ||
`; | ||
return [report]; | ||
} else { | ||
return []; | ||
} | ||
}); | ||
// If there are any size changes to report, format a comment and post | ||
// it. | ||
if (size_reports.length > 0) { | ||
const comment_sizes = size_reports.join(""); | ||
const body = `Code size changes for a hello-world Rust program linked with libstd with backtrace: | ||
${comment_sizes}`; | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body | ||
}); | ||
} |
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