Merge pull request #32 from craff/wip-middleware

add start time to query, improve stats middleware
This commit is contained in:
Simon Cruanes 2021-12-16 09:12:10 -05:00 committed by GitHub
commit b7fd8da9a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 6 deletions

View file

@ -7,18 +7,29 @@ let now_ = Unix.gettimeofday
let middleware_stat () : S.Middleware.t * (unit -> string) = let middleware_stat () : S.Middleware.t * (unit -> string) =
let n_req = ref 0 in let n_req = ref 0 in
let total_time_ = ref 0. in let total_time_ = ref 0. in
let parse_time_ = ref 0. in
let build_time_ = ref 0. in
let write_time_ = ref 0. in
let m h req ~resp = let m h req ~resp =
incr n_req; incr n_req;
let t1 = now_ () in let t1 = S.Request.start_time req in
let t2 = now_ () in
h req ~resp:(fun response -> h req ~resp:(fun response ->
let t3 = now_ () in
resp response; resp response;
let t2 = now_ () in let t4 = now_ () in
total_time_ := !total_time_ +. (t2 -. t1); total_time_ := !total_time_ +. (t4 -. t1);
parse_time_ := !parse_time_ +. (t2 -. t1);
build_time_ := !build_time_ +. (t3 -. t2);
write_time_ := !write_time_ +. (t4 -. t3);
) )
and get_stat () = and get_stat () =
Printf.sprintf "%d requests (average response time: %.3fs)" Printf.sprintf "%d requests (average response time: %.3fms = %.3fms + %.3fms + %.3fms)"
!n_req (!total_time_ /. float !n_req) !n_req (!total_time_ /. float !n_req *. 1e3)
(!parse_time_ /. float !n_req *. 1e3)
(!build_time_ /. float !n_req *. 1e3)
(!write_time_ /. float !n_req *. 1e3)
in in
m, get_stat m, get_stat

View file

@ -367,6 +367,7 @@ module Request = struct
path_components: string list; path_components: string list;
query: (string*string) list; query: (string*string) list;
body: 'body; body: 'body;
start_time: float;
} }
let headers self = self.headers let headers self = self.headers
@ -374,6 +375,7 @@ module Request = struct
let meth self = self.meth let meth self = self.meth
let path self = self.path let path self = self.path
let body self = self.body let body self = self.body
let start_time self = self.start_time
let non_query_path self = Tiny_httpd_util.get_non_query_path self.path let non_query_path self = Tiny_httpd_util.get_non_query_path self.path
@ -485,6 +487,7 @@ module Request = struct
let parse_req_start ~buf (bs:byte_stream) : unit t option resp_result = let parse_req_start ~buf (bs:byte_stream) : unit t option resp_result =
try try
let line = Byte_stream.read_line ~buf bs in let line = Byte_stream.read_line ~buf bs in
let start_time = Unix.gettimeofday () in
let meth, path = let meth, path =
try try
let m, p, v = Scanf.sscanf line "%s %s HTTP/1.%d\r" (fun x y z->x,y,z) in let m, p, v = Scanf.sscanf line "%s %s HTTP/1.%d\r" (fun x y z->x,y,z) in
@ -510,7 +513,7 @@ module Request = struct
| Error e -> bad_reqf 400 "invalid query: %s" e | Error e -> bad_reqf 400 "invalid query: %s" e
in in
Ok (Some {meth; query; host; path; path_components; Ok (Some {meth; query; host; path; path_components;
headers; body=()}) headers; body=(); start_time; })
with with
| End_of_file | Sys_error _ -> Ok None | End_of_file | Sys_error _ -> Ok None
| Bad_req (c,s) -> Error (c,s) | Bad_req (c,s) -> Error (c,s)

View file

@ -227,6 +227,7 @@ module Request : sig
path_components: string list; path_components: string list;
query: (string*string) list; query: (string*string) list;
body: 'body; body: 'body;
start_time: float; (** @since NEXT_RELEASE *)
} }
(** A request with method, path, host, headers, and a body, sent by a client. (** A request with method, path, host, headers, and a body, sent by a client.
@ -280,6 +281,10 @@ module Request : sig
val body : 'b t -> 'b val body : 'b t -> 'b
(** Request body, possibly empty. *) (** Request body, possibly empty. *)
val start_time : _ t -> float
(** time stamp (from {!Unix.gettimeofday}) after parsing the first line of the request
@since NEXT_RELEASE *)
val limit_body_size : max_size:int -> byte_stream t -> byte_stream t val limit_body_size : max_size:int -> byte_stream t -> byte_stream t
(** Limit the body size to [max_size] bytes, or return (** Limit the body size to [max_size] bytes, or return
a [413] error. a [413] error.