Skip to main content

Profile JSON-RPC calls

Every call a node's clients send is counted for the running test: per method, with timings, failures and calls repeated with identical params.

for (const account of [alice, alice, bob]) {
await balanceOf(anvil.testClient, token, account);
}
const { rpc } = await anvil.diagnostics.currentProfile();
expect(rpc.total).toBe(3);
expect(rpc.methods).toEqual([
expect.objectContaining({ method: "eth_call", count: 3, errors: 0 }),
]);
// alice's balance was read twice with the same params:
expect(rpc.duplicates).toEqual([
expect.objectContaining({ method: "eth_call", count: 2 }),
]);
expect(rpc.duplicateCalls).toBe(1);

Tested in examples/test/e2e/diagnostics-and-profiling.e2e.test.ts:125

Repeated calls are often polling (receipts, block numbers) or reads worth caching; withBatch(), multicall and withCacheTime() reduce them.

Concurrent tests​

// Each runTest() scope records only its own calls, even when they overlap.
await Promise.all(
[alice, bob].map((account) =>
anvil.diagnostics.runTest(`deposit as ${account}`, () =>
approveAndDeposit(account, 1n),
),
),
);
const [first, second] = anvil.diagnostics.reports.slice(-2);
expect(first.gas.transactions).toHaveLength(2);
expect(second.gas.transactions).toHaveLength(2);
const senders = [first, second].map((report) =>
report.gas.transactions.map((tx) => tx.from),
);
expect(new Set(senders[0]).size).toBe(1); // one account each
expect(senders[0][0]).not.toBe(senders[1][0]);

Tested in examples/test/e2e/diagnostics-and-profiling.e2e.test.ts:217

With test.concurrent, wrap each body: it.concurrent("…", () => anvil.diagnostics.runTest("…", async () => { … })).

Leave fixtures out​

beforeEach(async () => {
await mint(anvil.testClient, token, alice, 1n); // fixture work…
anvil.diagnostics.reset(); // …that doesn't count towards the test
});

it("profiles only the test body", async () => {
await balanceOf(anvil.testClient, token, alice);
const profile = await anvil.diagnostics.currentProfile();
expect(profile.gas.transactions).toEqual([]); // no mint
expect(profile.rpc.total).toBe(1);
});

Tested in examples/test/e2e/diagnostics-and-profiling.e2e.test.ts:238

What is counted: the calls of testClient, testClientFor(), createTestClient(), rpc() and cheats, once per call (batched or retried). eth_chainId is answered locally and not counted.

Concepts​