From 704c7ed0f4a88a8659bcace5171f3c19d0d275c1 Mon Sep 17 00:00:00 2001 From: FrankQiu Date: Wed, 27 Apr 2022 20:53:49 +0800 Subject: [PATCH 1/4] perf_hooks: fix timerify bug --- lib/internal/perf/timerify.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/perf/timerify.js b/lib/internal/perf/timerify.js index 6bfede7aa1fa20..132054d3d52c5b 100644 --- a/lib/internal/perf/timerify.js +++ b/lib/internal/perf/timerify.js @@ -75,8 +75,8 @@ function timerify(fn, options = {}) { const result = constructor ? ReflectConstruct(fn, args, fn) : ReflectApply(fn, this, args); - if (!constructor && typeof result?.finally === 'function') { - return result.finally( + if (!constructor && typeof result?.then === 'function') { + return result.then( FunctionPrototypeBind( processComplete, result, From da9e49a5e3cdad105b67e97c07a5a7cdb777a0c8 Mon Sep 17 00:00:00 2001 From: Himself65 Date: Wed, 27 Apr 2022 20:27:28 -0500 Subject: [PATCH 2/4] fixup! return result in processComplete --- lib/internal/perf/timerify.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/internal/perf/timerify.js b/lib/internal/perf/timerify.js index 132054d3d52c5b..3fdd05ed2d67db 100644 --- a/lib/internal/perf/timerify.js +++ b/lib/internal/perf/timerify.js @@ -50,6 +50,7 @@ function processComplete(name, start, args, histogram) { entry[n] = args[n]; enqueue(entry); + return this; } function timerify(fn, options = {}) { From 4b62b68d88395693327ee19cbfbc956a3f18c72c Mon Sep 17 00:00:00 2001 From: Himself65 Date: Sun, 1 May 2022 15:26:33 -0500 Subject: [PATCH 3/4] fixup! rewrite logic --- lib/internal/perf/timerify.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/internal/perf/timerify.js b/lib/internal/perf/timerify.js index 3fdd05ed2d67db..872f4ebee480bf 100644 --- a/lib/internal/perf/timerify.js +++ b/lib/internal/perf/timerify.js @@ -50,7 +50,6 @@ function processComplete(name, start, args, histogram) { entry[n] = args[n]; enqueue(entry); - return this; } function timerify(fn, options = {}) { @@ -78,16 +77,15 @@ function timerify(fn, options = {}) { ReflectApply(fn, this, args); if (!constructor && typeof result?.then === 'function') { return result.then( - FunctionPrototypeBind( - processComplete, - result, - fn.name, - start, - args, - histogram)); + function wrappedTimerifiedPromise(value) { + processComplete(fn.name, start, args, histogram); + return value; + } + ); + } else { + processComplete(fn.name, start, args, histogram); + return result; } - processComplete(fn.name, start, args, histogram); - return result; } ObjectDefineProperties(timerified, { From 3a28c1b2ac134df4f2feed6faef9810f308287ea Mon Sep 17 00:00:00 2001 From: Himself65 Date: Sun, 1 May 2022 20:35:59 -0500 Subject: [PATCH 4/4] fixup! add test case and lint fix --- lib/internal/perf/timerify.js | 7 +++---- test/parallel/test-performance-function.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/internal/perf/timerify.js b/lib/internal/perf/timerify.js index 872f4ebee480bf..f7c66af9193470 100644 --- a/lib/internal/perf/timerify.js +++ b/lib/internal/perf/timerify.js @@ -1,7 +1,6 @@ 'use strict'; const { - FunctionPrototypeBind, ObjectDefineProperties, MathCeil, ReflectApply, @@ -82,10 +81,10 @@ function timerify(fn, options = {}) { return value; } ); - } else { - processComplete(fn.name, start, args, histogram); - return result; } + processComplete(fn.name, start, args, histogram); + return result; + } ObjectDefineProperties(timerified, { diff --git a/test/parallel/test-performance-function.js b/test/parallel/test-performance-function.js index fcc3004d02884a..2160f828c97ddd 100644 --- a/test/parallel/test-performance-function.js +++ b/test/parallel/test-performance-function.js @@ -89,6 +89,20 @@ const { assert.strictEqual(p.name, 'timerified timerified m'); } +// Function +{ + const f1 = () => 1; + const f2 = async () => 2; + const h1 = createHistogram(); + const h2 = createHistogram(); + const g1 = performance.timerify(f1, { histogram: h1 }); + const g2 = performance.timerify(f2, { histogram: h2 }); + g1(); + g2().then(common.mustCall(() => { + assert.strictEqual(h1.count, h2.count); + })); +} + (async () => { const histogram = createHistogram(); const m = (a, b = 1) => {};