-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 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
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,44 @@ | ||
// 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 = getSession(); | ||
if (!session) return; | ||
return new Promise((res, rej) => { | ||
session!.post("Profiler.stop", (err, { profile }) => { | ||
if (err) return rej(err); | ||
let outPath = path.resolve(`./remix-${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(); | ||
}); | ||
}); | ||
}; |