-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
test: add test profiling script #7086
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
Open
Bertie690
wants to merge
15
commits into
pagefaultgames:beta
Choose a base branch
from
Bertie690:flamegraph
base: beta
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
22d9fe8
test: add test profiling script + `commander.js`
Bertie690 15da5fb
fix type errors
Bertie690 6df7146
fix this one ranodm formatting issue in fabi's code
Bertie690 d8de515
Update profiling.js
Bertie690 a71a9e1
add biome rule to ban importing from `commander` inside scripts dir
Bertie690 1b40411
Stopped using speedsrope which doesn't work
Bertie690 e8fd5c4
fix import issue
Bertie690 4a845ce
fix command crashing a bit
Bertie690 191a6ac
fix
Bertie690 60af132
Update process.js licensing info
Bertie690 a38c2f7
Update copyright yr
Bertie690 ecaa7eb
fix type errors
Bertie690 c47d8d9
Merge branch 'beta' into flamegraph
Bertie690 f95f14e
Merge remote-tracking branch 'upstream/beta' into flamegraph
Bertie690 56d3a80
Merge remote-tracking branch 'upstream/beta' into flamegraph
Bertie690 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ yarn-error.log* | |
| pnpm-debug.log* | ||
| lerna-debug.log* | ||
|
|
||
| temp | ||
| node_modules | ||
| dist | ||
| dist-ssr | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: 2025-2026 Pagefault Games | ||
| * SPDX-FileContributor: Bertie690 | ||
| * | ||
| * SPDX-License-Identifier: AGPL-3.0-only | ||
| */ | ||
|
|
||
| import { spawnSync } from "node:child_process"; | ||
|
|
||
| /** | ||
| * Check if a given command exists in the system's `PATH`. | ||
| * Equivalent to the `which`/`where` commands on Unix/Windows. | ||
| * @param {string} command - The command to check | ||
| * @returns {boolean} Whether the command exists. | ||
| */ | ||
| export function commandExists(command) { | ||
| const cmd = process.platform === "win32" ? "where" : "which"; | ||
| return spawnSync(cmd, [command], { stdio: "ignore" }).status === 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: 2026 Pagefault Games | ||
| * SPDX-FileContributor: Bertie690 | ||
| * | ||
| * SPDX-License-Identifier: AGPL-3.0-only | ||
| * | ||
| * This script wraps Vitest's test runner with node's built-in V8 profiler. | ||
| * Any extra CLI arguments are passed directly to `vitest run`. | ||
| */ | ||
Bertie690 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import { copyFile, glob, mkdir, mkdtemp, rm } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { basename, join } from "node:path"; | ||
| import { Command } from "@commander-js/extra-typings"; | ||
| import chalk from "chalk"; | ||
| import { startVitest } from "vitest/node"; | ||
| import { defaultCommanderHelpArgs } from "../helpers/arguments.js"; | ||
|
|
||
| const version = "1.0.0"; | ||
|
|
||
| console.log(chalk.hex("#ffa500")(`📈 Test Profiler - v${version}\n`)); | ||
|
|
||
| const testProfile = new Command("pnpm test:profile") | ||
| .description("Run Vitest with Node's V8 profiler and generate processed profiling output.") | ||
| .helpOption("-h, --help", "Show this help message.") | ||
| .version(version, "-v, --version", "Show the version number.") | ||
| .option("-o, --output <path>", "Directory to which V8 profiler output will be written.", "./temp/vitest-profile") | ||
| .option("-c, --cpu", "Enable CPU profiling.", true) | ||
| .option("--no-cpu", "Disable CPU profiling.") | ||
| .option("-m, --memory", "Enable memory profiling.", true) | ||
| .option("--no-memory", "Disable memory profiling.") | ||
| .option("--clean", "Whether to clean the output directory before writing new profiles.") | ||
| .argument("<vitest-args...>", "Arguments to pass directly to Vitest.") | ||
| .configureHelp(defaultCommanderHelpArgs) | ||
| // only show help on argument parsing errors, not on test failures | ||
| .showHelpAfterError(true) | ||
| .parse(); | ||
|
|
||
| const { output, cpu, memory, clean } = testProfile.opts(); | ||
| /** The directory to write profiling outputs to. (May or may not be the final destination.) */ | ||
| let outputDir = output; | ||
|
|
||
| if (!cpu && !memory) { | ||
| testProfile.error("Cannot disable both CPU and memory profiling!"); | ||
| } | ||
|
|
||
| testProfile.showHelpAfterError(false); | ||
|
|
||
| /** | ||
| * @returns {Promise<void>} | ||
| */ | ||
| async function main() { | ||
| if (clean) { | ||
| await rm(outputDir, { recursive: true, force: true }); | ||
| await mkdir(outputDir, { recursive: true }); | ||
| } else { | ||
| // output profile files to a temp directory before moving them to the desired location, ensuring both exist | ||
| outputDir = await mkdtemp(join(tmpdir(), "vitest-profile-"), { encoding: "utf-8" }); | ||
| } | ||
|
|
||
| console.log(chalk.grey("Running Vitest with V8 profiler...")); | ||
|
|
||
| /** @type {string[]} */ | ||
| const execArgv = []; | ||
| if (cpu) { | ||
| execArgv.push("--cpu-prof", `--cpu-prof-dir=${outputDir}`); | ||
| } | ||
| if (memory) { | ||
| execArgv.push("--heap-prof", `--heap-prof-dir=${outputDir}`); | ||
| } | ||
|
|
||
| const vitest = await startVitest("test", [...testProfile.args, "no-file-parallelism"], { | ||
| execArgv, | ||
| }); | ||
| // NB: This sets `process.exitCode` to a non-zero value if it fails | ||
| await vitest.close(); | ||
| if (process.exitCode) { | ||
| return; | ||
| } | ||
|
|
||
| const [cpuProfile, memoryProfile] = await Promise.all([ | ||
| (await glob(join(outputDir, "*.cpuprofile")).next()).value, | ||
| (await glob(join(outputDir, "*.heapprofile")).next()).value, | ||
| ]); | ||
| if (!cpuProfile && !memoryProfile) { | ||
| console.error(chalk.red.bold("No CPU or memory profile found!")); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| if (!clean) { | ||
| await Promise.all([ | ||
| cpuProfile && copyFile(cpuProfile, join(output, basename(cpuProfile))), | ||
| memoryProfile && copyFile(memoryProfile, join(output, basename(memoryProfile))), | ||
| ]); | ||
| } | ||
|
|
||
| if (cpuProfile) { | ||
| console.log("Wrote processed CPU profile to:", chalk.bold.blue(cpuProfile)); | ||
| } | ||
| if (memoryProfile) { | ||
| console.log("Wrote processed memory profile to:", chalk.bold.blue(memoryProfile)); | ||
| } | ||
|
|
||
| console.log(chalk.green.bold("Profiling complete!")); | ||
| } | ||
|
|
||
| await main(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.