Skip to content

Commit 1a01ac3

Browse files
himself65targos
authored andcommitted
perf_hooks: allow omitted parameters in 'performance.measure'
Make `startMark` and `endMark` parameters optional. PR-URL: #32651 Fixes: #32647 Refs: https://www.w3.org/TR/user-timing-2/#measure-method Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 180e437 commit 1a01ac3

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

doc/api/perf_hooks.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ const obs = new PerformanceObserver((items) => {
1717
performance.clearMarks();
1818
});
1919
obs.observe({ entryTypes: ['measure'] });
20+
performance.measure('Start to Now');
2021

2122
performance.mark('A');
2223
doSomeLongRunningProcess(() => {
24+
performance.measure('A to Now', 'A');
25+
2326
performance.mark('B');
2427
performance.measure('A to B', 'A', 'B');
2528
});
@@ -53,14 +56,18 @@ Creates a new `PerformanceMark` entry in the Performance Timeline. A
5356
`performanceEntry.duration` is always `0`. Performance marks are used
5457
to mark specific significant moments in the Performance Timeline.
5558

56-
### `performance.measure(name, startMark, endMark)`
59+
### `performance.measure(name[, startMark[, endMark]])`
5760
<!-- YAML
5861
added: v8.5.0
62+
changes:
63+
- version: REPLACEME
64+
pr-url: https://github.com/nodejs/node/pull/32651
65+
description: Make `startMark` and `endMark` parameters optional.
5966
-->
6067

6168
* `name` {string}
62-
* `startMark` {string}
63-
* `endMark` {string}
69+
* `startMark` {string} Optional.
70+
* `endMark` {string} Optional.
6471

6572
Creates a new `PerformanceMeasure` entry in the Performance Timeline. A
6673
`PerformanceMeasure` is a subclass of `PerformanceEntry` whose
@@ -73,9 +80,10 @@ Performance Timeline, or *may* identify any of the timestamp properties
7380
provided by the `PerformanceNodeTiming` class. If the named `startMark` does
7481
not exist, then `startMark` is set to [`timeOrigin`][] by default.
7582

76-
The `endMark` argument must identify any *existing* `PerformanceMark` in the
77-
Performance Timeline or any of the timestamp properties provided by the
78-
`PerformanceNodeTiming` class. If the named `endMark` does not exist, an
83+
The optional `endMark` argument must identify any *existing* `PerformanceMark`
84+
in the Performance Timeline or any of the timestamp properties provided by the
85+
`PerformanceNodeTiming` class. `endMark` will be `performance.now()`
86+
if no parameter is passed, otherwise if the named `endMark` does not exist, an
7987
error will be thrown.
8088

8189
### `performance.nodeTiming`

lib/perf_hooks.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,14 @@ class Performance {
395395

396396
measure(name, startMark, endMark) {
397397
name = `${name}`;
398-
endMark = `${endMark}`;
399-
startMark = startMark !== undefined ? `${startMark}` : '';
400398
const marks = this[kIndex][kMarks];
401-
if (!marks.has(endMark) && !(endMark in nodeTiming)) {
402-
throw new ERR_INVALID_PERFORMANCE_MARK(endMark);
399+
if (arguments.length >= 3) {
400+
if (!marks.has(endMark) && !(endMark in nodeTiming))
401+
throw new ERR_INVALID_PERFORMANCE_MARK(endMark);
402+
else
403+
endMark = `${endMark}`;
403404
}
405+
startMark = startMark !== undefined ? `${startMark}` : '';
404406
_measure(name, startMark, endMark);
405407
}
406408

src/node_perf.cc

+11-6
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ void Measure(const FunctionCallbackInfo<Value>& args) {
172172
HandleScope scope(env->isolate());
173173
Utf8Value name(env->isolate(), args[0]);
174174
Utf8Value startMark(env->isolate(), args[1]);
175-
Utf8Value endMark(env->isolate(), args[2]);
176175

177176
AliasedFloat64Array& milestones = env->performance_state()->milestones;
178177

@@ -186,11 +185,17 @@ void Measure(const FunctionCallbackInfo<Value>& args) {
186185
startTimestamp = milestones[milestone];
187186
}
188187

189-
uint64_t endTimestamp = GetPerformanceMark(env, *endMark);
190-
if (endTimestamp == 0) {
191-
PerformanceMilestone milestone = ToPerformanceMilestoneEnum(*endMark);
192-
if (milestone != NODE_PERFORMANCE_MILESTONE_INVALID)
193-
endTimestamp = milestones[milestone];
188+
uint64_t endTimestamp = 0;
189+
if (args[2]->IsUndefined()) {
190+
endTimestamp = PERFORMANCE_NOW();
191+
} else {
192+
Utf8Value endMark(env->isolate(), args[2]);
193+
endTimestamp = GetPerformanceMark(env, *endMark);
194+
if (endTimestamp == 0) {
195+
PerformanceMilestone milestone = ToPerformanceMilestoneEnum(*endMark);
196+
if (milestone != NODE_PERFORMANCE_MILESTONE_INVALID)
197+
endTimestamp = milestones[milestone];
198+
}
194199
}
195200

196201
if (endTimestamp < startTimestamp)
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
6+
const { PerformanceObserver, performance } = require('perf_hooks');
7+
const DELAY = 1000;
8+
9+
const expected = ['Start to Now', 'A to Now', 'A to B'];
10+
const obs = new PerformanceObserver(common.mustCall((items) => {
11+
const entries = items.getEntries();
12+
const { name, duration } = entries[0];
13+
assert.ok(duration > DELAY);
14+
assert.strictEqual(expected.shift(), name);
15+
}, 3));
16+
obs.observe({ entryTypes: ['measure'] });
17+
18+
performance.mark('A');
19+
setTimeout(common.mustCall(() => {
20+
performance.measure('Start to Now');
21+
performance.measure('A to Now', 'A');
22+
23+
performance.mark('B');
24+
performance.measure('A to B', 'A', 'B');
25+
}), DELAY);

0 commit comments

Comments
 (0)