-
-
Notifications
You must be signed in to change notification settings - Fork 35.8k
benchmark: add test runner hooks and options #63754
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const { finished } = require('node:stream/promises'); | ||
| const reporter = require('../fixtures/empty-test-reporter'); | ||
| const { | ||
| after, | ||
| afterEach, | ||
| before, | ||
| beforeEach, | ||
| describe, | ||
| it, | ||
| } = require('node:test'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| n: [1, 10, 100, 1000], | ||
| hook: ['before', 'after', 'beforeEach', 'afterEach'], | ||
| }, { | ||
| // We don't want to test the reporter here. | ||
| flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'], | ||
| }); | ||
|
|
||
| const hookList = { | ||
| before: before, | ||
| after: after, | ||
| beforeEach: beforeEach, | ||
| afterEach: afterEach, | ||
| }; | ||
|
|
||
| function run(loopAmount, avoidV8Optimization, hookFn) { | ||
| for (let i = 0; i < loopAmount; i++) { | ||
| describe(`${i}`, () => { | ||
| hookFn(() => { | ||
| avoidV8Optimization = i; | ||
| }); | ||
|
|
||
| it(`${i}`, () => { | ||
| avoidV8Optimization = i; | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| return finished(reporter); | ||
| } | ||
|
|
||
| function main(params) { | ||
| // eslint-disable-next-line prefer-const | ||
| let avoidV8Optimization = 0; | ||
|
Comment on lines
+47
to
+48
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why 0 and why let?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is To be 100% honest, I don't quite understand the mechanics for this. I never dug into v8 optimizations before. The variable is there because I decided to follow the existing patterns inside this particular folder. But please check this reference link and this for the comment that originally introduced this specific variable (Seems like the link is not working 100%, opening the discussion so just crtl+f for Obs: I just realized the hooks are noops, and given that this variable works as described, I shouldn't have a noop there, so I'm pushing code so the hooks also have minimal execution within the function. Thanks for calling my attention to this. |
||
| const hookFn = hookList[params.hook]; | ||
|
|
||
| bench.start(); | ||
|
|
||
| run(params.n, avoidV8Optimization, hookFn).then(() => { | ||
| bench.end(params.n); | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const { finished } = require('node:stream/promises'); | ||
| const reporter = require('../fixtures/empty-test-reporter'); | ||
| const { it } = require('node:test'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| n: [1, 10, 100, 1000], | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use calibrate N here. |
||
| option: [ | ||
| 'none', | ||
| 'skip', | ||
| 'skip-with-message', | ||
| 'skip-method', | ||
| 'skip-method-with-message', | ||
| 'todo', | ||
| 'todo-with-message', | ||
| 'todo-method', | ||
| 'todo-method-with-message', | ||
| ], | ||
| }, { | ||
| // We don't want to test the reporter here. | ||
| flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'], | ||
| }); | ||
|
|
||
| const allTests = { | ||
| 'none': (loopAmount, avoidV8Optimization) => { | ||
| for (let i = 0; i < loopAmount; i++) { | ||
| it(`${i}`, () => { | ||
| avoidV8Optimization = i; | ||
| }); | ||
| } | ||
|
|
||
| return finished(reporter); | ||
| }, | ||
| 'skip': (loopAmount) => { | ||
| for (let i = 0; i < loopAmount; i++) { | ||
| it(`${i}`, { skip: true }, () => { | ||
| throw new Error('This test should not run.'); | ||
| }); | ||
| } | ||
|
|
||
| return finished(reporter); | ||
| }, | ||
| 'skip-with-message': (loopAmount) => { | ||
| for (let i = 0; i < loopAmount; i++) { | ||
| it(`${i}`, { skip: 'skip reason' }, () => { | ||
| throw new Error('This test should not run.'); | ||
| }); | ||
| } | ||
|
|
||
| return finished(reporter); | ||
| }, | ||
| 'skip-method': (loopAmount, avoidV8Optimization) => { | ||
| for (let i = 0; i < loopAmount; i++) { | ||
| it(`${i}`, (t) => { | ||
| avoidV8Optimization = i; | ||
| t.skip(); | ||
| }); | ||
| } | ||
|
|
||
| return finished(reporter); | ||
| }, | ||
| 'skip-method-with-message': (loopAmount, avoidV8Optimization) => { | ||
| for (let i = 0; i < loopAmount; i++) { | ||
| it(`${i}`, (t) => { | ||
| avoidV8Optimization = i; | ||
| t.skip('skip reason'); | ||
| }); | ||
| } | ||
|
|
||
| return finished(reporter); | ||
| }, | ||
| 'todo': (loopAmount, avoidV8Optimization) => { | ||
| for (let i = 0; i < loopAmount; i++) { | ||
| it(`${i}`, { todo: true }, () => { | ||
| avoidV8Optimization = i; | ||
| }); | ||
| } | ||
|
|
||
| return finished(reporter); | ||
| }, | ||
| 'todo-with-message': (loopAmount, avoidV8Optimization) => { | ||
| for (let i = 0; i < loopAmount; i++) { | ||
| it(`${i}`, { todo: 'todo reason' }, () => { | ||
| avoidV8Optimization = i; | ||
| }); | ||
| } | ||
|
|
||
| return finished(reporter); | ||
| }, | ||
| 'todo-method': (loopAmount, avoidV8Optimization) => { | ||
| for (let i = 0; i < loopAmount; i++) { | ||
| it(`${i}`, (t) => { | ||
| avoidV8Optimization = i; | ||
| t.todo(); | ||
| }); | ||
| } | ||
|
|
||
| return finished(reporter); | ||
| }, | ||
| 'todo-method-with-message': (loopAmount, avoidV8Optimization) => { | ||
| for (let i = 0; i < loopAmount; i++) { | ||
| it(`${i}`, (t) => { | ||
| avoidV8Optimization = i; | ||
| t.todo('todo reason'); | ||
| }); | ||
| } | ||
|
|
||
| return finished(reporter); | ||
| }, | ||
| }; | ||
|
|
||
| function main({ n, option }) { | ||
| // eslint-disable-next-line prefer-const | ||
| let avoidV8Optimization = 0; | ||
| const runOption = allTests[option]; | ||
|
|
||
| bench.start(); | ||
|
|
||
| runOption(n, avoidV8Optimization).then(() => { | ||
| bench.end(n); | ||
| }); | ||
| } | ||
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.
Are these numbers arbitrarily chosen? Please use https://github.com/nodejs/node/blob/main/benchmark/calibrate-n.js
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.
Yes and No. I took those numbers from another benchmark in the same folder since I didn't know exactly how the configurations were used forward.
That being said, I didn't know about calibrate-n and how it worked, but after some study, I think I understand it now. I'm running it for both this and the PR for mock-timers. I will post the results here as soon as I have them and update the numbers with the best one found.