@@ -21,7 +21,16 @@ import {
2121 waitForOpenOutputGate ,
2222} from "@miniflare/shared" ;
2323import type { WebSocket } from "@miniflare/web-sockets" ;
24- import { Colorize , blue , bold , green , grey , red , yellow } from "kleur/colors" ;
24+ import {
25+ Colorize ,
26+ blue ,
27+ bold ,
28+ dim ,
29+ green ,
30+ grey ,
31+ red ,
32+ yellow ,
33+ } from "kleur/colors" ;
2534import { splitCookiesString } from "set-cookie-parser" ;
2635import {
2736 Request as BaseRequest ,
@@ -913,6 +922,10 @@ function millisFromHRTime([seconds, nanoseconds]: HRTime): string {
913922 return `${ ( ( seconds * 1e9 + nanoseconds ) / 1e6 ) . toFixed ( 2 ) } ms` ;
914923}
915924
925+ function millisFromCPUTime ( microseconds : number ) : string {
926+ return `${ ( microseconds / 1e3 ) . toFixed ( 2 ) } ms` ;
927+ }
928+
916929function colourFromHTTPStatus ( status : number ) : Colorize {
917930 if ( 200 <= status && status < 300 ) return green ;
918931 if ( 400 <= status && status < 500 ) return yellow ;
@@ -924,19 +937,27 @@ export async function logResponse(
924937 log : Log ,
925938 {
926939 start,
940+ startCpu,
927941 method,
928942 url,
929943 status,
930944 waitUntil,
931945 } : {
932946 start : HRTime ;
947+ startCpu ?: NodeJS . CpuUsage ;
933948 method : string ;
934949 url : string ;
935950 status ?: number ;
936951 waitUntil ?: Promise < any [ ] > ;
937952 }
938953) : Promise < void > {
954+ const cpuParts : string [ ] = [ ] ;
955+
939956 const responseTime = millisFromHRTime ( process . hrtime ( start ) ) ;
957+ if ( startCpu !== undefined ) {
958+ const responseTimeCpu = millisFromCPUTime ( process . cpuUsage ( startCpu ) . user ) ;
959+ cpuParts . push ( dim ( grey ( ` (CPU: ~${ responseTimeCpu } ` ) ) ) ;
960+ }
940961
941962 // Wait for all waitUntil promises to resolve
942963 let waitUntilResponse : any [ ] | undefined ;
@@ -948,6 +969,15 @@ export async function logResponse(
948969 log . error ( e ) ;
949970 }
950971 const waitUntilTime = millisFromHRTime ( process . hrtime ( start ) ) ;
972+ if ( startCpu !== undefined ) {
973+ if ( waitUntilResponse ?. length ) {
974+ const waitUntilTimeCpu = millisFromCPUTime (
975+ process . cpuUsage ( startCpu ) . user
976+ ) ;
977+ cpuParts . push ( dim ( grey ( `, waitUntil: ~${ waitUntilTimeCpu } ` ) ) ) ;
978+ }
979+ cpuParts . push ( dim ( grey ( ")" ) ) ) ;
980+ }
951981
952982 log . log (
953983 [
@@ -961,6 +991,7 @@ export async function logResponse(
961991 // Only include waitUntilTime if there were waitUntil promises
962992 waitUntilResponse ?. length ? grey ( `, waitUntil: ${ waitUntilTime } ` ) : "" ,
963993 grey ( ")" ) ,
994+ ...cpuParts ,
964995 ] . join ( "" )
965996 ) ;
966997}
0 commit comments