mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
e124b0ff7d
The code sample at the top of the "Performance measurements API" section of the docs does not run. The code in question: ```js const { PerformanceObserver, performance } = require('node:perf_hooks'); const obs = new PerformanceObserver((items) => { console.log(items.getEntries()[0].duration); performance.clearMarks(); }); obs.observe({ type: 'measure' }); performance.measure('Start to Now'); performance.mark('A'); doSomeLongRunningProcess(() => { performance.measure('A to Now', 'A'); performance.mark('B'); performance.measure('A to B', 'A', 'B'); }); ``` If you replace `doSomeLongRunningProcess` with an IIFE with a sleep() at the top of it, you get this: ```js const { PerformanceObserver, performance } = require('node:perf_hooks'); const obs = new PerformanceObserver((items) => { console.log(items.getEntries()[0].duration); performance.clearMarks(); }); obs.observe({ type: 'measure' }); performance.measure('Start to Now'); performance.mark('A'); (async function doSomeLongRunningProcess() { await new Promise(r => setTimeout(r, 5000)); performance.measure('A to Now', 'A'); performance.mark('B'); performance.measure('A to B', 'A', 'B'); })() ``` When you run this, you get the following output: ```sh $ node performance-test.js 17.873416 node:internal/per_context/domexception:53 ErrorCaptureStackTrace(this); ^ DOMException [SyntaxError]: The "A" performance mark has not been set at new DOMException (node:internal/per_context/domexception:53:5) at __node_internal_ (node:internal/util:695:10) at getMark (node:internal/perf/usertiming:65:11) at calculateStartDuration (node:internal/perf/usertiming:202:13) at measure (node:internal/perf/usertiming:220:7) at Performance.measure (node:internal/perf/performance:135:12) at /private/tmp/performance-test.js:14:15 Node.js v20.11.1 ``` I believe it's due to the call to `performance.clearMarks();` in the PerformanceObserver callback. If you remove that, it works as expected: ```js const { PerformanceObserver, performance } = require('node:perf_hooks'); const obs = new PerformanceObserver((items) => { console.log(items.getEntries()[0].duration); }); obs.observe({ type: 'measure' }); performance.measure('Start to Now'); performance.mark('A'); (async function doSomeLongRunningProcess() { await new Promise(r => setTimeout(r, 5000)); performance.measure('A to Now', 'A'); performance.mark('B'); performance.measure('A to B', 'A', 'B'); })() ``` ```sh $ node performance-test.js 17.761083 5002.468417 ``` PR-URL: https://github.com/nodejs/node/pull/54227 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> |
||
---|---|---|
.. | ||
addons.md | ||
assert.md | ||
async_context.md | ||
async_hooks.md | ||
buffer.md | ||
child_process.md | ||
cli.md | ||
cluster.md | ||
console.md | ||
corepack.md | ||
crypto.md | ||
debugger.md | ||
deprecations.md | ||
dgram.md | ||
diagnostics_channel.md | ||
dns.md | ||
documentation.md | ||
domain.md | ||
embedding.md | ||
errors.md | ||
esm.md | ||
events.md | ||
fs.md | ||
globals.md | ||
http2.md | ||
http.md | ||
https.md | ||
index.md | ||
inspector.md | ||
intl.md | ||
module.md | ||
modules.md | ||
n-api.md | ||
net.md | ||
os.md | ||
packages.md | ||
path.md | ||
perf_hooks.md | ||
permissions.md | ||
process.md | ||
punycode.md | ||
querystring.md | ||
readline.md | ||
repl.md | ||
report.md | ||
single-executable-applications.md | ||
sqlite.md | ||
stream.md | ||
string_decoder.md | ||
synopsis.md | ||
test.md | ||
timers.md | ||
tls.md | ||
tracing.md | ||
tty.md | ||
typescript.md | ||
url.md | ||
util.md | ||
v8.md | ||
vm.md | ||
wasi.md | ||
webcrypto.md | ||
webstreams.md | ||
worker_threads.md | ||
zlib.md |