• # OCaml (avec cohttp)

    Posté par (site web personnel) . En réponse au journal Le taptempo du web. Évalué à 2.

    Et voici une version en OCaml, j’ai utilisé cohttp qui est une librairie bas-niveau pour construire des requêtes client/serveur.

    open Cohttp
    open Cohttp_lwt_unix
    let server imgCount =
     let callback _conn req body =
     ignore body;
     let headers =
     Header.add (Request.headers req) "Location"
     (String.concat ""
     [
     "https://avatar.spacefox.fr/Renard-";
     string_of_int @@ (1 + Random.int imgCount);
     ".png";
     ])
     in
     Server.respond ~headers ~status:`Temporary_redirect ~body:`Empty ()
     in
     Server.create ~mode:(`TCP (`Port 8000)) (Server.make ~callback ())
    let () = Lwt_main.run (server 5)

    La sortie ps :

    $ ps aux | grep tempo
    sebasti+ 23319 0.8 0.4 119996 16464 pts/2 Sl+ 13:49 0:03 _build/default/tempo.exe

    et le résultat du bench :

    $ ab -n 100000 -c 10 http://localhost:8000/
    This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/
    Benchmarking localhost (be patient)
    ...
    Finished 100000 requests
    Server Software: 
    Server Hostname: localhost
    Server Port: 8000
    Document Path: /
    Document Length: 0 bytes
    Concurrency Level: 10
    Time taken for tests: 3.350 seconds
    Complete requests: 100000
    Failed requests: 0
    Non-2xx responses: 100000
    Total transferred: 18600000 bytes
    HTML transferred: 0 bytes
    Requests per second: 29851.08 [#/sec] (mean)
    Time per request: 0.335 [ms] (mean)
    Time per request: 0.033 [ms] (mean, across all concurrent requests)
    Transfer rate: 5422.17 [Kbytes/sec] received
    Connection Times (ms)
     min mean[+/-sd] median max
    Connect: 0 0 0.0 0 1
    Processing: 0 0 0.5 0 51
    Waiting: 0 0 0.5 0 51
    Total: 0 0 0.5 0 51
    Percentage of the requests served within a certain time (ms)
     50% 0
     66% 0
     75% 0
     80% 0
     90% 1
     95% 1
     98% 1
     99% 2
     100% 51 (longest request)