-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(builder): support RSPACK_PROFILE (#4772)
- Loading branch information
Showing
6 changed files
with
143 additions
and
0 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,7 @@ | ||
--- | ||
'@modern-js/builder-rspack-provider': patch | ||
--- | ||
|
||
chore(builder): support the use of the RSPACK_PROFILE environment variable for Rspack build performance profile | ||
|
||
chore(builder):支持使用 RSPACK_PROFILE 环境变量来进行 Rspack 构建性能分析 |
103 changes: 103 additions & 0 deletions
103
packages/builder/builder-rspack-provider/src/plugins/rspack-profile.ts
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,103 @@ | ||
import type { BuilderPlugin } from '../types'; | ||
import path from 'path'; | ||
import { | ||
experimental_registerGlobalTrace as registerGlobalTrace, | ||
experimental_cleanupGlobalTrace as cleanupGlobalTrace, | ||
} from '@rspack/core'; | ||
// eslint-disable-next-line node/no-unsupported-features/node-builtins | ||
import inspector from 'inspector'; | ||
// eslint-disable-next-line @typescript-eslint/no-restricted-imports | ||
import { fs } from '@modern-js/utils'; | ||
import { logger } from '@modern-js/builder-shared'; | ||
|
||
export const stopProfiler = ( | ||
output: string, | ||
profileSession?: inspector.Session, | ||
) => { | ||
if (!profileSession) { | ||
return; | ||
} | ||
profileSession.post('Profiler.stop', (error, param) => { | ||
if (error) { | ||
logger.error('Failed to generate JS CPU profile:', error); | ||
return; | ||
} | ||
fs.writeFileSync(output, JSON.stringify(param.profile)); | ||
}); | ||
}; | ||
|
||
// Reference rspack-cli | ||
// https://github.com/modern-js-dev/rspack/blob/509abcfc523bc20125459f5d428dc1645751700c/packages/rspack-cli/src/utils/profile.ts | ||
export const builderPluginRspackProfile = (): BuilderPlugin => ({ | ||
name: 'builder-plugin-rspack-profile', | ||
|
||
setup(api) { | ||
/** | ||
* RSPACK_PROFILE=ALL | ||
* RSPACK_PROFILE=TRACE|CPU|LOGGING | ||
*/ | ||
const RSPACK_PROFILE = process.env.RSPACK_PROFILE?.toUpperCase(); | ||
|
||
if (!RSPACK_PROFILE) { | ||
return; | ||
} | ||
|
||
const timestamp = Date.now(); | ||
const profileDir = path.join( | ||
api.context.distPath, | ||
`rspack-profile-${timestamp}`, | ||
); | ||
|
||
let profileSession: inspector.Session | undefined; | ||
|
||
const enableProfileTrace = | ||
RSPACK_PROFILE === 'ALL' || RSPACK_PROFILE.includes('TRACE'); | ||
|
||
const enableCPUProfile = | ||
RSPACK_PROFILE === 'ALL' || RSPACK_PROFILE.includes('CPU'); | ||
|
||
const enableLogging = | ||
RSPACK_PROFILE === 'ALL' || RSPACK_PROFILE.includes('LOGGING'); | ||
|
||
const traceFilePath = path.join(profileDir, 'trace.json'); | ||
const cpuProfilePath = path.join(profileDir, 'jscpuprofile.json'); | ||
const loggingFilePath = path.join(profileDir, 'logging.json'); | ||
|
||
const onStart = () => { | ||
fs.ensureDirSync(profileDir); | ||
|
||
if (enableProfileTrace) { | ||
registerGlobalTrace('trace', 'chrome', traceFilePath); | ||
} | ||
|
||
if (enableCPUProfile) { | ||
profileSession = new inspector.Session(); | ||
profileSession.connect(); | ||
profileSession.post('Profiler.enable'); | ||
profileSession.post('Profiler.start'); | ||
} | ||
}; | ||
|
||
api.onBeforeBuild(onStart); | ||
api.onBeforeStartDevServer(onStart); | ||
|
||
api.onAfterBuild(async ({ stats }) => { | ||
if (enableLogging && stats) { | ||
const logging = stats.toJson({ | ||
all: false, | ||
logging: 'verbose', | ||
loggingTrace: true, | ||
}); | ||
fs.writeFileSync(loggingFilePath, JSON.stringify(logging)); | ||
} | ||
}); | ||
|
||
api.onExit(() => { | ||
enableProfileTrace && cleanupGlobalTrace(); | ||
|
||
stopProfiler(cpuProfilePath, profileSession); | ||
|
||
logger.info(`Saved Rspack profile file to ${profileDir}`); | ||
}); | ||
}, | ||
}); |
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