Skip to content

Commit a195bfa

Browse files
committed
Web: record curl times
1 parent bdb1c35 commit a195bfa

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

web.ml

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ let url_get_args url = try String.split url "?" |> snd |> parse_url_args with _
4141

4242
let () = Curl.global_init Curl.CURLINIT_GLOBALALL
4343

44+
let curl_times h =
45+
Curl.[
46+
"Curl.dns", get_namelookuptime h;
47+
"Curl.conn", get_connecttime h;
48+
(* "Curl.app", get_appconnecttime; *)
49+
"Curl.pre", get_pretransfertime h;
50+
"Curl.start", get_starttransfertime h;
51+
"Curl.total", get_totaltime h;
52+
]
53+
4454
module CurlCache = Cache.Reuse(struct type t = Curl.t let create = Curl.init let reset = Curl.reset end)
4555

4656
let curl_default_setup h =
@@ -114,6 +124,7 @@ module type HTTP = sig
114124
val with_curl_cache : (Curl.t -> 'a IO.t) -> 'a IO.t
115125
val http_gets :
116126
?setup:(Curl.t -> unit) ->
127+
?timer:Action.timer ->
117128
?max_size:int ->
118129
?check:(Curl.t -> bool) ->
119130
?result:(Curl.t -> Curl.curlCode -> unit IO.t) ->
@@ -124,6 +135,7 @@ module type HTTP = sig
124135
?timeout:int ->
125136
?verbose:bool ->
126137
?setup:(Curl.t -> unit) ->
138+
?timer:Action.timer ->
127139
?max_size:int ->
128140
?http_1_0:bool ->
129141
?headers:string list ->
@@ -141,6 +153,7 @@ module type HTTP = sig
141153
?timeout:int ->
142154
?verbose:bool ->
143155
?setup:(Curl.t -> unit) ->
156+
?timer:Action.timer ->
144157
?http_1_0:bool ->
145158
?headers:string list ->
146159
?action:http_action ->
@@ -167,6 +180,7 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
167180
?timeout:int ->
168181
?verbose:bool ->
169182
?setup:(Curl.t -> unit) ->
183+
?timer:Action.timer ->
170184
?max_size:int ->
171185
?http_1_0:bool ->
172186
?headers:string list ->
@@ -182,8 +196,18 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
182196
let with_curl f = bracket (return @@ Curl.init ()) (fun h -> Curl.cleanup h; return_unit) f
183197
let with_curl_cache f = bracket (return @@ CurlCache.get ()) (fun h -> CurlCache.release h; return_unit) f
184198

199+
let update_timer h timer =
200+
match timer with
201+
| None -> ()
202+
| Some t ->
203+
let total = Curl.get_totaltime h in
204+
let now = Time.now () in
205+
t#record "Curl.start" (now -. total);
206+
curl_times h |> List.iter (fun (name,time) -> t#record name (now -. total +. time));
207+
()
208+
185209
(* deprecated *)
186-
let http_gets ?(setup=ignore) ?max_size ?(check=(fun _ -> true)) ?(result=(fun _ _ -> return_unit)) url =
210+
let http_gets ?(setup=ignore) ?timer ?max_size ?(check=(fun _ -> true)) ?(result=(fun _ _ -> return_unit)) url =
187211
with_curl_cache begin fun h ->
188212
Curl.set_url h url;
189213
curl_default_setup h;
@@ -201,8 +225,9 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
201225
| Some max_size when !read_size > max_size -> Exn.fail "received too much data (%db) when max is %db" !read_size max_size
202226
| _ -> l
203227
end;
204-
Curl_IO.perform h >>= fun code ->
205-
result h code >>= fun () ->
228+
timer |> Option.may (fun t -> t#mark "Web.http");
229+
catch (fun () -> Curl_IO.perform h) (fun exn -> update_timer h timer; IO.raise exn) >>= fun code ->
230+
(update_timer h timer; result h code) >>= fun () ->
206231
return @@ match code with
207232
| Curl.CURLE_OK -> `Ok (Curl.get_httpcode h, Buffer.contents b)
208233
| code -> `Error code
@@ -236,7 +261,7 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
236261

237262
(* NOTE don't forget to set http_1_0=true when sending requests to a Httpev-based server *)
238263
(* Don't use curl_setheaders when using ?headers option *)
239-
let http_request' ?ua ?timeout ?(verbose=false) ?(setup=ignore) ?max_size ?(http_1_0=false) ?headers ?body (action:http_action) url =
264+
let http_request' ?ua ?timeout ?(verbose=false) ?(setup=ignore) ?timer ?max_size ?(http_1_0=false) ?headers ?body (action:http_action) url =
240265
let open Curl in
241266
let set_body_and_headers h ct body =
242267
set_httpheader h (("Content-Type: "^ct) :: Option.default [] headers);
@@ -282,22 +307,22 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
282307
end;
283308
let t = new Action.timer in
284309
let result = if verbose then Some (verbose_curl_result nr_http action t) else None in
285-
http_gets ~setup ?result ?max_size url
310+
http_gets ~setup ?timer ?result ?max_size url
286311

287-
let http_request ?ua ?timeout ?verbose ?setup ?max_size ?http_1_0 ?headers ?body (action:http_action) url =
288-
http_request' ?ua ?timeout ?verbose ?setup ?max_size ?http_1_0 ?headers ?body action url >>= fun res ->
312+
let http_request ?ua ?timeout ?verbose ?setup ?timer ?max_size ?http_1_0 ?headers ?body (action:http_action) url =
313+
http_request' ?ua ?timeout ?verbose ?setup ?timer ?max_size ?http_1_0 ?headers ?body action url >>= fun res ->
289314
return @@ simple_result ?verbose res
290315

291-
let http_request_exn ?ua ?timeout ?verbose ?setup ?max_size ?http_1_0 ?headers ?body (action:http_action) url =
292-
http_request ?ua ?timeout ?verbose ?setup ?max_size ?http_1_0 ?headers ?body action url
316+
let http_request_exn ?ua ?timeout ?verbose ?setup ?timer ?max_size ?http_1_0 ?headers ?body (action:http_action) url =
317+
http_request ?ua ?timeout ?verbose ?setup ?timer ?max_size ?http_1_0 ?headers ?body action url
293318
>>= function `Ok s -> return s | `Error error -> fail "%s" error
294319

295-
let http_query ?ua ?timeout ?verbose ?setup ?max_size ?http_1_0 ?headers ?body (action:http_action) url =
320+
let http_query ?ua ?timeout ?verbose ?setup ?timer ?max_size ?http_1_0 ?headers ?body (action:http_action) url =
296321
let body = match body with Some (ct,s) -> Some (`Raw (ct,s)) | None -> None in
297-
http_request ?ua ?timeout ?verbose ?setup ?max_size ?http_1_0 ?headers ?body action url
322+
http_request ?ua ?timeout ?verbose ?setup ?timer ?max_size ?http_1_0 ?headers ?body action url
298323

299-
let http_submit ?ua ?timeout ?verbose ?setup ?http_1_0 ?headers ?(action=`POST) url args =
300-
http_request ?ua ?timeout ?verbose ?setup ?http_1_0 ?headers ~body:(`Form args) action url
324+
let http_submit ?ua ?timeout ?verbose ?setup ?timer ?http_1_0 ?headers ?(action=`POST) url args =
325+
http_request ?ua ?timeout ?verbose ?setup ?timer ?http_1_0 ?headers ~body:(`Form args) action url
301326

302327
end
303328

0 commit comments

Comments
 (0)