Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View Main.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
module Main where
 
import System.Environment (getArgs)
import Control.Monad
 
import qualified Data.ByteString.Lazy.Char8 as BSL
 
import Network.AMQP
 
connect = do
conn <- openConnection "127.0.0.1" "/" "guest" ""
chan <- openChannel conn
return (conn, chan)
 
main = do
args <- getArgs
case args of
["listen", exName, exType, rKey] -> doListen exName exType rKey
["send", exName, rKey, mBody] -> doSend exName rKey mBody
["flood", exName, rKey, limit] -> doFlood exName rKey limit
_ -> do
putStrLn "Commands:"
putStrLn " listen <exchangeName> <exchangeType> <routingKey>"
putStrLn " send <exchangeName> <routingKey> <message>"
putStrLn " flood <exchangeName <routingKey> <count>"
 
doListen exName exType rKey = do
(conn, chan) <- connect
 
(qName, _, _) <- declareQueue chan newQueue {queueName = "", queueExclusive = True}
declareExchange chan newExchange {exchangeName = exName, exchangeType = exType}
bindQueue chan qName exName rKey
 
consumeMsgs chan qName Ack $ \(msg, env) -> do
ackEnv env
BSL.putStrLn $ msgBody msg
 
putStrLn "go."
getLine
deleteExchange chan exName
closeConnection conn
putStrLn "fin."
 
doSend exName rKey mBody = do
(conn, chan) <- connect
 
let msg = newMsg { msgBody = BSL.pack mBody }
publishMsg chan exName rKey msg
 
closeConnection conn
putStrLn "sent!"
 
doFlood exName rKey limit = do
(conn, chan) <- connect
 
putStrLn "Battle stations!"
forM_ [1 .. read limit] $ \x ->
publishMsg chan exName rKey $ newMsg { msgBody = BSL.pack $ show x, msgDeliveryMode = Just Persistent }
 
-- Close gracefully, letting the connection flush remaining messages.
closeConnection conn
View Main.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# log some messages:
$ ./dist/build/raq/raq listen sendMeStuff direct stuff > 10000.log &
$ ./dist/build/raq/raq flood sendMeStuff stuff 10000
$ fg # [return]
 
# ... repeat for more burst sizes ...
 
# count messages received. 2 lines are from go/fin notices.
$ wc -l *.log
 
9831 10000.log
1002 1000.log
52 100.log
1443 1500.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.