diff --git a/.github/workflows/linux-ci-rust.yml b/.github/workflows/linux-ci-rust.yml index ff9f902cd2a..68eb219622e 100644 --- a/.github/workflows/linux-ci-rust.yml +++ b/.github/workflows/linux-ci-rust.yml @@ -97,9 +97,8 @@ jobs: - name: Run tests in WASM run: tools/rust-test wasm - upload-binary-sizes: + check-binary-sizes: runs-on: ubuntu-latest - if: github.event_name == 'push' steps: - uses: actions/checkout@v3 - uses: actions-rs/toolchain@v1 @@ -109,67 +108,45 @@ jobs: - name: Install toolchain run: | rustup target add armv7-linux-androideabi + - name: Build binaries run: | cargo build --target armv7-linux-androideabi --release working-directory: rust-test + - name: Generate release report run: | ./tools/release-size measure-rust > release-report.json + - name: Upload release report uses: actions/upload-artifact@v2 with: - name: release_report + name: release_report_1 path: release-report.json - check-binary-sizes: - runs-on: ubuntu-latest - if: github.event_name == 'pull_request' - steps: - - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - # TODO remove this step - - name: Install toolchain - run: | - rustup target add armv7-linux-androideabi - - name: Build binaries - run: | - cargo build --target armv7-linux-androideabi --release - working-directory: rust-test - - name: Generate release report - run: | - ./tools/release-size measure-rust > release-report.json - + # Download previous release report, compare the release binary sizes and post/update a comment in the Pull Request. - name: Download previous release report - id: download_prev_report + if: github.event_name == 'pull_request' uses: dawidd6/action-download-artifact@v3 with: commit: ${{github.event.pull_request.base.sha}} path: previous if_no_artifact_found: warn # Same artifact name as at the "Upload release report" step. - name: release_report + name: release_report_1 - name: Craft Comment Body - if: steps.download_prev_artifact.outputs.found_artifact == 'true' - id: craft_body + if: github.event_name == 'pull_request' run: | - before_report=./previous/release-report.json - after_report=./release-report.json - markdown_diff=$(./tools/release-size compare $before_report $after_report) - echo "markdown_body=$markdown_diff" >> $GITHUB_OUTPUT + # Please note `previous/release-report.json` may not exist if the previous report was not found. + ./tools/release-size compare previous/release-report.json release-report.json | + > report-diff.md - name: Create or Update Comment uses: edumserrano/find-create-or-update-comment@v2 - if: steps.download_prev_artifact.outputs.found_artifact == 'true' with: issue-number: ${{ github.event.pull_request.number }} body-includes: "Binary size comparison" comment-author: 'github-actions[bot]' edit-mode: replace - body: | - ## Binary size comparison - - ${{ steps.craft_body.outputs.markdown_body }} + body-path: 'report-diff.md' diff --git a/rust-test/Cargo.lock b/rust-test/Cargo.lock index 55fe896cc0e..358730e09cd 100644 --- a/rust-test/Cargo.lock +++ b/rust-test/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "bytes" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" + [[package]] name = "itoa" version = "1.0.10" @@ -26,14 +32,31 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rlp" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb919243f34364b6bd2fc10ef797edbfa75f33c252e7998527479c6d6b47e1ec" +dependencies = [ + "bytes", + "rustc-hex", +] + [[package]] name = "rust-test" version = "0.1.0" dependencies = [ + "rlp", "serde", "serde_json", ] +[[package]] +name = "rustc-hex" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" + [[package]] name = "ryu" version = "1.0.17" diff --git a/rust-test/Cargo.toml b/rust-test/Cargo.toml index 2c03c0ab247..51213fd8a2b 100644 --- a/rust-test/Cargo.toml +++ b/rust-test/Cargo.toml @@ -12,3 +12,4 @@ crate-type = ["staticlib", "rlib"] # Creates static lib [dependencies] serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +rlp = "0.5.2" diff --git a/rust-test/src/lib.rs b/rust-test/src/lib.rs index 4943bb82084..5e9c15ff678 100644 --- a/rust-test/src/lib.rs +++ b/rust-test/src/lib.rs @@ -1,4 +1,5 @@ use serde::Serialize; +use rlp::RlpStream; #[derive(Serialize)] struct Foo { @@ -6,5 +7,6 @@ struct Foo { } pub fn add(left: usize, right: usize) { + let _ = RlpStream::default(); println!("{}", serde_json::to_string(&Foo {foo: left + right}).unwrap()); } diff --git a/tools/release-size b/tools/release-size index 3ec6f0d0722..3ed32d4ce3d 100755 --- a/tools/release-size +++ b/tools/release-size @@ -13,6 +13,14 @@ RUST_TARGETS = [ LIB_NAME = "libwallet_core_rs.a" +def display_size(size_kb: int) -> str: + if size_kb >= 10000: + size_mb = float(size_kb) / 1024 + return f'{size_mb:+.2f} MB' + else: + return f'{size_kb} KB' + + def measure_rust(_args): result = {} @@ -20,26 +28,35 @@ def measure_rust(_args): # TODO 'rust-test' to 'rust' path = f'rust-test/target/{target}/release/{LIB_NAME}' file_stats = os.stat(path) - file_size_mb = file_stats.st_size / 1024 / 1024 - result[target] = round(file_size_mb, 2) + file_size_kb = file_stats.st_size / 1024 + result[target] = int(file_size_kb) print(json.dumps(result)) def compare_sizes(args): - print('```diff') + def display_target(target: str, before_kb: int, current_kb: int): + diff_kb = current_kb - before_kb + print(f'➡️ **{target}**') + print("```diff") + if before_kb == current_kb: + print(f' Size {display_size(before_kb)}') + else: + print(f'- Size {display_size(before_kb)}') + print(f'+ Size {display_size(current_kb)} \t {display_size(diff_kb)}') + print("```") - before_json = json.load(open(args.before, 'r')) - after_json = json.load(open(args.after, 'r')) - for target, after_value in after_json.items(): - before_value = before_json[target] - diff = after_value - before_value + current_json = json.load(open(args.current, 'r')) + before_json = {} + if os.path.isfile(args.before): + before_json = json.load(open(args.before, 'r')) - print(f'**{target}**') - print(f'- Size {before_value}') - print(f'+ Size {after_value} \t {diff:+.2f}') + print("## Binary size comparison") + print() + for target, current_kb in current_json.items(): + before_kb = before_json.get(target, 0) + display_target(target, before_kb, current_kb) print() - print("```") if __name__ == '__main__': @@ -50,9 +67,9 @@ if __name__ == '__main__': measure_parser.set_defaults(func=measure_rust) compare_parser = subparsers.add_parser('compare', - help="Compares binary sizes. Takes 'before' and 'after' file names") + help="Compares binary sizes. Takes 'before' and 'current' file names") compare_parser.add_argument('before') - compare_parser.add_argument('after') + compare_parser.add_argument('current') compare_parser.set_defaults(func=compare_sizes) args = parser.parse_args()