import Immutable from "immutable" ; import is from "is" ; const timers = { } ; const startDate = Date . now ( ) ; parqm param { Promise } p return { Promise } function measure ( type , p ) { timers [ type ] = timers [ type ] || { type : type , count : 0 , total : 0 , min : undefined , max : 0 , } ; const start = Date . now ( ) ; return p . fin ( ( ) => { const end = Date . now ( ) ; const duration = end - start ; timers [ type ] . count ++ ; timers [ type ] . total += duration ; if ( is . undefined ( timers [ type ] . min ) ) { timers [ type ] . min = duration ; } else { timers [ type ] . min = Math . min ( timers [ type ] . min , duration ) ; } timers [ type ] . max = Math . max ( timers [ type ] . max , duration ) ; } ) ; } param { number } ms return { string } function time ( ms ) { if ( ms < 1000 ) { return `${ ms . toFixed ( 0 ) }ms` ; } return `${ ( ms / 1000 ) . toFixed ( 2 ) }s` ; } param { Logger } logger function dump ( logger ) { const prefix = "    > " ; let measured = 0 ; const totalDuration = Date . now ( ) - startDate ; const logLevel = logger . getLevel ( ) ; logger . setLevel ( "debug" ) ; Immutable . Map ( timers ) . valueSeq ( ) . sortBy ( ( timer ) => { measured += timer . total ; return timer . total ; } ) . forEach ( ( timer ) => { const percent = ( timer . total * 100 ) / totalDuration ; logger . debug . ln ( `${ percent . toFixed ( 1 ) }% of time spent in "${ timer . type }" (${ timer . count } times) :` ) ; logger . debug . ln ( `${ prefix }Total: ${ time ( timer . total ) } | Average: ${ time ( timer . total / timer . count ) }` ) ; logger . debug . ln ( `${ prefix }Min: ${ time ( timer . min ) } | Max: ${ time ( timer . max ) }` ) ; logger . debug . ln ( "---------------------------" ) ; } ) ; logger . debug . ln ( `${ time ( totalDuration - measured ) } spent in non-mesured sections` ) ; logger . setLevel ( logLevel ) ; } export default { measure : measure , dump : dump , } ;