-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: `--profile` dev: `--profile` to start with profiler `p + enter` to write profile and restart profiler
- Loading branch information
Showing
6 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,10 @@ | ||
--- | ||
"@remix-run/dev": patch | ||
--- | ||
|
||
vite: profiling | ||
|
||
- build: `--profile` | ||
- dev: | ||
- `--profile` to start with profiler | ||
- `p + enter` to write profile and restart profiler |
This file contains 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 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 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 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 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,50 @@ | ||
// Adapted from: | ||
// - https://github.com/vitejs/vite/blob/9fc5d9cb3a1b9df067e00959faa9da43ae03f776/packages/vite/bin/vite.js | ||
// - https://github.com/vitejs/vite/blob/9fc5d9cb3a1b9df067e00959faa9da43ae03f776/packages/vite/src/node/cli.ts | ||
|
||
import fs from "node:fs"; | ||
import type { Session } from "node:inspector"; | ||
import path from "node:path"; | ||
import colors from "picocolors"; | ||
|
||
declare namespace global { | ||
let __remix_profile_session: Session | undefined; | ||
} | ||
|
||
export const getSession = () => global.__remix_profile_session; | ||
|
||
export const start = async (callback?: () => void | Promise<void>) => { | ||
let inspector = await import("node:inspector").then((r) => r.default); | ||
let session = (global.__remix_profile_session = new inspector.Session()); | ||
session.connect(); | ||
session.post("Profiler.enable", () => { | ||
session.post("Profiler.start", callback); | ||
}); | ||
}; | ||
|
||
let profileCount = 0; | ||
|
||
export const stop = (log: (message: string) => void): void | Promise<void> => { | ||
let session = global.__remix_profile_session; | ||
if (!session) return; | ||
return new Promise((res, rej) => { | ||
session!.post("Profiler.stop", (err: any, { profile }: any) => { | ||
// Write profile to disk, upload, etc. | ||
if (!err) { | ||
let outPath = path.resolve( | ||
`./remix-profile-${profileCount++}.cpuprofile` | ||
); | ||
fs.writeFileSync(outPath, JSON.stringify(profile)); | ||
log( | ||
colors.yellow( | ||
`CPU profile written to ${colors.white(colors.dim(outPath))}` | ||
) | ||
); | ||
global.__remix_profile_session = undefined; | ||
res(); | ||
} else { | ||
rej(err); | ||
} | ||
}); | ||
}); | ||
}; |