Skip to content

Commit

Permalink
feat(benchmark): only mode (#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aslemammad committed Sep 18, 2022
1 parent 62c1001 commit 119932f
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 56 deletions.
17 changes: 17 additions & 0 deletions docs/api/index.md
Expand Up @@ -296,6 +296,23 @@ You can use `bench.skip` syntax to skip running certain benchmarks.
})
```

### bench.only

- **Type:** `(name: string, fn: BenchFunction, options?: BenchOptions) => void`

Use `bench.only` to only run certain benchmarks in a given suite. This is useful when debugging.

```ts
import { bench } from 'vitest'

bench.only('normal sorting', () => {
const x = [1, 5, 4, 2, 3]
x.sort((a, b) => {
return a - b
})
})
```

## describe

When you use `test` or `bench` in the top level of file, they are collected as part of the implicit suite for it. Using `describe` you can define a new suite in the current context, as a set of related tests or benchmarks and other nested suites. A suite lets you organize your tests and benchmarks so reports are more clear.
Expand Down
10 changes: 7 additions & 3 deletions packages/vitest/src/runtime/run.ts
Expand Up @@ -238,8 +238,9 @@ export async function runSuite(suite: Suite) {
else {
try {
const beforeAllCleanups = await callSuiteHook(suite, suite, 'beforeAll', [suite])

if (isRunningInBenchmark()) {
await runBenchmarkSuit(suite)
await runBenchmarkSuite(suite)
}
else {
for (let tasksGroup of partitionSuiteChildren(suite)) {
Expand Down Expand Up @@ -301,21 +302,24 @@ function createBenchmarkResult(name: string): BenchmarkResult {
} as BenchmarkResult
}

async function runBenchmarkSuit(suite: Suite) {
async function runBenchmarkSuite(suite: Suite) {
const { Task, Bench } = await importTinybench()
const start = performance.now()

const benchmarkGroup = []
const benchmarkSuiteGroup = []
for (const task of suite.tasks) {
if (task.mode !== 'run')
continue

if (task.type === 'benchmark')
benchmarkGroup.push(task)
else if (task.type === 'suite')
benchmarkSuiteGroup.push(task)
}

if (benchmarkSuiteGroup.length)
await Promise.all(benchmarkSuiteGroup.map(subSuite => runBenchmarkSuit(subSuite)))
await Promise.all(benchmarkSuiteGroup.map(subSuite => runBenchmarkSuite(subSuite)))

if (benchmarkGroup.length) {
const defer = createDefer()
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest/src/runtime/suite.ts
Expand Up @@ -116,7 +116,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
})

const benchmark = createBenchmark(function (name: string, fn = noop, options: BenchOptions) {
const mode = this.skip ? 'skip' : 'run'
const mode = this.only ? 'only' : this.skip ? 'skip' : 'run'

if (!isRunningInBenchmark())
throw new Error('`bench()` is only available in benchmark mode. Run with `vitest bench` instead.')
Expand Down Expand Up @@ -251,14 +251,14 @@ function createTest(fn: (

function createBenchmark(fn: (
(
this: Record<'skip', boolean | undefined>,
this: Record<'skip' | 'only', boolean | undefined>,
name: string,
fn: BenchFunction,
options: BenchOptions
) => void
)) {
const benchmark = createChainable(
['skip'],
['skip', 'only'],
fn,
) as BenchmarkAPI

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/types/benchmark.ts
Expand Up @@ -53,7 +53,7 @@ export interface BenchmarkResult extends TinybenchResult {

export type BenchFunction = (this: BenchFactory) => Promise<void> | void
export type BenchmarkAPI = ChainableFunction<
'skip',
'skip' | 'only',
[name: string, fn: BenchFunction, options?: BenchOptions],
void
> & {
Expand Down

0 comments on commit 119932f

Please sign in to comment.