11import { generateAll } from './defaultAction' ;
22import { ScullyConfig } from './interfacesandenums' ;
3- import { performance , PerformanceObserver } from 'perf_hooks' ;
3+ import { performance , PerformanceObserver , PerformanceObserverCallback } from 'perf_hooks' ;
44import { log , yellow } from './log' ;
5+ import { performanceIds } from './performanceIds' ;
56
67/**
78 * Starts the entire process
@@ -10,20 +11,23 @@ import {log, yellow} from './log';
1011export const startScully = ( config ?: Partial < ScullyConfig > ) => {
1112 let routeCount = 0 ;
1213 return new Promise ( resolve => {
13- performance . mark ( 'start' ) ;
14- const obs = new PerformanceObserver ( ( list , observer ) => {
15- const duration = list . getEntries ( ) [ 0 ] . duration ;
16- performance . clearMarks ( ) ;
17- observer . disconnect ( ) ;
18- resolve ( { routeCount, duration} ) ;
19- } ) ;
14+ performance . mark ( 'startDuration' ) ;
15+ performanceIds . add ( 'Duration' ) ;
16+ let innerResolve ;
17+ const durationProm = new Promise ( r => ( innerResolve = r ) ) ;
18+ const obs = new PerformanceObserver ( measurePerformance ( innerResolve ) ) ;
2019 obs . observe ( { entryTypes : [ 'measure' ] , buffered : true } ) ;
21- generateAll ( config ) . then ( routes => {
22- routeCount = routes . length ;
23- performance . mark ( 'stop' ) ;
24- performance . measure ( 'duration' , 'start' , 'stop' ) ;
20+ const numberOfRoutesProm = generateAll ( config ) . then ( routes => {
21+ performance . mark ( 'stopDuration' ) ;
22+ /** measure all performance checks */
23+ [ ...performanceIds . values ( ) ] . forEach ( id => performance . measure ( id , `start${ id } ` , `stop${ id } ` ) ) ;
24+ return routes . length ;
2525 } ) ;
26- } ) . then ( ( { routeCount : numberOfRoutes , duration} ) => {
26+ Promise . all ( [ numberOfRoutesProm , durationProm ] ) . then ( ( [ numberOfRoutes , durations ] ) =>
27+ resolve ( { numberOfRoutes, durations} )
28+ ) ;
29+ } ) . then ( ( { numberOfRoutes, durations} : { numberOfRoutes : number ; durations : { [ key : string ] : number } } ) => {
30+ const duration = durations . Duration ;
2731 // tslint:disable-next-line:variable-name
2832 const seconds = duration / 1000 ;
2933 const routesProSecond = Math . ceil ( ( numberOfRoutes / seconds ) * 100 ) / 100 ;
@@ -32,6 +36,30 @@ export const startScully = (config?: Partial<ScullyConfig>) => {
3236Generating took ${ yellow ( Math . floor ( seconds * 100 ) / 100 ) } seconds for ${ yellow ( numberOfRoutes ) } pages:
3337 That is ${ yellow ( routesProSecond ) } pages per second,
3438 or ${ yellow ( Math . ceil ( singleTime ) ) } milliseconds for each page.
39+
40+ Finding routes in the angular app took ${ logSeconds ( durations . Traverse ) }
41+ Pulling in route-data took ${ logSeconds ( durations . Discovery ) }
42+ Rendering the pages took ${ logSeconds ( durations . Render ) }
43+
3544` ) ;
3645 } ) ;
3746} ;
47+
48+ function measurePerformance ( resolve : ( value ?: unknown ) => void ) : PerformanceObserverCallback {
49+ return ( list , observer ) => {
50+ const durations = list
51+ . getEntries ( )
52+ . reduce ( ( acc , entry ) => ( { ...acc , [ entry . name ] : Math . floor ( entry . duration * 100 ) / 100 } ) , { } ) ;
53+ // console.log(durations);
54+ performance . clearMarks ( ) ;
55+ observer . disconnect ( ) ;
56+ resolve ( durations ) ;
57+ } ;
58+ }
59+
60+ function logSeconds ( milliSeconds ) {
61+ if ( milliSeconds < 1000 ) {
62+ return yellow ( Math . floor ( milliSeconds ) ) + ' milliseconds' ;
63+ }
64+ return yellow ( Math . floor ( milliSeconds / 10 ) / 100 ) + ' seconds' ;
65+ }
0 commit comments