| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5efe4f7 commit 22fd621
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,25 @@ | |||
| 1 | + // Test basic functionality of timerify and PerformanceObserver. | ||
| 2 | + 'use strict'; | ||
| 3 | + | ||
| 4 | + const common = require('../common'); | ||
| 5 | + const assert = require('assert'); | ||
| 6 | + const { timerify, PerformanceObserver } = require('perf_hooks'); | ||
| 7 | + | ||
| 8 | + // Verifies that `performance.timerify` is an alias of `perf_hooks.timerify`. | ||
| 9 | + assert.strictEqual(performance.timerify, timerify); | ||
| 10 | + | ||
| 11 | + // Intentional non-op. Do not wrap in common.mustCall(); | ||
| 12 | + const n = timerify(function noop() {}); | ||
| 13 | + | ||
| 14 | + const obs = new PerformanceObserver(common.mustCall((list) => { | ||
| 15 | + const entries = list.getEntries(); | ||
| 16 | + const entry = entries[0]; | ||
| 17 | + assert(entry); | ||
| 18 | + assert.strictEqual(entry.name, 'noop'); | ||
| 19 | + assert.strictEqual(entry.entryType, 'function'); | ||
| 20 | + assert.strictEqual(typeof entry.duration, 'number'); | ||
| 21 | + assert.strictEqual(typeof entry.startTime, 'number'); | ||
| 22 | + obs.disconnect(); | ||
| 23 | + })); | ||
| 24 | + obs.observe({ entryTypes: ['function'] }); | ||
| 25 | + n(); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + // Test that timerify works with class constructors and creates performance | ||
| 2 | + // entries with the correct name. | ||
| 3 | + | ||
| 4 | + 'use strict'; | ||
| 5 | + | ||
| 6 | + const common = require('../common'); | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + const { timerify, PerformanceObserver } = require('perf_hooks'); | ||
| 9 | + | ||
| 10 | + class N {} | ||
| 11 | + const n = timerify(N); | ||
| 12 | + | ||
| 13 | + const obs = new PerformanceObserver(common.mustCall((list) => { | ||
| 14 | + const entries = list.getEntries(); | ||
| 15 | + const entry = entries[0]; | ||
| 16 | + assert.strictEqual(entry[0], 1); | ||
| 17 | + assert.strictEqual(entry[1], 'abc'); | ||
| 18 | + assert(entry); | ||
| 19 | + assert.strictEqual(entry.name, 'N'); | ||
| 20 | + assert.strictEqual(entry.entryType, 'function'); | ||
| 21 | + assert.strictEqual(typeof entry.duration, 'number'); | ||
| 22 | + assert.strictEqual(typeof entry.startTime, 'number'); | ||
| 23 | + obs.disconnect(); | ||
| 24 | + })); | ||
| 25 | + obs.observe({ entryTypes: ['function'] }); | ||
| 26 | + | ||
| 27 | + new n(1, 'abc'); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,16 @@ | |||
| 1 | + // Test that errors thrown in timerified functions bubble up without creating | ||
| 2 | + // performance timeline entries. | ||
| 3 | + | ||
| 4 | + 'use strict'; | ||
| 5 | + | ||
| 6 | + const common = require('../common'); | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + const { timerify, PerformanceObserver } = require('perf_hooks'); | ||
| 9 | + | ||
| 10 | + const obs = new PerformanceObserver(common.mustNotCall()); | ||
| 11 | + obs.observe({ entryTypes: ['function'] }); | ||
| 12 | + const n = timerify(() => { | ||
| 13 | + throw new Error('test'); | ||
| 14 | + }); | ||
| 15 | + assert.throws(() => n(), /^Error: test$/); | ||
| 16 | + obs.disconnect(); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,17 @@ | |||
| 1 | + // Test that timerify works with histogram option for asynchronous functions. | ||
| 2 | + | ||
| 3 | + import '../common/index.mjs'; | ||
| 4 | + import assert from 'assert'; | ||
| 5 | + import { createHistogram, timerify } from 'perf_hooks'; | ||
| 6 | + import { setTimeout as sleep } from 'timers/promises'; | ||
| 7 | + | ||
| 8 | + const histogram = createHistogram(); | ||
| 9 | + const m = async (a, b = 1) => { | ||
| 10 | + await sleep(10); | ||
| 11 | + }; | ||
| 12 | + const n = timerify(m, { histogram }); | ||
| 13 | + assert.strictEqual(histogram.max, 0); | ||
| 14 | + for (let i = 0; i < 10; i++) { | ||
| 15 | + await n(); | ||
| 16 | + } | ||
| 17 | + assert.notStrictEqual(histogram.max, 0); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,22 @@ | |||
| 1 | + // Test that timerify works with histogram option for synchronous functions. | ||
| 2 | + | ||
| 3 | + import '../common/index.mjs'; | ||
| 4 | + import assert from 'assert'; | ||
| 5 | + import { createHistogram, timerify } from 'perf_hooks'; | ||
| 6 | + import { setTimeout as sleep } from 'timers/promises'; | ||
| 7 | + | ||
| 8 | + let _deadCode; | ||
| 9 | + | ||
| 10 | + const histogram = createHistogram(); | ||
| 11 | + const m = (a, b = 1) => { | ||
| 12 | + for (let i = 0; i < 1e3; i++) | ||
| 13 | + _deadCode = i; | ||
| 14 | + }; | ||
| 15 | + const n = timerify(m, { histogram }); | ||
| 16 | + assert.strictEqual(histogram.max, 0); | ||
| 17 | + for (let i = 0; i < 10; i++) { | ||
| 18 | + n(); | ||
| 19 | + await sleep(10); | ||
| 20 | + } | ||
| 21 | + assert(_deadCode >= 0); | ||
| 22 | + assert.notStrictEqual(histogram.max, 0); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,28 @@ | |||
| 1 | + // Test that timerify throws appropriate errors for invalid argument types. | ||
| 2 | + | ||
| 3 | + 'use strict'; | ||
| 4 | + | ||
| 5 | + require('../common'); | ||
| 6 | + const assert = require('assert'); | ||
| 7 | + const { timerify } = require('perf_hooks'); | ||
| 8 | + | ||
| 9 | + [1, {}, [], null, undefined, Infinity].forEach((input) => { | ||
| 10 | + assert.throws(() => timerify(input), | ||
| 11 | + { | ||
| 12 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 13 | + name: 'TypeError', | ||
| 14 | + message: /The "fn" argument must be of type function/ | ||
| 15 | + }); | ||
| 16 | + }); | ||
| 17 | + | ||
| 18 | + const asyncFunc = async (a, b = 1) => {}; | ||
| 19 | + const syncFunc = (a, b = 1) => {}; | ||
| 20 | + | ||
| 21 | + [1, '', {}, [], false].forEach((histogram) => { | ||
| 22 | + assert.throws(() => timerify(asyncFunc, { histogram }), { | ||
| 23 | + code: 'ERR_INVALID_ARG_TYPE' | ||
| 24 | + }); | ||
| 25 | + assert.throws(() => timerify(syncFunc, { histogram }), { | ||
| 26 | + code: 'ERR_INVALID_ARG_TYPE' | ||
| 27 | + }); | ||
| 28 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,19 @@ | |||
| 1 | + // Test that functions can be wrapped multiple times and verify length and name | ||
| 2 | + // properties are preserved correctly. | ||
| 3 | + | ||
| 4 | + 'use strict'; | ||
| 5 | + | ||
| 6 | + require('../common'); | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + const { timerify } = require('perf_hooks'); | ||
| 9 | + | ||
| 10 | + const m = (a, b = 1) => {}; | ||
| 11 | + const n = timerify(m); | ||
| 12 | + const o = timerify(m); | ||
| 13 | + const p = timerify(n); | ||
| 14 | + assert.notStrictEqual(n, o); | ||
| 15 | + assert.notStrictEqual(n, p); | ||
| 16 | + assert.notStrictEqual(o, p); | ||
| 17 | + assert.strictEqual(n.length, m.length); | ||
| 18 | + assert.strictEqual(n.name, 'timerified m'); | ||
| 19 | + assert.strictEqual(p.name, 'timerified timerified m'); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,24 @@ | |||
| 1 | + // Regression tests for https://github.com/nodejs/node/issues/40623 | ||
| 2 | + // Test that timerify preserves return values and class constructor behavior. | ||
| 3 | + | ||
| 4 | + 'use strict'; | ||
| 5 | + | ||
| 6 | + require('../common'); | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + const { timerify } = require('perf_hooks'); | ||
| 9 | + | ||
| 10 | + assert.strictEqual(timerify(function func() { | ||
| 11 | + return 1; | ||
| 12 | + })(), 1); | ||
| 13 | + assert.strictEqual(timerify(function() { | ||
| 14 | + return 1; | ||
| 15 | + })(), 1); | ||
| 16 | + assert.strictEqual(timerify(() => { | ||
| 17 | + return 1; | ||
| 18 | + })(), 1); | ||
| 19 | + class C {} | ||
| 20 | + const wrap = timerify(C); | ||
| 21 | + assert.ok(new wrap() instanceof C); | ||
| 22 | + assert.throws(() => wrap(), { | ||
| 23 | + name: 'TypeError', | ||
| 24 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments