|
1 |
| -# Github composite action to report on code size changes |
| 1 | +# Github composite action to report on code size changes across different |
| 2 | +# platforms. |
| 3 | + |
2 | 4 | name: Report binary size changes on PR
|
3 | 5 | description: |
|
4 |
| - Report on code size changes resulting from a PR as a comment on the PR |
5 |
| - (accessed via context). |
| 6 | + Report on code size changes across different platforms resulting from a PR. |
| 7 | + The only input argument is the path to a directory containing a set of |
| 8 | + "*.json" files (extension required), each file containing the keys: |
| 9 | +
|
| 10 | + - platform: the platform that the code size change was measured on |
| 11 | + - reference: the size in bytes of the reference binary (base of PR) |
| 12 | + - updated: the size in bytes of the updated binary (head of PR) |
| 13 | +
|
| 14 | + The size is reported as a comment on the PR (accessed via context). |
6 | 15 | inputs:
|
7 |
| - reference: |
8 |
| - description: The size in bytes of the reference binary (base of PR). |
9 |
| - required: true |
10 |
| - updated: |
11 |
| - description: The size in bytes of the updated binary (head of PR). |
| 16 | + data-directory: |
| 17 | + description: > |
| 18 | + Path to directory containing size data as a set of "*.json" files. |
12 | 19 | required: true
|
13 | 20 | runs:
|
14 | 21 | using: composite
|
15 | 22 | steps:
|
16 | 23 | - name: Post a PR comment if the size has changed
|
17 | 24 | uses: actions/github-script@v6
|
18 | 25 | env:
|
19 |
| - SIZE_REFERENCE: ${{ inputs.reference }} |
20 |
| - SIZE_UPDATED: ${{ inputs.updated }} |
| 26 | + DATA_DIRECTORY: ${{ inputs.data-directory }} |
21 | 27 | with:
|
22 | 28 | script: |
|
23 |
| - const reference = process.env.SIZE_REFERENCE; |
24 |
| - const updated = process.env.SIZE_UPDATED; |
| 29 | + const fs = require("fs"); |
25 | 30 |
|
26 |
| - if (!(reference > 0)) { |
27 |
| - core.setFailed(`Reference size invalid: ${reference}`); |
28 |
| - return; |
29 |
| - } |
| 31 | + const size_dir = process.env.DATA_DIRECTORY; |
30 | 32 |
|
31 |
| - if (!(updated > 0)) { |
32 |
| - core.setFailed(`Updated size invalid: ${updated}`); |
33 |
| - return; |
34 |
| - } |
| 33 | + // Map the set of all the *.json files into an array of objects. |
| 34 | + const globber = await glob.create(`${size_dir}/*.json`); |
| 35 | + const files = await globber.glob(); |
| 36 | + const sizes = files.map(path => { |
| 37 | + const contents = fs.readFileSync(path); |
| 38 | + return JSON.parse(contents); |
| 39 | + }); |
| 40 | +
|
| 41 | + // Map each object into some text, but only if it shows any difference |
| 42 | + // to report. |
| 43 | + const size_reports = sizes.flatMap(size_data => { |
| 44 | + const platform = size_data["platform"]; |
| 45 | + const reference = size_data["reference"]; |
| 46 | + const updated = size_data["updated"]; |
| 47 | +
|
| 48 | + if (!(reference > 0)) { |
| 49 | + core.setFailed(`Reference size invalid: ${reference}`); |
| 50 | + return; |
| 51 | + } |
| 52 | +
|
| 53 | + if (!(updated > 0)) { |
| 54 | + core.setFailed(`Updated size invalid: ${updated}`); |
| 55 | + return; |
| 56 | + } |
| 57 | +
|
| 58 | + const formatter = Intl.NumberFormat("en", { |
| 59 | + useGrouping: "always" |
| 60 | + }); |
| 61 | +
|
| 62 | + const updated_str = formatter.format(updated); |
| 63 | + const reference_str = formatter.format(reference); |
| 64 | +
|
| 65 | + const diff = updated - reference; |
| 66 | + const diff_pct = (updated / reference) - 1; |
| 67 | +
|
| 68 | + const diff_str = Intl.NumberFormat("en", { |
| 69 | + useGrouping: "always", |
| 70 | + sign: "exceptZero" |
| 71 | + }).format(diff); |
35 | 72 |
|
36 |
| - const formatter = Intl.NumberFormat("en", {useGrouping: "always"}); |
| 73 | + const diff_pct_str = Intl.NumberFormat("en", { |
| 74 | + style: "percent", |
| 75 | + useGrouping: "always", |
| 76 | + sign: "exceptZero", |
| 77 | + maximumFractionDigits: 2 |
| 78 | + }).format(diff_pct); |
37 | 79 |
|
38 |
| - const updated_str = formatter.format(updated); |
39 |
| - const reference_str = formatter.format(reference); |
| 80 | + if (diff !== 0) { |
| 81 | + // The body is created here and wrapped so "weirdly" to avoid whitespace at the start of the lines, |
| 82 | + // which is interpreted as a code block by Markdown. |
| 83 | + const report = `On platform \`${platform}\`: |
40 | 84 |
|
41 |
| - const diff = updated - reference; |
42 |
| - const diff_pct = (updated / reference) - 1; |
| 85 | + - Original binary size: **${reference_str} B** |
| 86 | + - Updated binary size: **${updated_str} B** |
| 87 | + - Difference: **${diff_str} B** (${diff_pct_str}) |
43 | 88 |
|
44 |
| - const diff_str = Intl.NumberFormat("en", { |
45 |
| - useGrouping: "always", |
46 |
| - sign: "exceptZero" |
47 |
| - }).format(diff); |
| 89 | + `; |
48 | 90 |
|
49 |
| - const diff_pct_str = Intl.NumberFormat("en", { |
50 |
| - style: "percent", |
51 |
| - useGrouping: "always", |
52 |
| - sign: "exceptZero", |
53 |
| - maximumFractionDigits: 2 |
54 |
| - }).format(diff_pct); |
| 91 | + return [report]; |
| 92 | + } else { |
| 93 | + return []; |
| 94 | + } |
| 95 | + }); |
55 | 96 |
|
56 |
| - if (diff !== 0) { |
57 |
| - // The body is created here and wrapped so "weirdly" to avoid whitespace at the start of the lines, |
58 |
| - // which is interpreted as a code block by Markdown. |
59 |
| - const body = `Below is the size of a hello-world Rust program linked with libstd with backtrace. |
| 97 | + // If there are any size changes to report, format a comment and post |
| 98 | + // it. |
| 99 | + if (size_reports.length > 0) { |
| 100 | + const comment_sizes = size_reports.join(""); |
| 101 | + const body = `Code size changes for a hello-world Rust program linked with libstd with backtrace: |
60 | 102 |
|
61 |
| - Original binary size: **${reference_str} B** |
62 |
| - Updated binary size: **${updated_str} B** |
63 |
| - Difference: **${diff_str} B** (${diff_pct_str})`; |
| 103 | + ${comment_sizes}`; |
64 | 104 |
|
65 | 105 | github.rest.issues.createComment({
|
66 | 106 | issue_number: context.issue.number,
|
67 | 107 | owner: context.repo.owner,
|
68 | 108 | repo: context.repo.repo,
|
69 | 109 | body
|
70 |
| - }) |
| 110 | + }); |
71 | 111 | }
|
0 commit comments