globalSetup
One-time setup before benchmarks; Anvil spawning
Loading...
The globalSetup block runs once before all benchmarks in the file. It is the correct place to spawn long-lived services like an Anvil node for Ethereum RPC benchmarks.
globalSetup can appear at two levels. The placement determines its scope:
| Placement | Scope |
|---|---|
| File-level | Before any suite; shared across all suites in the file |
| Suite-level | Inside a suite block; only that suite's benchmarks |
1use std::anvil2
3globalSetup {4 anvil.spawnAnvil() # Local node, no fork5 anvil.spawnAnvil(fork: "https://rpc.url") # Fork from an RPC endpoint6}7
8suite evmBench {9 # ... setup, fixtures, benchmarks ...10}11
12suite anotherSuite {13 # Also uses the same Anvil instance from file-level globalSetup14}1use std::anvil2
3suite evmBench {4 globalSetup {5 anvil.spawnAnvil(fork: "https://eth.llamarpc.com")6 }7
8 setup go { ... }9 setup ts { ... }10
11 bench getBlockNumber {12 go: getBlockNumber()13 ts: await getBlockNumber()14 }15}globalSetup, it overrides file-level globalSetup for that suite.When anvil.spawnAnvil() is called, poly-bench injects the RPC URL as an environment variable. Each language accesses it differently:
| Language | Access |
|---|---|
| TypeScript | ANVIL_RPC_URL (module-level constant) |
| Go | os.Getenv("ANVIL_RPC_URL") |
| Rust | std::env::var("ANVIL_RPC_URL") |
| Python | os.environ["ANVIL_RPC_URL"] |
anvil.spawnAnvil() is called; access it via the env var in each language.1use std::anvil2
3suite rpcBench {4 globalSetup {5 anvil.spawnAnvil(fork: "https://eth.llamarpc.com")6 }7
8 setup go {9 import (10 "github.com/ethereum/go-ethereum/ethclient"11 )12 declare { var client *ethclient.Client }13 init {14 client, _ = ethclient.Dial(os.Getenv("ANVIL_RPC_URL"))15 }16 helpers {17 func getBlock() uint64 {18 num, _ := client.BlockNumber(context.Background())19 return num20 }21 }22 }23
24 setup ts {25 import {26 import { createPublicClient, http } from 'viem'27 import { mainnet } from 'viem/chains'28 }29 declare { let client: any }30 init {31 client = createPublicClient({32 chain: mainnet,33 transport: http(ANVIL_RPC_URL),34 })35 }36 helpers {37 async function getBlock(): Promise<bigint> {38 return await client.getBlockNumber()39 }40 }41 }42
43 benchAsync getBlockNumber {44 go: getBlock()45 ts: await getBlock()46 }47}Use globalSetup for one-time setup that must complete before any benchmark runs. Typical uses include spawning an Anvil node with anvil.spawnAnvil(), starting other long-lived services such as mock servers, or loading config that all benchmarks depend on.
For per-language initialization, use the init sub-section in setup blocks.
spawnAnvil parameters