GeezerVison is showing github.com — perf_hooks: allow omitted parameters in 'performance.measure' · nodejs/node@f302ac9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/perf_hooks.md +14-6Lines changed: 14 additions & 6 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -17,9 +17,12 @@ const obs = new PerformanceObserver((items) => { | 17 | 17 | performance.clearMarks(); | 18 | 18 | }); | 19 | 19 | obs.observe({ entryTypes: ['measure'] }); | | 20 | +performance.measure('Start to Now'); | 20 | 21 |
| 21 | 22 | performance.mark('A'); | 22 | 23 | doSomeLongRunningProcess(() => { | | 24 | + performance.measure('A to Now', 'A'); | | 25 | + | 23 | 26 | performance.mark('B'); | 24 | 27 | performance.measure('A to B', 'A', 'B'); | 25 | 28 | }); | @@ -53,14 +56,18 @@ Creates a new `PerformanceMark` entry in the Performance Timeline. A | 53 | 56 | `performanceEntry.duration` is always `0`. Performance marks are used | 54 | 57 | to mark specific significant moments in the Performance Timeline. | 55 | 58 |
| 56 | | -### `performance.measure(name, startMark, endMark)` | | 59 | +### `performance.measure(name[, startMark[, endMark]])` | 57 | 60 | <!-- YAML | 58 | 61 | 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. | 59 | 66 | --> | 60 | 67 |
| 61 | 68 | * `name` {string} | 62 | | -* `startMark` {string} | 63 | | -* `endMark` {string} | | 69 | +* `startMark` {string} Optional. | | 70 | +* `endMark` {string} Optional. | 64 | 71 |
| 65 | 72 | Creates a new `PerformanceMeasure` entry in the Performance Timeline. A | 66 | 73 | `PerformanceMeasure` is a subclass of `PerformanceEntry` whose | @@ -73,9 +80,10 @@ Performance Timeline, or *may* identify any of the timestamp properties | 73 | 80 | provided by the `PerformanceNodeTiming` class. If the named `startMark` does | 74 | 81 | not exist, then `startMark` is set to [`timeOrigin`][] by default. | 75 | 82 |
| 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 | 79 | 87 | error will be thrown. | 80 | 88 |
| 81 | 89 | ### `performance.nodeTiming` | |
Collapse file Copy file name to clipboardExpand all lines: lib/perf_hooks.js +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -395,12 +395,14 @@ class Performance { | 395 | 395 |
| 396 | 396 | measure(name, startMark, endMark) { | 397 | 397 | name = `${name}`; | 398 | | - endMark = `${endMark}`; | 399 | | - startMark = startMark !== undefined ? `${startMark}` : ''; | 400 | 398 | 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}`; | 403 | 404 | } | | 405 | + startMark = startMark !== undefined ? `${startMark}` : ''; | 404 | 406 | _measure(name, startMark, endMark); | 405 | 407 | } | 406 | 408 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_perf.cc +11-6Lines changed: 11 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -172,7 +172,6 @@ void Measure(const FunctionCallbackInfo<Value>& args) { | 172 | 172 | HandleScope scope(env->isolate()); | 173 | 173 | Utf8Value name(env->isolate(), args[0]); | 174 | 174 | Utf8Value startMark(env->isolate(), args[1]); | 175 | | - Utf8Value endMark(env->isolate(), args[2]); | 176 | 175 |
| 177 | 176 | AliasedFloat64Array& milestones = env->performance_state()->milestones; | 178 | 177 |
| @@ -186,11 +185,17 @@ void Measure(const FunctionCallbackInfo<Value>& args) { | 186 | 185 | startTimestamp = milestones[milestone]; | 187 | 186 | } | 188 | 187 |
| 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 | + } | 194 | 199 | } | 195 | 200 |
| 196 | 201 | if (endTimestamp < startTimestamp) | |
Collapse file Copy file name to clipboard +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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); |
|