|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { execSync } from "node:child_process"; |
| 3 | +import path from "node:path"; |
| 4 | +import { describe, it } from "node:test"; |
| 5 | + |
| 6 | +import { getAllFilesMatching } from "@nomicfoundation/hardhat-utils/fs"; |
| 7 | +import debug from "debug"; |
| 8 | + |
| 9 | +const log = debug( |
| 10 | + "hardhat:test:network-manager:request-handlers:request-array", |
| 11 | +); |
| 12 | + |
| 13 | +/** |
| 14 | + * Example debug output: |
| 15 | + * 2025-03-18T10:54:37.575Z hardhat:test:network-manager:request-handlers:request-array importing hd-wallet-handler.ts took 37ms |
| 16 | + * 2025-03-18T10:54:37.576Z hardhat:test:network-manager:request-handlers:request-array importing local-accounts.ts took 36ms |
| 17 | + * 2025-03-18T10:54:37.576Z hardhat:test:network-manager:request-handlers:request-array importing fixed-sender-handler.ts took 11ms |
| 18 | + * 2025-03-18T10:54:37.576Z hardhat:test:network-manager:request-handlers:request-array importing chain-id-handler.ts took 11ms |
| 19 | + * 2025-03-18T10:54:37.576Z hardhat:test:network-manager:request-handlers:request-array importing chain-id.ts took 11ms |
| 20 | + * 2025-03-18T10:54:37.576Z hardhat:test:network-manager:request-handlers:request-array importing automatic-gas-handler.ts took 11ms |
| 21 | + * 2025-03-18T10:54:37.576Z hardhat:test:network-manager:request-handlers:request-array importing automatic-gas-price-handler.ts took 11ms |
| 22 | + * 2025-03-18T10:54:37.576Z hardhat:test:network-manager:request-handlers:request-array importing fixed-gas-handler.ts took 11ms |
| 23 | + * 2025-03-18T10:54:37.576Z hardhat:test:network-manager:request-handlers:request-array importing automatic-sender-handler.ts took 10ms |
| 24 | + * 2025-03-18T10:54:37.576Z hardhat:test:network-manager:request-handlers:request-array importing sender.ts took 10ms |
| 25 | + * 2025-03-18T10:54:37.576Z hardhat:test:network-manager:request-handlers:request-array importing fixed-gas-price-handler.ts took 10ms |
| 26 | + * 2025-03-18T10:54:37.576Z hardhat:test:network-manager:request-handlers:request-array importing multiplied-gas-estimation.ts took 10ms |
| 27 | + */ |
| 28 | + |
| 29 | +describe( |
| 30 | + "HandlersArray", |
| 31 | + { |
| 32 | + skip: process.env.HARDHAT_DISABLE_SLOW_TESTS === "true", |
| 33 | + }, |
| 34 | + async () => { |
| 35 | + it(`should load all the handlers in a reasonable amount of time`, async () => { |
| 36 | + const handlersDir = path.resolve( |
| 37 | + "src/internal/builtin-plugins/network-manager/request-handlers/handlers", |
| 38 | + ); |
| 39 | + const handlers = await getAllFilesMatching(handlersDir); |
| 40 | + const handlerImports = []; |
| 41 | + |
| 42 | + const nodeOptions: any = { |
| 43 | + cwd: process.cwd(), |
| 44 | + stdio: "pipe", |
| 45 | + env: { |
| 46 | + ...process.env, |
| 47 | + FORCE_COLOR: "0", |
| 48 | + NO_COLOR: "1", |
| 49 | + }, |
| 50 | + }; |
| 51 | + |
| 52 | + // NOTE: First, we run a dummy command to warm up node. The number of runs |
| 53 | + // is arbitrary, but it seems to be enough to get reasonable stability. |
| 54 | + for (let i = 0; i < 20; i++) { |
| 55 | + execSync("node --import tsx/esm -e ''", nodeOptions); |
| 56 | + } |
| 57 | + |
| 58 | + for (const handler of handlers) { |
| 59 | + const output = execSync( |
| 60 | + `node --import tsx/esm -e "const start = Date.now(); await import('${handler}'); console.log(Date.now() - start);"`, |
| 61 | + { |
| 62 | + cwd: process.cwd(), |
| 63 | + stdio: "pipe", |
| 64 | + env: { |
| 65 | + ...process.env, |
| 66 | + FORCE_COLOR: "0", |
| 67 | + NO_COLOR: "1", |
| 68 | + }, |
| 69 | + }, |
| 70 | + ); |
| 71 | + const duration = parseInt(output.toString().trim(), 10); |
| 72 | + handlerImports.push({ |
| 73 | + handler: path.basename(handler), |
| 74 | + duration, |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + handlerImports.sort((a, b) => b.duration - a.duration); |
| 79 | + |
| 80 | + for (const { handler, duration } of handlerImports) { |
| 81 | + log(`importing ${handler} took ${duration}ms`); |
| 82 | + } |
| 83 | + |
| 84 | + // NOTE: The maximum import duration is arbitrary, but it seems to reasonably detect the outliers. |
| 85 | + const maxImportDuration = 20; |
| 86 | + const longestHandlerImport = handlerImports[0]; |
| 87 | + |
| 88 | + assert.ok( |
| 89 | + longestHandlerImport.duration < maxImportDuration, |
| 90 | + `The maximum import duration of ${longestHandlerImport.duration}ms (${longestHandlerImport.handler}) exceeds the ${maxImportDuration}ms limit`, |
| 91 | + ); |
| 92 | + }); |
| 93 | + }, |
| 94 | +); |
0 commit comments