GeezerVison is showing github.com — perf: pass testfiles at once when `--no-isolate --maxWorkers=1` (#8835) · vitest-dev/vitest@584aa71
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 | .vite-node | 17 | 17 | ltex* | 18 | 18 | .DS_Store | | 19 | +.zed | 19 | 20 | bench/test/*/*/ | 20 | 21 | **/bench.json | 21 | 22 | **/browser/browser.json | @@ -35,4 +36,4 @@ test/browser/html/ | 35 | 36 | test/core/html/ | 36 | 37 | .vitest-attachments | 37 | 38 | explainFiles.txt | 38 | | -.vitest-dump | | 39 | +.vitest-dump |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/pool.ts +51-4Lines changed: 51 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { Awaitable } from '@vitest/utils' | | 2 | +import type { ContextTestEnvironment } from '../types/worker' | 2 | 3 | import type { Vitest } from './core' | 3 | 4 | import type { PoolTask } from './pools/types' | 4 | 5 | import type { TestProject } from './project' | @@ -87,7 +88,7 @@ export function createPool(ctx: Vitest): ProcessPool { | 87 | 88 |
| 88 | 89 | const sorted = await sequencer.sort(specs) | 89 | 90 | const environments = await getSpecificationsEnvironments(specs) | 90 | | - const groups = groupSpecs(sorted) | | 91 | + const groups = groupSpecs(sorted, environments) | 91 | 92 |
| 92 | 93 | const projectEnvs = new WeakMap<TestProject, Partial<NodeJS.ProcessEnv>>() | 93 | 94 | const projectExecArgvs = new WeakMap<TestProject, string[]>() | @@ -330,9 +331,8 @@ function getMemoryLimit(config: ResolvedConfig, pool: string) { | 330 | 331 | return null | 331 | 332 | } | 332 | 333 |
| 333 | | -function groupSpecs(specs: TestSpecification[]) { | 334 | | - // Test files are passed to test runner one at a time, except Typechecker. | 335 | | - // TODO: Should non-isolated test files be passed to test runner all at once? | | 334 | +function groupSpecs(specs: TestSpecification[], environments: Awaited<ReturnType<typeof getSpecificationsEnvironments>>) { | | 335 | + // Test files are passed to test runner one at a time, except for Typechecker or when "--maxWorker=1 --no-isolate" | 336 | 336 | type SpecsForRunner = TestSpecification[] | 337 | 337 |
| 338 | 338 | // Tests in a single group are executed with `maxWorkers` parallelism. | @@ -346,6 +346,43 @@ function groupSpecs(specs: TestSpecification[]) { | 346 | 346 | // Type tests are run in a single group, per project | 347 | 347 | const typechecks: Record<string, TestSpecification[]> = {} | 348 | 348 |
| | 349 | + const serializedEnvironmentOptions = new Map<ContextTestEnvironment, string>() | | 350 | + | | 351 | + function getSerializedOptions(env: ContextTestEnvironment) { | | 352 | + const options = serializedEnvironmentOptions.get(env) | | 353 | + | | 354 | + if (options) { | | 355 | + return options | | 356 | + } | | 357 | + | | 358 | + const serialized = JSON.stringify(env.options) | | 359 | + serializedEnvironmentOptions.set(env, serialized) | | 360 | + return serialized | | 361 | + } | | 362 | + | | 363 | + function isEqualEnvironments(a: TestSpecification, b: TestSpecification) { | | 364 | + const aEnv = environments.get(a) | | 365 | + const bEnv = environments.get(b) | | 366 | + | | 367 | + if (!aEnv && !bEnv) { | | 368 | + return true | | 369 | + } | | 370 | + | | 371 | + if (!aEnv || !bEnv || aEnv.name !== bEnv.name) { | | 372 | + return false | | 373 | + } | | 374 | + | | 375 | + if (!aEnv.options && !bEnv.options) { | | 376 | + return true | | 377 | + } | | 378 | + | | 379 | + if (!aEnv.options || !bEnv.options) { | | 380 | + return false | | 381 | + } | | 382 | + | | 383 | + return getSerializedOptions(aEnv) === getSerializedOptions(bEnv) | | 384 | + } | | 385 | + | 349 | 386 | specs.forEach((spec) => { | 350 | 387 | if (spec.pool === 'typescript') { | 351 | 388 | typechecks[spec.project.name] ||= [] | @@ -361,6 +398,7 @@ function groupSpecs(specs: TestSpecification[]) { | 361 | 398 | } | 362 | 399 |
| 363 | 400 | const maxWorkers = resolveMaxWorkers(spec.project) | | 401 | + const isolate = spec.project.config.isolate | 364 | 402 | groups[order] ||= { specs: [], maxWorkers } | 365 | 403 |
| 366 | 404 | // Multiple projects with different maxWorkers but same groupId | @@ -370,6 +408,15 @@ function groupSpecs(specs: TestSpecification[]) { | 370 | 408 | throw new Error(`Projects "${last}" and "${spec.project.name}" have different 'maxWorkers' but same 'sequence.groupId'.\nProvide unique 'sequence.groupId' for them.`) | 371 | 409 | } | 372 | 410 |
| | 411 | + // Non-isolated single worker can receive all files at once | | 412 | + if (isolate === false && maxWorkers === 1) { | | 413 | + const previous = groups[order].specs[0]?.[0] | | 414 | + | | 415 | + if (previous && previous.project.name === spec.project.name && isEqualEnvironments(spec, previous)) { | | 416 | + return groups[order].specs[0].push(spec) | | 417 | + } | | 418 | + } | | 419 | + | 373 | 420 | groups[order].specs.push([spec]) | 374 | 421 | }) | 375 | 422 |
| |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { test } from "vitest" | | 2 | + | | 3 | +test("a", () => { }) |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { test } from "vitest" | | 2 | + | | 3 | +test("b", () => { }) |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { test } from "vitest" | | 2 | + | | 3 | +test("c", () => { }) |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { test } from 'vitest' | | 2 | + | | 3 | +test('print config', () => { | | 4 | + // @ts-expect-error -- internal | | 5 | + console.log(JSON.stringify(globalThis.__vitest_worker__.ctx.files.map(file => file.filepath))) | | 6 | +}) |
Collapse file Copy file name to clipboardExpand all lines: test/config/test/pool.test.ts +40-3Lines changed: 40 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { SerializedConfig } from 'vitest' | 2 | 2 | import type { TestUserConfig } from 'vitest/node' | | 3 | +import { normalize } from 'pathe' | 3 | 4 | import { assert, describe, expect, test } from 'vitest' | 4 | | -import { runVitest } from '../../test-utils' | | 5 | +import { runVitest, StableTestFileOrderSorter } from '../../test-utils' | 5 | 6 |
| 6 | 7 | describe.each(['forks', 'threads', 'vmThreads', 'vmForks'])('%s', async (pool) => { | 7 | 8 | test('resolves top-level pool', async () => { | @@ -51,8 +52,39 @@ test('project level pool options overwrites top-level', async () => { | 51 | 52 | expect(config.fileParallelism).toBe(false) | 52 | 53 | }) | 53 | 54 |
| 54 | | -async function getConfig(options: Partial<TestUserConfig>, cliOptions: Partial<TestUserConfig> = {}) { | 55 | | - let config: SerializedConfig | undefined | | 55 | +test('isolated single worker pool receives single testfile at once', async () => { | | 56 | + const files = await getConfig<string[]>({ | | 57 | + maxWorkers: 1, | | 58 | + isolate: true, | | 59 | + sequence: { sequencer: StableTestFileOrderSorter }, | | 60 | + }, { include: ['print-testfiles.test.ts', 'a.test.ts', 'b.test.ts', 'c.test.ts'] }) | | 61 | + | | 62 | + expect(files.map(normalizeFilename)).toMatchInlineSnapshot(` | | 63 | + [ | | 64 | + "<process-cwd>/fixtures/pool/print-testfiles.test.ts", | | 65 | + ] | | 66 | + `) | | 67 | +}) | | 68 | + | | 69 | +test('non-isolated single worker pool receives all testfiles at once', async () => { | | 70 | + const files = await getConfig<string[]>({ | | 71 | + maxWorkers: 1, | | 72 | + isolate: false, | | 73 | + sequence: { sequencer: StableTestFileOrderSorter }, | | 74 | + }, { include: ['print-testfiles.test.ts', 'a.test.ts', 'b.test.ts', 'c.test.ts'] }) | | 75 | + | | 76 | + expect(files.map(normalizeFilename)).toMatchInlineSnapshot(` | | 77 | + [ | | 78 | + "<process-cwd>/fixtures/pool/a.test.ts", | | 79 | + "<process-cwd>/fixtures/pool/b.test.ts", | | 80 | + "<process-cwd>/fixtures/pool/c.test.ts", | | 81 | + "<process-cwd>/fixtures/pool/print-testfiles.test.ts", | | 82 | + ] | | 83 | + `) | | 84 | +}) | | 85 | + | | 86 | +async function getConfig<T = SerializedConfig>(options: Partial<TestUserConfig>, cliOptions: Partial<TestUserConfig> = {}): Promise<T> { | | 87 | + let config: T | undefined | 56 | 88 |
| 57 | 89 | await runVitest({ | 58 | 90 | root: './fixtures/pool', | @@ -66,3 +98,8 @@ async function getConfig(options: Partial<TestUserConfig>, cliOptions: Partial<T | 66 | 98 | assert(config) | 67 | 99 | return config | 68 | 100 | } | | 101 | + | | 102 | +function normalizeFilename(filename: string) { | | 103 | + return normalize(filename) | | 104 | + .replace(normalize(process.cwd()), '<process-cwd>') | | 105 | +} |
|