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(vitest): support hooks in bench runner #5076

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

adriencaccia
Copy link
Contributor

Fix #5075

Copy link

netlify bot commented Jan 30, 2024

Deploy Preview for fastidious-cascaron-4ded94 canceled.

Name Link
🔨 Latest commit b807000
🔍 Latest deploy log https://app.netlify.com/sites/fastidious-cascaron-4ded94/deploys/65b8f0eeeb851400082ad790

@@ -90,7 +94,12 @@ async function runBenchmarkSuite(suite: Suite, runner: NodeBenchmarkRunner) {
await task.warmup()
tasks.push([
await new Promise<BenchTask>(resolve => setTimeout(async () => {
resolve(await task.run())
let beforeEachCleanups: HookCleanupCallback[] = []
beforeEachCleanups = await callSuiteHook(benchmark.suite, benchmark, 'beforeEach', runner, [benchmark.context, benchmark.suite])
Copy link
Member

Choose a reason for hiding this comment

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

Can we use tinybench's hooks? I don't have any issues with this, but just asking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting idea!
I think we might need to refine what each hook does, since in benchmarking there is one more level of hooks.

At the moment, with my implementation, this is what would happen (I added some comment at the end to see the execution order):

import {
  afterAll,
  afterEach,
  beforeAll,
  beforeEach,
  bench,
  describe,
} from 'vitest'

describe('hooks', () => {
  beforeAll(() => {
    console.log('beforeAll')
  })
  beforeEach(() => {
    console.log('beforeEach')
  })
  afterEach(() => {
    console.log('afterEach')
  })
  afterAll(() => {
    console.log('afterAll')
  })

  bench('one', () => {
    console.log('bench one')
  }, {
    setup: () => {
      console.log('setup')
    },
    teardown: () => {
      console.log('tearDown')
    },
    iterations: 10,
    time: 1,
  })
})

// when the bench is executed, the following should be logged

// beforeAll
// beforeEach
// setup     x10
// bench one x10
// tearDown  x10
// afterEach
// beforeAll

Do you think it is what should be expected?

As far as using the hooks from tinybench, I can map the *Each hooks from vitest to the Task *All hooks from tinybench.

Copy link
Member

Choose a reason for hiding this comment

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

Oh interesting edge case, nice, I think let's keep it as is and this should be documented somewhere.

After all, @sheremet-va might have some interesting ideas and insights around this.

Copy link
Member

Choose a reason for hiding this comment

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

I think it makes more sense to use Vitest's hooks. They are shown in the reporter for example if they take too long.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I will go and try to add a test that verify the order of execution of the hooks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will switch the PR to draft for the moment, and mark it as ready when I will have added the tests

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.

Support hooks in bench API
3 participants