Skip to content

Commit 2757521

Browse files
committed
bench(startup): add ESM variant that exercises the iitm loader (#8956)
The startup variants load the fixture through CommonJS require, which goes through require-in-the-middle and never registers the iitm ESM loader. That loader is what the synchronous module.registerHooks work changes from an off-thread async loader to an in-thread sync one, so the suite reported no movement for that change: nothing put the loader on a measured path. with-tracer-everything-esm registers the loader via --import ../../../register.js and imports the same fixture through ESM, so every dependency and its transitive graph flow through the loader's resolve/load hooks. The fixture's index.mjs reads the same dependencies as index.js, so the dependency list stays single-sourced in package.json. Refs: #8942
1 parent 56f1d27 commit 2757521

4 files changed

Lines changed: 46 additions & 6 deletions

File tree

benchmark/sirun/startup/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
Measures tracer startup overhead: how much the loader hooks add when wrapping a
22
representative production dependency graph. `everything-fixture/` is a
33
self-contained sub-project (own `package.json`/`package-lock.json`/`node_modules`)
4-
loaded via a single `require`, curated toward modules dd-trace instruments.
4+
curated toward modules dd-trace instruments. Both fixture entries read the same
5+
`dependencies`, so updating `package.json` covers both:
6+
7+
- `index.js` loads them with CommonJS `require`, exercising require-in-the-middle.
8+
- `index.mjs` loads them with ESM `import`; the `with-tracer-everything-esm`
9+
variant registers the iitm ESM loader via `--import ../../../register.js`, so
10+
this is the variant that measures the synchronous-vs-asynchronous loader cost.
511

612
## Updating the fixture
713

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createRequire } from 'node:module'
2+
3+
// Bare-specifier dynamic imports resolve against this sub-project's own
4+
// node_modules and go through the ESM resolve/load hooks, unlike the CJS
5+
// `require` in index.js. This is what puts the iitm ESM loader on the measured
6+
// path when the startup bench registers it via `--import ../../../register.js`.
7+
const { dependencies } = createRequire(import.meta.url)('./package.json')
8+
9+
await Promise.all(Object.keys(dependencies).map((name) => import(name)))

benchmark/sirun/startup/meta.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
"USE_TRACER": "1",
1717
"EVERYTHING": "1"
1818
}
19+
},
20+
"with-tracer-everything-esm": {
21+
"env": {
22+
"USE_TRACER": "1",
23+
"EVERYTHING": "1",
24+
"ESM": "1",
25+
"NODE_OPTIONS": "--import ../../../register.js"
26+
}
1927
}
2028
}
2129
}

benchmark/sirun/startup/startup-test.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,27 @@ if (Number(process.env.USE_TRACER)) {
77
}
88

99
if (Number(process.env.EVERYTHING)) {
10-
require('./everything-fixture')
10+
if (Number(process.env.ESM)) {
11+
// The ESM variant registers the iitm ESM loader through
12+
// NODE_OPTIONS=--import ../../../register.js, so importing the fixture routes
13+
// every dependency and its transitive graph through the loader's resolve/load
14+
// hooks. The CJS branch below goes through require-in-the-middle and never
15+
// touches the ESM loader, so this is the only startup variant that measures
16+
// the synchronous-vs-asynchronous loader cost the iitm hooks change.
17+
assert.match(
18+
process.env.NODE_OPTIONS ?? '',
19+
/--import\b.+register\.js/,
20+
'ESM startup variant must register the iitm loader via --import register.js'
21+
)
22+
// The floating import is the measured workload: it keeps the process alive
23+
// until the graph finishes loading, and a rejection surfaces as a non-zero exit.
24+
import('./everything-fixture/index.mjs')
25+
} else {
26+
require('./everything-fixture')
1127

12-
assert.ok(
13-
require.cache[require.resolve('./everything-fixture/node_modules/express')],
14-
'everything-fixture did not load (express not in require cache)'
15-
)
28+
assert.ok(
29+
require.cache[require.resolve('./everything-fixture/node_modules/express')],
30+
'everything-fixture did not load (express not in require cache)'
31+
)
32+
}
1633
}

0 commit comments

Comments
 (0)