-
Notifications
You must be signed in to change notification settings - Fork 4
/
Lib.hs
54 lines (42 loc) · 1.36 KB
/
Lib.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{-# LANGUAGE OverloadedStrings #-}
module Lib
( run
) where
import Data.Conduit.Network
import Data.Conduit.List as CL
import Data.Conduit
import System.Environment
import Control.Concurrent
import Control.Concurrent.Async
import Data.Time.Clock.POSIX
import Data.Conduit.Binary as CB
import qualified Data.ByteString as BS
msg = BS.concat $ Prelude.replicate 100 "this is my message bitch"
readLen = BS.length msg
run = runBench
runBench :: IO ()
runBench = do
args <- getArgs
let clientCount = read $ args !! 0
let testTime = read $ args !! 1 -- in seconds
putStrLn $ "testing for " ++ show clientCount ++ " clients for " ++ show testTime ++ " seconds."
reqCounts <- runConns clientCount testTime
putStrLn $ "the server completed " ++ show (sum reqCounts)
runConns clientCount testTime = do
mapConcurrently (connThread testTime) [1..clientCount]
connThread testTime tid = do
start <- getTime
loop testTime start 0
loop testTime startTime reqCount = do
runConn
now <- getTime
if (now - startTime < testTime)
then loop testTime startTime (reqCount + 1)
else return reqCount -- we're done
runConn :: IO ()
runConn = runTCPClient (clientSettings 4000 "127.0.0.1") $ \app -> do
sourceList [msg] $$ appSink app
res <- appSource app =$ CB.isolate readLen $$ CL.consume
return ()
getTime :: IO Integer
getTime = (round) `fmap` getPOSIXTime