From 4e0ef29d1cf514bc1a9f6c0ddfc8cfe4abfe056d Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 4 Feb 2025 16:03:27 +0000 Subject: [PATCH] test: add benchmarks for dev server initial build (#30742) --- packages/kit/test/write-types.bench.ts | 18 ++++++++++---- packages/nuxt/test/build.bench.ts | 33 ++++++++++++++++++++++++++ packages/nuxt/test/load-nuxt.bench.ts | 12 ++++++++-- 3 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 packages/nuxt/test/build.bench.ts diff --git a/packages/kit/test/write-types.bench.ts b/packages/kit/test/write-types.bench.ts index 92065ad234..751eaf3cff 100644 --- a/packages/kit/test/write-types.bench.ts +++ b/packages/kit/test/write-types.bench.ts @@ -1,17 +1,25 @@ import { fileURLToPath } from 'node:url' -import { afterAll, bench, describe } from 'vitest' -import { join, normalize } from 'pathe' +import { rm } from 'node:fs/promises' +import { afterAll, beforeAll, bench, describe } from 'vitest' +import { join, normalize, resolve } from 'pathe' import { withoutTrailingSlash } from 'ufo' import { loadNuxt, writeTypes } from '@nuxt/kit' +import type { Nuxt } from 'nuxt/schema' -describe('writeTypes', async () => { +describe('writeTypes', () => { const relativeDir = join('../../..', 'test/fixtures/basic-types') const path = withoutTrailingSlash(normalize(fileURLToPath(new URL(relativeDir, import.meta.url)))) - const nuxt = await loadNuxt({ cwd: path }) + let nuxt: Nuxt + + beforeAll(async () => { + nuxt = await loadNuxt({ cwd: path }) + await rm(resolve(path, '.nuxt'), { recursive: true, force: true }) + }, 20_000) + afterAll(async () => { await nuxt.close() - }) + }, 20_000) bench('writeTypes in the basic-types fixture', async () => { await writeTypes(nuxt) diff --git a/packages/nuxt/test/build.bench.ts b/packages/nuxt/test/build.bench.ts new file mode 100644 index 0000000000..9517ec4905 --- /dev/null +++ b/packages/nuxt/test/build.bench.ts @@ -0,0 +1,33 @@ +import { fileURLToPath } from 'node:url' +import { rm } from 'node:fs/promises' +import { beforeAll, bench, describe } from 'vitest' +import { join, normalize } from 'pathe' +import { withoutTrailingSlash } from 'ufo' +import { build, loadNuxt } from 'nuxt' + +const basicTestFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../test/fixtures/basic', import.meta.url)))) + +describe('build', () => { + beforeAll(async () => { + await rm(join(basicTestFixtureDir, '.nuxt'), { recursive: true, force: true }) + }) + + bench('initial dev server build in the basic test fixture', async () => { + const nuxt = await loadNuxt({ + cwd: basicTestFixtureDir, + ready: true, + overrides: { + dev: true, + sourcemap: false, + builder: { + bundle: () => Promise.resolve(), + }, + }, + }) + await new Promise((resolve) => { + nuxt.hook('build:done', () => resolve()) + build(nuxt) + }) + await nuxt.close() + }) +}) diff --git a/packages/nuxt/test/load-nuxt.bench.ts b/packages/nuxt/test/load-nuxt.bench.ts index f3e705960e..2325894b9c 100644 --- a/packages/nuxt/test/load-nuxt.bench.ts +++ b/packages/nuxt/test/load-nuxt.bench.ts @@ -1,6 +1,7 @@ import { fileURLToPath } from 'node:url' -import { bench, describe } from 'vitest' -import { normalize } from 'pathe' +import { rm } from 'node:fs/promises' +import { beforeAll, bench, describe } from 'vitest' +import { join, normalize } from 'pathe' import { withoutTrailingSlash } from 'ufo' import { loadNuxt } from 'nuxt' @@ -8,6 +9,13 @@ const emptyDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../ const basicTestFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../test/fixtures/basic', import.meta.url)))) describe('loadNuxt', () => { + beforeAll(async () => { + await Promise.all([ + rm(join(emptyDir, '.nuxt'), { recursive: true, force: true }), + rm(join(basicTestFixtureDir, '.nuxt'), { recursive: true, force: true }), + ]) + }) + bench('loadNuxt in an empty directory', async () => { const nuxt = await loadNuxt({ cwd: emptyDir,