-
-
Notifications
You must be signed in to change notification settings - Fork 35.8k
Add benchmarks for node only mode and mock timers. #64097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
luanmuniz
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
luanmuniz:benchmark-test-runner-only-mock-timers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+306
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,262 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('node:assert'); | ||
| const { test } = require('node:test'); | ||
| const nodeTimersPromises = require('node:timers/promises'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| n: [1, 10, 100, 1000], | ||
| mode: [ | ||
| 'enable-empty-apis', | ||
| 'enable-setTimeout', | ||
| 'enable-setInterval', | ||
| 'enable-setImmediate', | ||
| 'enable-Date', | ||
| 'enable-scheduler.wait', | ||
| 'enable-AbortSignal.timeout', | ||
| 'enable-all', | ||
| 'enable-default', | ||
| 'setTimeout', | ||
| 'setInterval', | ||
| 'setImmediate', | ||
| 'scheduler.wait', | ||
| 'AbortSignal.timeout', | ||
| 'Date', | ||
| 'setTime', | ||
| 'runAll', | ||
| ], | ||
| }, { | ||
| // We don't want to test the reporter here. | ||
| flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'], | ||
| }); | ||
|
|
||
| function benchmarkEnable(n, mode) { | ||
| const enableMode = mode.replace('enable-', ''); | ||
| let enableOptions = { apis: [enableMode] }; | ||
|
|
||
| if (enableMode === 'all') { | ||
| enableOptions.apis = ['setTimeout', 'setInterval', 'setImmediate', 'Date', 'scheduler.wait', 'AbortSignal.timeout']; | ||
| } | ||
|
|
||
| if (enableMode === 'empty-apis') { | ||
| enableOptions.apis = []; | ||
| } | ||
|
|
||
| if (enableMode === 'default') { | ||
| enableOptions = undefined; | ||
| } | ||
|
|
||
| test((t) => { | ||
| bench.start(); | ||
|
|
||
| for (let i = 0; i < n; i++) { | ||
| t.mock.timers.enable(enableOptions); | ||
| t.mock.timers.reset(); | ||
| } | ||
|
|
||
| bench.end(n); | ||
| }); | ||
| } | ||
|
|
||
| function benchmarkSetTimeout(n) { | ||
| test((t) => { | ||
| let noDead; | ||
|
|
||
| t.mock.timers.enable({ apis: ['setTimeout'] }); | ||
| bench.start(); | ||
|
|
||
| for (let i = 0; i < n; i++) { | ||
| setTimeout(() => { | ||
| noDead = i; | ||
| }, i + 1); | ||
| } | ||
|
|
||
| t.mock.timers.tick(n + 1); | ||
| bench.end(n); | ||
|
|
||
| assert.strictEqual(noDead, n - 1); | ||
| }); | ||
| } | ||
|
|
||
| function benchmarkSetInterval(n) { | ||
| test((t) => { | ||
| let noDead = 0; | ||
|
|
||
| t.mock.timers.enable({ apis: ['setInterval'] }); | ||
|
|
||
| setInterval(() => { | ||
| noDead++; | ||
| }, 1); | ||
|
|
||
| bench.start(); | ||
|
|
||
| t.mock.timers.tick(n); | ||
|
|
||
| bench.end(n); | ||
|
|
||
| assert.strictEqual(noDead, n); | ||
| }); | ||
| } | ||
|
|
||
| function benchmarkSetImmediate(n) { | ||
| test((t) => { | ||
| let noDead; | ||
|
|
||
| t.mock.timers.enable({ apis: ['setImmediate'] }); | ||
|
|
||
| bench.start(); | ||
|
|
||
| for (let i = 0; i < n; i++) { | ||
| setImmediate(() => { | ||
| noDead = i; | ||
| }); | ||
| } | ||
|
|
||
| t.mock.timers.tick(0); | ||
| bench.end(n); | ||
|
|
||
| assert.strictEqual(noDead, n - 1); | ||
| }); | ||
| } | ||
|
|
||
| function benchmarkSchedulerWait(n) { | ||
| test(async (t) => { | ||
| const promises = []; | ||
| let noDead; | ||
|
|
||
| t.mock.timers.enable({ apis: ['scheduler.wait'] }); | ||
|
|
||
| bench.start(); | ||
|
|
||
| for (let i = 0; i < n; i++) { | ||
| promises.push(nodeTimersPromises.scheduler.wait(i + 1).then(() => { | ||
| noDead = i; | ||
| })); | ||
| } | ||
|
|
||
| t.mock.timers.tick(n + 1); | ||
| await Promise.all(promises); | ||
| bench.end(n); | ||
|
|
||
| assert.strictEqual(noDead, n - 1); | ||
| }); | ||
| } | ||
|
|
||
| function benchmarkAbortSignalTimeout(n) { | ||
| test((t) => { | ||
| let noDead; | ||
|
|
||
| t.mock.timers.enable({ apis: ['AbortSignal.timeout'] }); | ||
|
|
||
| bench.start(); | ||
|
|
||
| for (let i = 0; i < n; i++) { | ||
| noDead = AbortSignal.timeout(i + 1); | ||
| } | ||
|
|
||
| t.mock.timers.tick(n + 1); | ||
| bench.end(n); | ||
|
|
||
| assert.strictEqual(noDead.aborted, true); | ||
| }); | ||
| } | ||
|
|
||
| function benchmarkDate(n) { | ||
| test((t) => { | ||
| let noDead; | ||
|
|
||
| t.mock.timers.enable({ apis: ['Date'] }); | ||
|
|
||
| bench.start(); | ||
|
|
||
| for (let i = 0; i < n; i++) { | ||
| noDead = Date.now(); | ||
| } | ||
|
|
||
| bench.end(n); | ||
|
|
||
| assert.strictEqual(noDead, 0); | ||
| }); | ||
| } | ||
|
|
||
| function benchmarkSetTime(n) { | ||
| test((t) => { | ||
| let noDead; | ||
|
|
||
| t.mock.timers.enable({ apis: ['Date'] }); | ||
|
|
||
| bench.start(); | ||
|
|
||
| for (let i = 0; i < n; i++) { | ||
| t.mock.timers.setTime(i); | ||
| noDead = Date.now(); | ||
| } | ||
|
|
||
| bench.end(n); | ||
|
|
||
| assert.strictEqual(noDead, n - 1); | ||
| }); | ||
| } | ||
|
|
||
| function benchmarkRunAll(n) { | ||
| test((t) => { | ||
| let noDead = 0; | ||
|
|
||
| t.mock.timers.enable({ apis: ['setTimeout'] }); | ||
|
|
||
| for (let i = 0; i < n; i++) { | ||
| setTimeout(() => { | ||
| noDead++; | ||
| }, i + 1); | ||
| } | ||
|
|
||
| bench.start(); | ||
|
|
||
| t.mock.timers.runAll(); | ||
|
|
||
| bench.end(n); | ||
|
|
||
| assert.strictEqual(noDead, n); | ||
| }); | ||
| } | ||
|
|
||
| function main({ n, mode }) { | ||
| switch (mode) { | ||
| case 'enable-empty-apis': | ||
| case 'enable-setTimeout': | ||
| case 'enable-setInterval': | ||
| case 'enable-setImmediate': | ||
| case 'enable-Date': | ||
| case 'enable-scheduler.wait': | ||
| case 'enable-AbortSignal.timeout': | ||
| case 'enable-all': | ||
| case 'enable-default': | ||
| benchmarkEnable(n, mode); | ||
| break; | ||
| case 'setTimeout': | ||
| benchmarkSetTimeout(n); | ||
| break; | ||
| case 'setInterval': | ||
| benchmarkSetInterval(n); | ||
| break; | ||
| case 'setImmediate': | ||
| benchmarkSetImmediate(n); | ||
| break; | ||
| case 'scheduler.wait': | ||
| benchmarkSchedulerWait(n); | ||
| break; | ||
| case 'AbortSignal.timeout': | ||
| benchmarkAbortSignalTimeout(n); | ||
| break; | ||
| case 'Date': | ||
| benchmarkDate(n); | ||
| break; | ||
| case 'setTime': | ||
| benchmarkSetTime(n); | ||
| break; | ||
| case 'runAll': | ||
| benchmarkRunAll(n); | ||
| break; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const { finished } = require('node:stream/promises'); | ||
| const reporter = require('../fixtures/empty-test-reporter'); | ||
| const { test } = require('node:test'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| n: [1, 10, 100, 1000], | ||
| selected: [1], | ||
| }, { | ||
| // We don't want to test the reporter here. | ||
| flags: [ | ||
| '--test-reporter=./benchmark/fixtures/empty-test-reporter.js', | ||
| '--test-only', | ||
| ], | ||
| }); | ||
|
|
||
| async function run({ n, selected }) { | ||
| // eslint-disable-next-line no-unused-vars | ||
| let avoidV8Optimization; | ||
|
|
||
| for (let i = 0; i < selected; i++) { | ||
| test(`selected-${i}`, { only: true }, () => { | ||
| avoidV8Optimization = i; | ||
| }); | ||
| } | ||
|
|
||
| for (let i = 0; i < n; i++) { | ||
| test(`not-selected-${i}`, () => { | ||
| throw new Error(`This test ${i} should not run.`); | ||
| }); | ||
| } | ||
|
|
||
| return finished(reporter); | ||
| } | ||
|
|
||
| function main(params) { | ||
| bench.start(); | ||
|
|
||
| run(params).then(() => { | ||
| bench.end(params.n); | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unnecessary; it's a clojure. Adding an assert.ok is probably more useful.