-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for profiling through SANITY_DEBUG_PROFILING
- Loading branch information
Showing
7 changed files
with
118 additions
and
42 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 |
---|---|---|
@@ -1,3 +1,38 @@ | ||
import {lstatSync} from 'node:fs' | ||
import {join} from 'node:path' | ||
|
||
import debugIt from 'debug' | ||
|
||
export const debug = debugIt('sanity:core') | ||
|
||
function isDir(path: string): boolean { | ||
try { | ||
return lstatSync(path).isDirectory() | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
/** | ||
* Runs a function such that it will be profiled when the environment variable | ||
* SANITY_DEBUG_PROFILING is set to a directory. A file (starting with `key`) will | ||
* be placed in said directory. The generated file can be inspected by using the | ||
* Speedscpe NPM package: `speedscope ${filename}` opens a UI in the browser. | ||
*/ | ||
export async function withTracingProfiling<T>(key: string, fn: () => Promise<T>): Promise<T> { | ||
const dir = process.env.SANITY_DEBUG_PROFILING | ||
if (!dir) return await fn() | ||
|
||
if (!isDir(dir)) | ||
throw new Error(`SANITY_DEBUG_PROFILING (${JSON.stringify(dir)}) must be set to a directory`) | ||
|
||
let profiling | ||
try { | ||
profiling = await import('./util/profiling') | ||
} catch (err) { | ||
throw new Error(`Failed to load SANITY_DEBUG_PROFILING: ${err}`) | ||
} | ||
|
||
const filenamePrefix = join(dir, key) | ||
return profiling.withTracing(filenamePrefix, fn) | ||
} |
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
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,30 @@ | ||
import {writeFileSync} from 'node:fs' | ||
import {close as closeInspector, open as openInspector, Session} from 'node:inspector/promises' | ||
|
||
// This file should not be imported directly since it depends on | ||
// `inspector/promises` which is only available since Node v19 (and we want to | ||
// support earlier Node versions as well. | ||
|
||
/** | ||
* Runs a function with a tracing profiler and writes the result into a file. | ||
* | ||
* @param filenamePrefix - The filename where the report will be written. The full name | ||
* will be `{filenamePrefix}-{random}.cpuprofile`. | ||
*/ | ||
export async function withTracing<T>(filenamePrefix: string, fn: () => Promise<T>): Promise<T> { | ||
// Make it available in the Chrome DevTools as well | ||
|
||
openInspector() | ||
const session = new Session() | ||
session.connect() | ||
await session.post('Profiler.enable') | ||
await session.post('Profiler.start') | ||
try { | ||
return await fn() | ||
} finally { | ||
closeInspector() | ||
const fullname = `${filenamePrefix}-${Date.now()}-${Math.floor(Math.random() * 10000)}.cpuprofile` | ||
const {profile} = await session.post('Profiler.stop') | ||
writeFileSync(fullname, JSON.stringify(profile)) | ||
} | ||
} |