-
Notifications
You must be signed in to change notification settings - Fork 2
/
Perftest.hs
78 lines (62 loc) · 1.96 KB
/
Perftest.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import Network.Socket
import Control.Monad
import Control.Concurrent
import Control.Exception
import System.IO
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.Char8 as B8
import Query
main :: IO ()
main = do
let keysCount = 10000
runJob 1 (updateMetrics keysCount)
runJob 1 updateLimits
runJob 1 (updateLevels keysCount)
vars <- replicateM 300 $ myForkIO $ runJob 1 (updateMetrics 2000)
mapM_ takeMVar vars
print "Done put"
runJob 10 getOverLimit
runJob 1 stop
runJob :: Int -> (Handle -> IO ()) -> IO ()
runJob n job = bracket newConnHandle hClose (replicateM_ n . job)
keys :: [B.ByteString]
keys = [B8.pack $ "[email protected]" ++ show x | x <- [1..] :: [Integer]]
endpoint :: B.ByteString
endpoint = B8.pack "submit"
levelname :: B.ByteString
levelname = B8.pack "level1"
metrics :: [QMetric]
metrics = [QMetric key endpoint 10 | key <- keys]
levels :: [QLevel]
levels = [QLevel key levelname | key <- keys]
updateMetrics :: Int -> Handle -> IO ()
updateMetrics m h = do
let q = UpdateMetrics $ take m metrics
writeQuery h q
updateLevels :: Int -> Handle -> IO ()
updateLevels m h = do
let q = UpdateLevels $ take m levels
writeQuery h q
updateLimits :: Handle -> IO ()
updateLimits h = do
let q = UpdateLimits [QLimit levelname endpoint 2800]
writeQuery h q
getOverLimit :: Handle -> IO ()
getOverLimit h = do
writeQuery h GetOverLimit
print . length . show =<< readReply h
threadDelay 1000000
stop :: Handle -> IO ()
stop h = writeQuery h Stop
newConnHandle :: IO Handle
newConnHandle = do
addrinfos <- getAddrInfo Nothing (Just "127.0.0.1") (Just "1813")
let serveraddr = head addrinfos
sock <- socket (addrFamily serveraddr) Stream defaultProtocol
connect sock (addrAddress serveraddr)
socketToHandle sock ReadWriteMode
myForkIO :: IO () -> IO (MVar ())
myForkIO io = do
mvar <- newEmptyMVar
_ <- forkFinally io (\_ -> putMVar mvar ())
return mvar