mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 11:15:35 -05:00
Merge pull request #32 from craff/wip-middleware
add start time to query, improve stats middleware
This commit is contained in:
commit
b7fd8da9a0
3 changed files with 25 additions and 6 deletions
|
|
@ -7,18 +7,29 @@ let now_ = Unix.gettimeofday
|
|||
let middleware_stat () : S.Middleware.t * (unit -> string) =
|
||||
let n_req = 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 =
|
||||
incr n_req;
|
||||
let t1 = now_ () in
|
||||
h req ~resp:(fun response ->
|
||||
resp response;
|
||||
let t1 = S.Request.start_time req in
|
||||
let t2 = now_ () in
|
||||
total_time_ := !total_time_ +. (t2 -. t1);
|
||||
h req ~resp:(fun response ->
|
||||
let t3 = now_ () in
|
||||
resp response;
|
||||
let t4 = now_ () in
|
||||
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 () =
|
||||
Printf.sprintf "%d requests (average response time: %.3fs)"
|
||||
!n_req (!total_time_ /. float !n_req)
|
||||
Printf.sprintf "%d requests (average response time: %.3fms = %.3fms + %.3fms + %.3fms)"
|
||||
!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
|
||||
m, get_stat
|
||||
|
||||
|
|
|
|||
|
|
@ -367,6 +367,7 @@ module Request = struct
|
|||
path_components: string list;
|
||||
query: (string*string) list;
|
||||
body: 'body;
|
||||
start_time: float;
|
||||
}
|
||||
|
||||
let headers self = self.headers
|
||||
|
|
@ -374,6 +375,7 @@ module Request = struct
|
|||
let meth self = self.meth
|
||||
let path self = self.path
|
||||
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
|
||||
|
||||
|
|
@ -485,6 +487,7 @@ module Request = struct
|
|||
let parse_req_start ~buf (bs:byte_stream) : unit t option resp_result =
|
||||
try
|
||||
let line = Byte_stream.read_line ~buf bs in
|
||||
let start_time = Unix.gettimeofday () in
|
||||
let meth, path =
|
||||
try
|
||||
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
|
||||
in
|
||||
Ok (Some {meth; query; host; path; path_components;
|
||||
headers; body=()})
|
||||
headers; body=(); start_time; })
|
||||
with
|
||||
| End_of_file | Sys_error _ -> Ok None
|
||||
| Bad_req (c,s) -> Error (c,s)
|
||||
|
|
|
|||
|
|
@ -227,6 +227,7 @@ module Request : sig
|
|||
path_components: string list;
|
||||
query: (string*string) list;
|
||||
body: 'body;
|
||||
start_time: float; (** @since NEXT_RELEASE *)
|
||||
}
|
||||
(** 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
|
||||
(** 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
|
||||
(** Limit the body size to [max_size] bytes, or return
|
||||
a [413] error.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue