bench.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/bin/bash
  2. set -e
  3. if [ -z "$CI" ]; then
  4. MYDIR=$(realpath "$(dirname "$0")")
  5. cd "$MYDIR"
  6. cd ..
  7. export RPC_URL=http://localhost:8547
  8. export PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
  9. export WALLER_ADDRESS=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
  10. ls
  11. cd "nitro-testnode"
  12. ./test-node.bash script send-l2 --to address_$WALLER_ADDRESS --ethamount 0.1
  13. cd ..
  14. cd "pyth-mock-solidity"
  15. deployed_to=$(
  16. forge script ./script/MockPyth.s.sol:MockPythScript \
  17. --rpc-url "$RPC_URL" \
  18. --private-key "$PRIVATE_KEY" \
  19. --broadcast \ | grep -oP '(?<=Pyth contract address: )0x[a-fA-F0-9]{40}' | tail -n 1
  20. )
  21. export MOCK_PYTH_ADDRESS=$deployed_to
  22. cd ..
  23. else
  24. echo "Skipping MockPyth deployment in CI"
  25. fi
  26. MYDIR=$(realpath "$(dirname "$0")")
  27. cd "$MYDIR"
  28. cd ..
  29. # Optimize contract's wasm binary by crate name.
  30. opt_wasm () {
  31. local CONTRACT_CRATE_NAME=$1
  32. local CONTRACT_BIN_NAME="${CONTRACT_CRATE_NAME//-/_}.wasm"
  33. local CONTRACT_OPT_BIN_NAME="${CONTRACT_CRATE_NAME//-/_}_opt.wasm"
  34. echo
  35. echo "Optimizing $CONTRACT_CRATE_NAME WASM binary"
  36. # https://rustwasm.github.io/book/reference/code-size.html#use-the-wasm-opt-tool
  37. wasm-opt -O3 -o ./target/wasm32-unknown-unknown/release/"$CONTRACT_OPT_BIN_NAME" ./target/wasm32-unknown-unknown/release/"$CONTRACT_BIN_NAME"
  38. }
  39. # Retrieve all alphanumeric contract's crate names in `./examples` directory.
  40. get_example_crate_names () {
  41. # shellcheck disable=SC2038
  42. # NOTE: optimistically relying on the 'name = ' string at Cargo.toml file
  43. find ./examples -maxdepth 2 -type f -name "Cargo.toml" | xargs grep 'name = ' | grep -oE '".*"' | tr -d "'\""
  44. }
  45. cargo build --release --target wasm32-unknown-unknown -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort
  46. # Optimize contract's wasm for gas usage.
  47. for CRATE_NAME in $(get_example_crate_names)
  48. do
  49. opt_wasm "$CRATE_NAME"
  50. done
  51. export RPC_URL=http://localhost:8547
  52. # No need to compile benchmarks with `--release`
  53. # since this only runs the benchmarking code and the contracts have already been compiled with `--release`.
  54. cargo run -p benches
  55. echo "NOTE: To measure non cached contract's gas usage correctly,
  56. benchmarks should run on a clean instance of the nitro test node."
  57. echo
  58. echo "Finished running benches!"