From 93cd65e533418d46d8e017a0675cc84f51b216bc Mon Sep 17 00:00:00 2001 From: Oba Date: Fri, 12 Jul 2024 10:06:58 +0200 Subject: [PATCH] dev: loop profiling (#1256) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Time spent on this PR: ## Pull request type Please check the type of change your PR introduces: - [ ] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no api changes) - [ ] Build related changes - [ ] Documentation content changes - [X] Other (please describe): profiling ## What is the current behavior? Resolves #1249 ## What is the new behavior? For max = 10: ![profile001](https://github.com/kkrt-labs/kakarot/assets/92337658/1de65da6-64f6-490c-9090-a3a6ce58c9f1) For max = 50 ![profile002](https://github.com/kkrt-labs/kakarot/assets/92337658/efc15393-17d4-4607-ac79-13a828ed25af) - - - This change is [Reviewable](https://reviewable.io/reviews/kkrt-labs/kakarot/1256) --- .../src/PlainOpcodes/PlainOpcodes.sol | 8 ++++++++ tests/fixtures/starknet.py | 2 +- tests/src/kakarot/test_kakarot.py | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/solidity_contracts/src/PlainOpcodes/PlainOpcodes.sol b/solidity_contracts/src/PlainOpcodes/PlainOpcodes.sol index cd49b0f16..30d4925e6 100644 --- a/solidity_contracts/src/PlainOpcodes/PlainOpcodes.sol +++ b/solidity_contracts/src/PlainOpcodes/PlainOpcodes.sol @@ -207,6 +207,14 @@ contract PlainOpcodes { return hash; } + function loopProfiling(uint256 max) public pure returns (uint256) { + uint256 val = 0; + for (uint256 i = 0; i < max; i++) { + val += i; + } + return val; + } + receive() external payable {} fallback() external payable {} } diff --git a/tests/fixtures/starknet.py b/tests/fixtures/starknet.py index 25cfdf0e3..385741613 100644 --- a/tests/fixtures/starknet.py +++ b/tests/fixtures/starknet.py @@ -179,7 +179,7 @@ def _factory(entrypoint, **kwargs) -> list: static_locals={"debug_info": debug_info(program)}, vm_class=VmWithCoverage, ) - run_resources = RunResources(n_steps=4_000_000) + run_resources = RunResources(n_steps=10_000_000) try: runner.run_until_pc(end, run_resources) except Exception as e: diff --git a/tests/src/kakarot/test_kakarot.py b/tests/src/kakarot/test_kakarot.py index f6e7db151..1348eb69e 100644 --- a/tests/src/kakarot/test_kakarot.py +++ b/tests/src/kakarot/test_kakarot.py @@ -385,3 +385,22 @@ async def test_failing_contract(self, cairo_run): data="0xADD_DATA", ) assert not evm["reverted"] + + class TestLoopProfiling: + @pytest.mark.slow + @pytest.mark.NoCI + @pytest.mark.parametrize("steps", [10, 50, 100, 200]) + @SyscallHandler.patch("IAccount.is_valid_jumpdest", lambda addr, data: [1]) + def test_loop_profiling(self, get_contract, steps): + plain_opcodes = get_contract("PlainOpcodes", "PlainOpcodes") + initial_state = { + CONTRACT_ADDRESS: { + "code": list(plain_opcodes.bytecode_runtime), + "storage": {}, + "balance": 0, + "nonce": 0, + } + } + with SyscallHandler.patch_state(parse_state(initial_state)): + res = plain_opcodes.loopProfiling(steps) + assert res == sum(x for x in range(steps))