From 77710251e10982f847d2d3dfcf5d920b0a03a539 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Sat, 29 Jun 2024 12:48:23 +0800 Subject: [PATCH] lib: add toJSON to PerformanceMeasure PR-URL: https://github.com/nodejs/node/pull/53603 Refs: https://github.com/nodejs/node/issues/53570 Reviewed-By: Chengzhong Wu Reviewed-By: Chemi Atlow Reviewed-By: Benjamin Gruenbaum --- lib/internal/perf/usertiming.js | 10 ++++++++++ .../test-performance-measure-detail.js | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 test/parallel/test-performance-measure-detail.js diff --git a/lib/internal/perf/usertiming.js b/lib/internal/perf/usertiming.js index ab109c3c04bce6..dfbbcbaf36c538 100644 --- a/lib/internal/perf/usertiming.js +++ b/lib/internal/perf/usertiming.js @@ -136,6 +136,16 @@ class PerformanceMeasure extends PerformanceEntry { validateInternalField(this, kDetail, 'PerformanceMeasure'); return this[kDetail]; } + + toJSON() { + return { + name: this.name, + entryType: this.entryType, + startTime: this.startTime, + duration: this.duration, + detail: this[kDetail], + }; + } } ObjectDefineProperties(PerformanceMeasure.prototype, { detail: kEnumerableProperty, diff --git a/test/parallel/test-performance-measure-detail.js b/test/parallel/test-performance-measure-detail.js new file mode 100644 index 00000000000000..1bfcda661f43d4 --- /dev/null +++ b/test/parallel/test-performance-measure-detail.js @@ -0,0 +1,20 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const util = require('util'); +const { performance, PerformanceObserver } = require('perf_hooks'); + +const perfObserver = new PerformanceObserver(common.mustCall((items) => { + const entries = items.getEntries(); + assert.ok(entries.length === 1); + for (const entry of entries) { + assert.ok(util.inspect(entry).includes('this is detail')); + } +})); + +perfObserver.observe({ entryTypes: ['measure'] }); + +performance.measure('sample', { + detail: 'this is detail', +});