Skip to content

Commit

Permalink
Merge pull request #412 from near/austin/code_size_tests
Browse files Browse the repository at this point in the history
feat: add compiled code size tests
  • Loading branch information
mikedotexe authored May 17, 2021
2 parents 58bcd0d + 43cfcb8 commit 11b4dd8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ jobs:
- uses: Swatinem/rust-cache@v1
- name: Test Format
run: cargo fmt -- --check
- name: Add wasm32 target
run: rustup target add wasm32-unknown-unknown
- name: Test
run: cargo test --all
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ members = [
exclude = [
"examples/cross-contract-high-level",
"examples/fungible-token",
"examples/lockable-fungible-token",
"examples/cross-contract-low-level",
"examples/mission-control",
"examples/status-message",
"examples/status-message-collections",
"examples/test-contract",
]

# Special triple # comment for ci.
Expand Down
37 changes: 37 additions & 0 deletions near-sdk/tests/code_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// Compiles contract to wasm with release configuration and returns the code size.
fn check_example_size(example: &str) -> usize {
let status = std::process::Command::new("cargo")
.env("RUSTFLAGS", "-C link-arg=-s")
.args(&["build", "--release", "--target", "wasm32-unknown-unknown", "--manifest-path"])
.arg(format!("../examples/{}/Cargo.toml", example))
.status()
.unwrap();
if !status.success() {
panic!("building wasm example returned non-zero code {}", status);
}

let wasm = std::fs::read(format!(
"../examples/{}/target/wasm32-unknown-unknown/release/{}.wasm",
example,
example.replace("-", "_")
))
.unwrap();

wasm.len()
}

#[test]
fn lock_fungible_code_size_check() {
let size = check_example_size("lockable-fungible-token");

// Current contract size at the time of writing this test is 164_433, giving about ~10% buffer.
assert!(size < 180_000);
}

#[test]
fn status_message_code_size_check() {
let size = check_example_size("status-message");

// Currently 140823.
assert!(size < 155_000);
}

0 comments on commit 11b4dd8

Please sign in to comment.