@@ -41,6 +41,16 @@ let url_get_args url = try String.split url "?" |> snd |> parse_url_args with _
41
41
42
42
let () = Curl. global_init Curl. CURLINIT_GLOBALALL
43
43
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
+
44
54
module CurlCache = Cache. Reuse (struct type t = Curl. t let create = Curl. init let reset = Curl. reset end )
45
55
46
56
let curl_default_setup h =
@@ -114,6 +124,7 @@ module type HTTP = sig
114
124
val with_curl_cache : (Curl .t -> 'a IO .t ) -> 'a IO .t
115
125
val http_gets :
116
126
?setup : (Curl .t -> unit ) ->
127
+ ?timer : Action .timer ->
117
128
?max_size : int ->
118
129
?check : (Curl .t -> bool ) ->
119
130
?result : (Curl .t -> Curl .curlCode -> unit IO .t ) ->
@@ -124,6 +135,7 @@ module type HTTP = sig
124
135
?timeout:int ->
125
136
?verbose:bool ->
126
137
?setup:(Curl .t -> unit ) ->
138
+ ?timer:Action .timer ->
127
139
?max_size:int ->
128
140
?http_1_0:bool ->
129
141
?headers:string list ->
@@ -141,6 +153,7 @@ module type HTTP = sig
141
153
?timeout : int ->
142
154
?verbose : bool ->
143
155
?setup : (Curl .t -> unit ) ->
156
+ ?timer : Action .timer ->
144
157
?http_1_0 : bool ->
145
158
?headers : string list ->
146
159
?action : http_action ->
@@ -167,6 +180,7 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
167
180
?timeout:int ->
168
181
?verbose:bool ->
169
182
?setup:(Curl .t -> unit ) ->
183
+ ?timer:Action .timer ->
170
184
?max_size:int ->
171
185
?http_1_0:bool ->
172
186
?headers:string list ->
@@ -182,8 +196,18 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
182
196
let with_curl f = bracket (return @@ Curl. init () ) (fun h -> Curl. cleanup h; return_unit) f
183
197
let with_curl_cache f = bracket (return @@ CurlCache. get () ) (fun h -> CurlCache. release h; return_unit) f
184
198
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
+
185
209
(* 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 =
187
211
with_curl_cache begin fun h ->
188
212
Curl. set_url h url;
189
213
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
201
225
| Some max_size when ! read_size > max_size -> Exn. fail " received too much data (%db) when max is %db" ! read_size max_size
202
226
| _ -> l
203
227
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 () ->
206
231
return @@ match code with
207
232
| Curl. CURLE_OK -> `Ok (Curl. get_httpcode h, Buffer. contents b)
208
233
| code -> `Error code
@@ -236,7 +261,7 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
236
261
237
262
(* NOTE don't forget to set http_1_0=true when sending requests to a Httpev-based server *)
238
263
(* 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 =
240
265
let open Curl in
241
266
let set_body_and_headers h ct body =
242
267
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
282
307
end ;
283
308
let t = new Action. timer in
284
309
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
286
311
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 ->
289
314
return @@ simple_result ?verbose res
290
315
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
293
318
>> = function `Ok s -> return s | `Error error -> fail " %s" error
294
319
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 =
296
321
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
298
323
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
301
326
302
327
end
303
328
0 commit comments