I need to develop a simple performance testing tool for internal use. What I need is sending a brunch of http/tcp requests in every period time without waiting the previous request respond.
Since most of my colleagues write C++, I developed the prototype using C++ (pseudocode):
for (int i=0; i<BIG_NUM; i++) {
sleep(TINY_TIME);
int ret = send_req();
log(ret);
}
In this case, every iter in the for
loop have to wait the response from the target in order to send another request.
So I develop another prototype using OCaml & Async, it works as expected:
open Core
open Async
open Cohttp
open Cohttp_async
module Log = Async.Log
let mock_lst = [111; 222; 333; 444; 555; 666]
let logger : Log.t = Log.create ~level:`Debug
~output:[(Log.Output.file `Text ~filename:"log.txt")] ~on_error:`Raise
let seq_send (url:string) (param:string) : unit Deferred.t =
Log.debug logger "%s\n" (param^" start!");
let uri = Uri.of_string (url^param) in
let%bind _, body = Cohttp_async.Client.get uri in
let%bind body = Body.to_string body in
Log.debug logger "%s\n" body; return ()
let ord_send_iter (lst:int list) : unit Deferred.t =
Deferred.List.iteri ~how:`Parallel lst
~f:(fun idx ele ->
after (sec (float_of_int idx)) >>= fun () ->
(seq_send "http://127.0.0.1:9001/?id=" (string_of_int ele)))
let () =
Command.async
~summary:"test"
Command.Spec.(
empty
)
(fun () -> ord_send_iter mock_lst)
|> Command.run
I was planing to post this question in some C++ forum, but I am not sure they would familiar with both OCaml and Async.
I was wondering if maybe anybody who knows both C++ and OCaml would like to give me some advice?