From 4770e3e7297f70a2d29ac115701bfabc6d7af7d0 Mon Sep 17 00:00:00 2001 From: craff Date: Wed, 15 Dec 2021 18:14:17 -1000 Subject: [PATCH 1/6] Timing start of request --- examples/echo.ml | 6 +++--- src/Tiny_httpd.ml | 5 ++++- src/Tiny_httpd.mli | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/echo.ml b/examples/echo.ml index 94939263..f24b4911 100644 --- a/examples/echo.ml +++ b/examples/echo.ml @@ -10,15 +10,15 @@ let middleware_stat () : S.Middleware.t * (unit -> string) = let m h req ~resp = incr n_req; - let t1 = now_ () in + let t1 = S.Request.time req in h req ~resp:(fun response -> resp response; let t2 = now_ () in total_time_ := !total_time_ +. (t2 -. t1); ) 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)" + !n_req (!total_time_ /. float !n_req *. 1e3) in m, get_stat diff --git a/src/Tiny_httpd.ml b/src/Tiny_httpd.ml index f86d4d65..1c7711fe 100644 --- a/src/Tiny_httpd.ml +++ b/src/Tiny_httpd.ml @@ -367,6 +367,7 @@ module Request = struct path_components: string list; query: (string*string) list; body: 'body; + 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 time self = self.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 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=(); time}) with | End_of_file | Sys_error _ -> Ok None | Bad_req (c,s) -> Error (c,s) diff --git a/src/Tiny_httpd.mli b/src/Tiny_httpd.mli index 2a56f434..dad0a702 100644 --- a/src/Tiny_httpd.mli +++ b/src/Tiny_httpd.mli @@ -227,6 +227,7 @@ module Request : sig path_components: string list; query: (string*string) list; body: 'body; + time: float; } (** A request with method, path, host, headers, and a body, sent by a client. @@ -280,6 +281,9 @@ module Request : sig val body : 'b t -> 'b (** Request body, possibly empty. *) + val time : 'b t -> float + (** time (from [Unix.gettimeofday] after parsing the first line*) + 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. From 672e33c8272d4fc224082d535935c98236cbf5ca Mon Sep 17 00:00:00 2001 From: craff Date: Wed, 15 Dec 2021 18:18:52 -1000 Subject: [PATCH 2/6] detailed time in echo example --- examples/echo.ml | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/examples/echo.ml b/examples/echo.ml index f24b4911..348a1286 100644 --- a/examples/echo.ml +++ b/examples/echo.ml @@ -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 = S.Request.time req in + let t2 = now_ () in h req ~resp:(fun response -> + let t3 = now_ () in resp response; - let t2 = now_ () in - total_time_ := !total_time_ +. (t2 -. t1); + 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: %.3fms)" + 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 From 7b14cbd6a7bd3e45c8e551b7901daa4efdb3396d Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 16 Dec 2021 00:26:07 -0500 Subject: [PATCH 3/6] rename `time` into `start_time` --- src/Tiny_httpd.ml | 8 ++++---- src/Tiny_httpd.mli | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Tiny_httpd.ml b/src/Tiny_httpd.ml index 1c7711fe..3ec46dea 100644 --- a/src/Tiny_httpd.ml +++ b/src/Tiny_httpd.ml @@ -367,7 +367,7 @@ module Request = struct path_components: string list; query: (string*string) list; body: 'body; - time: float; + start_time: float; } let headers self = self.headers @@ -375,7 +375,7 @@ module Request = struct let meth self = self.meth let path self = self.path let body self = self.body - let time self = self.time + let start_time self = self.start_time let non_query_path self = Tiny_httpd_util.get_non_query_path self.path @@ -487,7 +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 time = Unix.gettimeofday () 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 @@ -513,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=(); time}) + headers; body=(); start_time; }) with | End_of_file | Sys_error _ -> Ok None | Bad_req (c,s) -> Error (c,s) diff --git a/src/Tiny_httpd.mli b/src/Tiny_httpd.mli index dad0a702..58c67314 100644 --- a/src/Tiny_httpd.mli +++ b/src/Tiny_httpd.mli @@ -281,8 +281,9 @@ module Request : sig val body : 'b t -> 'b (** Request body, possibly empty. *) - val time : 'b t -> float - (** time (from [Unix.gettimeofday] after parsing the first line*) + 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 From accdb1e0ac3ea3372b4946172b5706854a94c6ff Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 16 Dec 2021 00:26:18 -0500 Subject: [PATCH 4/6] Update src/Tiny_httpd.mli --- src/Tiny_httpd.mli | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tiny_httpd.mli b/src/Tiny_httpd.mli index 58c67314..0d59275c 100644 --- a/src/Tiny_httpd.mli +++ b/src/Tiny_httpd.mli @@ -227,7 +227,7 @@ module Request : sig path_components: string list; query: (string*string) list; body: 'body; - time: float; + start_time: float; (** @since NEXT_RELEASE *) } (** A request with method, path, host, headers, and a body, sent by a client. From ffc18a56175a91a272306fe6b715f0859330b096 Mon Sep 17 00:00:00 2001 From: craff Date: Wed, 15 Dec 2021 20:13:32 -1000 Subject: [PATCH 5/6] fix in echo --- examples/echo.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/echo.ml b/examples/echo.ml index 348a1286..a28416e3 100644 --- a/examples/echo.ml +++ b/examples/echo.ml @@ -13,7 +13,7 @@ let middleware_stat () : S.Middleware.t * (unit -> string) = let m h req ~resp = incr n_req; - let t1 = S.Request.time req in + let t1 = S.Request.time_start req in let t2 = now_ () in h req ~resp:(fun response -> let t3 = now_ () in From 51be9c0c8f31952e762548fc51c05e1579299658 Mon Sep 17 00:00:00 2001 From: craff Date: Wed, 15 Dec 2021 20:19:42 -1000 Subject: [PATCH 6/6] real fix in echo! --- examples/echo.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/echo.ml b/examples/echo.ml index a28416e3..f77bbe72 100644 --- a/examples/echo.ml +++ b/examples/echo.ml @@ -13,7 +13,7 @@ let middleware_stat () : S.Middleware.t * (unit -> string) = let m h req ~resp = incr n_req; - let t1 = S.Request.time_start req in + let t1 = S.Request.start_time req in let t2 = now_ () in h req ~resp:(fun response -> let t3 = now_ () in