TimingHTTP request timing metrics
Per Recommendation #12: Detailed timing breakdown for requests, similar to curl's --write-out timing variables.
|--DNS--|--Connect--|--TLS--|--Request--|--Wait--|--Content--|
^ ^ ^ ^ ^ ^
namelookup connect ssl_handsh send ttfb total (* Start timing *)
let timer = Timing.start () in
(* DNS resolution *)
let addrs = Eio.Net.getaddrinfo_stream net host in
Timing.mark_dns timer;
(* TCP connect *)
let flow = Eio.Net.connect ~sw net addr in
Timing.mark_connect timer;
(* TLS handshake (for HTTPS) *)
let tls_flow = Tls_eio.client_of_flow tls_cfg flow in
Timing.mark_tls timer;
(* Send request *)
send_request tls_flow request;
Timing.mark_send timer;
(* First byte received *)
Timing.mark_ttfb timer;
(* Complete transfer *)
Timing.mark_transfer_end timer;
(* Get timing metrics *)
let metrics = Timing.finish timer in
Printf.printf "Total: %.3fms\n" (Timing.total metrics *. 1000.0)val empty : tEmpty timing with all phases unknown
val make :
?dns_lookup:float ->
?tcp_connect:float ->
?tls_handshake:float ->
?request_sent:float ->
?time_to_first_byte:float ->
?content_transfer:float ->
total:float ->
unit ->
tCreate timing metrics manually
val dns_lookup : t -> float optionTime for DNS resolution in seconds
val tcp_connect : t -> float optionTime to establish TCP connection in seconds
val tls_handshake : t -> float optionTime for TLS handshake in seconds (HTTPS only)
val request_sent : t -> float optionTime to send request in seconds
val time_to_first_byte : t -> float optionTime from request sent to first response byte in seconds
val content_transfer : t -> float optionTime to transfer response body in seconds
val total : t -> floatTotal request time in seconds
val connection_time : t -> float optionTotal connection setup time (DNS + TCP + TLS)
val server_time : t -> float optionServer processing time (TTFB - request send time)
val pp : Format.formatter -> t -> unitPretty-print timing in human readable format
val to_string : t -> stringConvert to string representation
val to_assoc : t -> (string * float) listConvert to association list for logging/serialization
Use this during request execution to collect timing data incrementally.
val start : unit -> timerStart a new timer
val mark_dns : timer -> unitMark DNS resolution complete
val mark_connect : timer -> unitMark TCP connection established
val mark_tls : timer -> unitMark TLS handshake complete
val mark_send : timer -> unitMark request fully sent
val mark_ttfb : timer -> unitMark first response byte received
val mark_transfer_end : timer -> unitMark response body transfer complete
val src : Logs.Src.tLog source for timing operations
val log_timing : ?level:Logs.level -> t -> unitLog timing metrics at specified level (default: Debug)