@@ -3,15 +3,17 @@ module Main where
33import Network.Transport
44import Network.Transport.TCP (mkTransport, TCPConfig (..))
55
6−import Control.Monad (forever, replicateM_)
6+import Control.DeepSeq (deepseq)
7+import Control.Monad (forever, replicateM, replicateM_)
78import Criterion.Main (Benchmark, bench, defaultMain, nfIO)
8−import Data.Binary
9−import Data.ByteString.Lazy (ByteString)
9+import Data.Serialize
10+import Data.ByteString (ByteString)
1011import Data.Maybe (fromJust)
1112import Data.Int
1213import System.Environment (getArgs, withArgs)
14+import System.Random
1315
14−import qualified Data.ByteString.Lazy as BS
16+import qualified Data.ByteString.Char8 as BS
1517
1618-- | This performs a benchmark on the TCP transport to measure how long
1719-- it takes to transfer a number of bytes. This can be compiled using:
@@ -67,17 +69,19 @@ main = do
6769 send sourceEndPing [serialize sourceAddrPong]
6870
6971 -- benchmark the data
70− let bs = BS.replicate size 0
72+ rands <- replicateM size randomIO
73+ let bs' = BS.pack (rands `deepseq` (rands :: [Char]))
74+ bs = BS.unpack bs' `deepseq` bs'
7175 withArgs args' $ defaultMain [ benchSend sourceEndPing targetEndPong bs ]
7276
7377-- | The effect of `ping sourceEndPing targetEndPong bs` is to send the
7478-- `ByteString` `bs` using `sourceEndPing`, and to then receive
7579-- a single byte back from `targetEndPong`.
76−ping :: SourceEnd -> TargetEnd -> ByteString -> IO Word8
80+ping :: SourceEnd -> TargetEnd -> ByteString -> IO Bool
7781ping sourceEndPing targetEndPong bs = do
7882 send sourceEndPing [bs]
79− [cs] <- receive targetEndPong
80− return $ decode cs
83+ cs <- receive targetEndPong
84+ return $! BS.head bs == BS.head (head cs)
8185
8286-- | This function takes a `TargetEnd` for the pings, and a `SourceEnd` for
8387-- pongs. Whenever a ping is received from the `TargetEnd`, a pong is sent
@@ -85,7 +89,7 @@ ping sourceEndPing targetEndPong bs = do
8589pong :: TargetEnd -> SourceEnd -> IO ()
8690pong targetEndPing sourceEndPong = do
8791 bs <- receive targetEndPing
88− send sourceEndPong [encode (0 :: Word8)]
92+ send sourceEndPong bs
8993
9094-- | The effect of `benchSend sourceEndPing targetEndPong bs` is to send
9195-- `bs` pings down `sourceEndPing` using the `ping` function. The time