• # une version en haskell

    Posté par . En réponse au journal Java : presque 9 000 requêtes par seconde avec 8 Mo de RAM. Évalué à 5.

    {-# LANGUAGE OverloadedStrings #-}
    import Control.Concurrent.STM
    import qualified Data.ByteString.Char8 as C
    import Fmt
    import Network.Wai 
    import Network.HTTP.Types
    import Network.Wai.Handler.Warp 
    import System.Environment
    import System.Random.Stateful
    import Text.Read 
    app :: RandomGen g => Int -> TGenM g -> Application
    app nImgs rngVar _ respond = do
     i <- atomically $ applyTGen (uniformR (1, nImgs)) rngVar
     let url = fmt $ "https://avatar.spacefox.fr/Renard-" +| i |+ ".png"
     respond $ responseLBS status302 [("Location", url)] ""
    main :: IO ()
    main = do
     args <- fmap readMaybe <$> getArgs
     rngVar <- getStdGen >>= newTGenMIO
     case args of
     [Just port, Just nImgs] -> do
     C.putStrLn $ fmt $ "listening on port " +| port |+ ""
     run port (app nImgs rngVar)
     _ -> C.putStrLn "usage: <port> <nImgs>"

    compilation/exécution:

    $ ghc -O2 -rtsopts serve.hs
    $ ./serve 8080 21
    

    benchmark (AMD Ryzen 7 PRO 4750U, 1.7GHz) :

    $ ab -n 100000 -c 10 http://localhost:8080/
    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)
    Completed 10000 requests
    Completed 20000 requests
    Completed 30000 requests
    Completed 40000 requests
    Completed 50000 requests
    Completed 60000 requests
    Completed 70000 requests
    Completed 80000 requests
    Completed 90000 requests
    Completed 100000 requests
    Finished 100000 requests
    Server Software: Warp/3.3.20
    Server Hostname: localhost
    Server Port: 8080
    Document Path: /
    Document Length: 0 bytes
    Concurrency Level: 10
    Time taken for tests: 5.373 seconds
    Complete requests: 100000
    Failed requests: 0
    Non-2xx responses: 100000
    Total transferred: 13157347 bytes
    HTML transferred: 0 bytes
    Requests per second: 18612.30 [#/sec] (mean)
    Time per request: 0.537 [ms] (mean)
    Time per request: 0.054 [ms] (mean, across all concurrent requests)
    Transfer rate: 2391.49 [Kbytes/sec] received
    Connection Times (ms)
     min mean[+/-sd] median max
    Connect: 0 0 0.0 0 1
    Processing: 0 0 0.1 0 6
    Waiting: 0 0 0.1 0 5
    Total: 0 1 0.1 1 6
    Percentage of the requests served within a certain time (ms)
     50% 1
     66% 1
     75% 1
     80% 1
     90% 1
     95% 1
     98% 1
     99% 1
     100% 6 (longest request)