@@ -1,16 +1,22 @@
11module Main where
22
3−import Network.Transport
3+-- TODO: Merge with PingTCPTransport and factor differences.
4+
5+import Network.Transport (newConnection, receive, connect, send,
6+ serialize, deserialize, SourceEnd, TargetEnd)
47import Network.Transport.Pipes (mkTransport)
58
69import Control.Monad (forever, replicateM_)
7−import Criterion.Main (Benchmark, bench, defaultMain, nfIO)
8−import Data.Binary
10+import Criterion.Main (Benchmark, bench, defaultMainWith, nfIO)
11+import Criterion.Config (defaultConfig, ljust, Config(cfgSamples))
12+-- import Data.Binary (encode,decode)
13+import Data.Serialize (encode,decode)
914import Data.Maybe (fromJust)
1015import Data.Int
1116import System.Environment (getArgs, withArgs)
1217
13−import qualified Data.ByteString.Lazy.Char8 as BS
18+-- import qualified Data.ByteString.Lazy.Char8 as LBS
19+import qualified Data.ByteString.Char8 as BS
1420
1521-- | This performs a ping benchmark on the TCP transport. This can be
1622-- compiled using:
@@ -50,7 +56,7 @@ main = do
5056 forever $ pong targetEndPing sourceEndPong
5157
5258
53− "client" : sourceAddrFilePath : pingsStr : args' -> do
59+ "client" : sourceAddrFilePath : pingsStr : reps : args' -> do
5460 let pings = read pingsStr
5561 -- establish transport
5662 transport <- mkTransport
@@ -64,10 +70,17 @@ main = do
6470 send sourceEndPing [serialize sourceAddrPong]
6571
6672 -- benchmark the pings
67−-- withArgs args' $ defaultMain [ benchPing sourceEndPing targetEndPong pings]
68− replicateM_ pings (ping sourceEndPing targetEndPong)
73+ case (read reps) :: Int of
74+ 0 -> error "What would zero reps mean?"
75+ 1 -> do putStrLn "Because you're timing only one trial, skipping Criterion..."
76+ replicateM_ pings (ping sourceEndPing targetEndPong)
77+ n -> withArgs args' $ defaultMainWith
78+ (defaultConfig{ cfgSamples = ljust n })
79+ (return ()) -- Init action.
80+ [ benchPing sourceEndPing targetEndPong (fromIntegral pings) ]
6981 putStrLn "Done with all ping/pongs."
7082
83+
7184-- | This function takes a `TargetEnd` for the pings, and a `SourceEnd` for
7285-- pongs. Whenever a ping is received from the `TargetEnd`, a pong is sent
7386-- in reply down the `SourceEnd`, repeating whatever was sent.
@@ -81,9 +94,14 @@ pong targetEndPing sourceEndPong = do
8194-- `targetEndPong`, which is then returned.
8295ping :: SourceEnd -> TargetEnd -> IO Int64
8396ping sourceEndPing targetEndPong = do
84− send sourceEndPing [BS.empty]
97+ -- send sourceEndPing [BS.empty]
98+ send sourceEndPing [encode (42 :: Int64)]
99+
85100 [bs] <- receive targetEndPong
86− return $ decode bs
101+-- return $ decode bs
102+ case decode bs of
103+ Left err -> error$ "decode failure: " ++ err
104+ Right x -> return x
87105
88106-- | The effect of `benchPing sourceEndPing targetEndPong n` is to send
89107-- `n` pings down `sourceEndPing` using the `ping` function. The time