starknet-devnet-rs icon indicating copy to clipboard operation
starknet-devnet-rs copied to clipboard

Investigate slow response time on fork tests

Open amanusk opened this issue 7 months ago • 1 comments

Fetching information from a node is considerably faster that fetching it from Devent, using the same node + forking.

Test executed on a local node, so traffic time shouldn't be an issue.

Example script

from starknet_py.net.full_node_client import FullNodeClient
from starknet_py.hash.selector import get_selector_from_name
from starknet_py.hash.utils import pedersen_hash


# --- helper --------------------------------------------------------------
async def fetch_storage(client, contract_addr, selector, idx: int):
    """
    One concurrent task:
      • computes the key for slot idx
      • awaits the storage value
      • returns both so the caller can print / post‑process
    """
    key = pedersen_hash(selector, idx)
    storage = await client.get_storage_at(contract_addr, key)
    return key, storage


async def run():
    # Connect to Starknet testnet (Sepolia)
    # client = FullNodeClient("https://free-rpc.nethermind.io/sepolia-juno/rpc/v0_8")
    # client = FullNodeClient("https://starknet-sepolia.public.blastapi.io/rpc/v0_8")
    # client = FullNodeClient("http://192.168.1.45:9235")
    client = FullNodeClient("http://127.0.0.1:5050")

    contract_address = (
        "0x01ab144ed3433b3949b3116389d2730d77bbff5d0c4e61451b856d5903a2c970"
    )

    selector = get_selector_from_name("my_array")  # compute once
    tasks = [fetch_storage(client, contract_address, selector, i) for i in range(200)]

    # asyncio.as_completed yields tasks in the order they *finish*,
    # so we can print results immediately without waiting for the slowest one.
    for fut in asyncio.as_completed(tasks):
        key, storage = await fut
        print(f"Storage at {hex(key)} : {storage}")


if __name__ == "__main__":
    import asyncio

    contract = asyncio.run(run())

When running using the local node directly, execution 200 requests takes 700ms

real	0m0.702s

Runing the same script, where devnet is started with fork of the same node

starknet-devnet --fork-network http://192.168.1.45:9235/rpc/v0_8

Results in execution of ~17 seconds

real	0m16.664s

amanusk avatar May 06 '25 09:05 amanusk

A few thoughts:

  1. It would make sense to compare how this performs on an unforked Devnet.
    • Would allow seeing how much the forking itself contributes to the slowdown.
    • Requires deploying the same contract on Devnet.
  2. The 200 transactions are expected to be executed in parallel? It could be that the origin node just allows for better parallelization than Devnet.
    • Devnet uses a single mutex for accessing the whole state, making it a bottleneck.
  3. How does the script perform on a non-Devnet forking provider?
    • I.e. maybe alternatives to Devnet aren't much faster.

FabijanC avatar May 07 '25 07:05 FabijanC