@@ -501,11 +501,17 @@ export function networkRecorder(session, directory) {
501501 } ;
502502}
503503
504- export async function profileWeb ( { directory, profileArgs, args, network } ) {
504+ export async function profileWeb ( {
505+ directory,
506+ profileArgs,
507+ args,
508+ network,
509+ trace = false ,
510+ } ) {
505511 const vite = normalizeWebViteArgs ( args ) ;
506512 await recordManifest ( directory , "web" , profileArgs , {
507513 coverage : [
508- "chromium-renderer" ,
514+ trace ? "chromium-trace" : "chromium-renderer" ,
509515 "vite-broker" ,
510516 ...( network ? [ "chromium-network" ] : [ ] ) ,
511517 ] ,
@@ -551,6 +557,7 @@ export async function profileWeb({ directory, profileArgs, args, network }) {
551557 let networkCapture ;
552558 let nodeProfiler ;
553559 let rendererStarted = false ;
560+ let tracing = false ;
554561 let brokerStarted = false ;
555562 let captureStarted = false ;
556563 let outcome ;
@@ -592,12 +599,19 @@ export async function profileWeb({ directory, profileArgs, args, network }) {
592599 networkCapture = networkRecorder ( session , directory ) ;
593600 await networkCapture . start ( page , duringStartup ) ;
594601 }
595- await duringStartup ( ( ) => session . send ( "Profiler.enable" ) ) ;
596- await duringStartup ( ( ) =>
597- session . send ( "Profiler.setSamplingInterval" , { interval : 1000 } ) ,
598- ) ;
599- await duringStartup ( ( ) => session . send ( "Profiler.start" ) ) ;
600- rendererStarted = true ;
602+ if ( trace ) {
603+ // The DevTools trace carries denser renderer CPU samples; a second
604+ // sampler would only add overhead to the recorded timings.
605+ await duringStartup ( ( ) => browser . startTracing ( page ) ) ;
606+ tracing = true ;
607+ } else {
608+ await duringStartup ( ( ) => session . send ( "Profiler.enable" ) ) ;
609+ await duringStartup ( ( ) =>
610+ session . send ( "Profiler.setSamplingInterval" , { interval : 1000 } ) ,
611+ ) ;
612+ await duringStartup ( ( ) => session . send ( "Profiler.start" ) ) ;
613+ rendererStarted = true ;
614+ }
601615 await duringStartup ( ( ) => page . goto ( url ) ) ;
602616 control . abort . signal . throwIfAborted ( ) ;
603617 captureStarted = true ;
@@ -622,6 +636,16 @@ export async function profileWeb({ directory, profileArgs, args, network }) {
622636 failures . push ( error ) ;
623637 }
624638 }
639+ if ( tracing ) {
640+ try {
641+ await writeFile (
642+ `${ directory } /chromium-trace.json` ,
643+ await browser . stopTracing ( ) ,
644+ ) ;
645+ } catch ( error ) {
646+ failures . push ( error ) ;
647+ }
648+ }
625649 if ( brokerStarted ) {
626650 try {
627651 ( { profile : brokerProfile } = await nodeProfiler . send ( "Profiler.stop" ) ) ;
@@ -750,7 +774,10 @@ async function main() {
750774 const target = process . argv [ 2 ] ;
751775 const profileArgs = process . argv . slice ( 3 ) ;
752776 const network = target === "web" && profileArgs . includes ( "--network" ) ;
753- const args = profileArgs . filter ( ( argument ) => argument !== "--network" ) ;
777+ const trace = target === "web" && profileArgs . includes ( "--trace" ) ;
778+ const args = profileArgs . filter (
779+ ( argument ) => argument !== "--network" && argument !== "--trace" ,
780+ ) ;
754781 if ( target !== "web" && target !== "desktop" ) {
755782 console . error (
756783 "Usage: node scripts/profile-dev.mjs <web|desktop> [arguments]" ,
@@ -770,7 +797,7 @@ async function main() {
770797 const directory = `${ root } .profiles/${ stamp } -${ target } ` ;
771798 await mkdir ( directory , { recursive : true } ) ;
772799 if ( target === "web" )
773- await profileWeb ( { directory, profileArgs, args, network } ) ;
800+ await profileWeb ( { directory, profileArgs, args, network, trace } ) ;
774801 else await profileDesktop ( { directory, profileArgs, args } ) ;
775802}
776803
0 commit comments