Skip to content
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

feat: add per-task untimed lifecycle hooks #33

Merged
merged 6 commits into from
Mar 4, 2023

Conversation

achingbrain
Copy link
Contributor

@achingbrain achingbrain commented Feb 21, 2023

Adds four lifecycle hooks that can be configured to run on a per-task basis without impacting the test timings:

  • beforeAll: run once before iterations of the task start
  • beforeEach: run before each iteration of the task
  • afterEach: run after each iteration of the task
  • afterAll: run after all iterations finish

A contrived example:

suite.add('my task', async () => {
  // the timed function
}, {
  beforeAll: () => {
    // start a server
  },
  beforeEach: () => {
    // set up some data that the timed function will mutate
  },
  afterEach: () => {
    // tear down any mutated data or maybe verify that the timed function did what it claims to do
  },
  afterAll: () => {
    // stop the server
  }
})

This is useful if, for example, you are testing several implementations of the same functionality and need to perform different operations on each implementation to bring them into the same state so that the timing data from the task function is useful data.

Fixes #32

Adds four lifecycle hooks that can be configured to run on a per-task
basis without impacting the test timings:

- `before`: run once before iterations of the task start
- `beforeEach`: run before each iteration of the task
- `afterEach`: run after each iteration of the task
- `after`: run after all iterations finish

A contrived example:

```js
suite.add('my task', async () => {
  // the timed function
}, {
  before: () => {
    // start a server
  },
  beforeEach: () => {
    // set up some data that the timed function will mutate
  },
  afterEach: () => {
    // tear down any mutated data
  },
  after: () => {
    // stop the server
  }
})
```

This is useful if, for example, you are testing several implementations
of the same functionality and need to perform different operations on
each implementation to bring them into the same state that the timing
data from the task function is useful data.
@achingbrain achingbrain mentioned this pull request Feb 21, 2023
@@ -38,7 +38,7 @@
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.26.0",
"nano-staged": "^0.5.0",
"size-limit": "^8.0.1",
"size-limit": "^7.0.8",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this change, npm i fails with:

% npm i
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/size-limit
npm ERR!   dev size-limit@"^8.2.4" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer size-limit@"7.0.8" from @size-limit/[email protected]
npm ERR! node_modules/@size-limit/preset-small-lib
npm ERR!   dev @size-limit/preset-small-lib@"^7.0.4" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! 
npm ERR! For a full report see:
npm ERR! /Users/alex/.npm/_logs/2023-02-21T11_08_37_616Z-eresolve-report.txt

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/alex/.npm/_logs/2023-02-21T11_08_37_616Z-debug-0.log

If you update size-limit, @size-limit/preset-small-li and @size-limit/time all to the latest versions, conflicting versions of esbuild are pulled in which tsup tries to pass the watch flag to - this was recently removed and errors out, so the simplest thing to do here seems to be to downgrade size-limit.

@achingbrain
Copy link
Contributor Author

achingbrain commented Feb 22, 2023

I've changed the names before -> beforeAll, after -> afterAll as from this comment I think the jest style is your preference rather than the mocha style.

@achingbrain
Copy link
Contributor Author

@Aslemammad are there any changes you'd like to be made or is there anything else I can do here?

@Aslemammad
Copy link
Member

Aslemammad commented Feb 27, 2023 via email

@Aslemammad Aslemammad merged commit 0c6a3f3 into tinylibs:main Mar 4, 2023
achingbrain added a commit to ipfs/helia that referenced this pull request Mar 4, 2023
tinylibs/tinybench#33 has been merged so
update the minimum used version
@Aslemammad
Copy link
Member

Thank you so much, I forgot to write this one :)

achingbrain added a commit to ipfs/helia that referenced this pull request Mar 9, 2023
tinylibs/tinybench#33 has been merged so update
the minimum used version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Per-iteration setup?
2 participants