Create a gist now

Instantly share code, notes, and snippets.

@wiz /Main.hs
Created Oct 9, 2012

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
# 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