@@ -3,15 +3,17 @@ module Main where
3import Network.Transport
3import Network.Transport
4import Network.Transport.TCP (mkTransport, TCPConfig (..))
4import Network.Transport.TCP (mkTransport, TCPConfig (..))
6+import Control.DeepSeq (deepseq)
6−import Control.Monad (forever, replicateM_)
7+import Control.Monad (forever, replicateM, replicateM_)
7import Criterion.Main (Benchmark, bench, defaultMain, nfIO)
8import Criterion.Main (Benchmark, bench, defaultMain, nfIO)
8−import Data.Binary
9+import Data.Serialize
9−import Data.ByteString.Lazy (ByteString)
10+import Data.ByteString (ByteString)
10import Data.Maybe (fromJust)
11import Data.Maybe (fromJust)
11import Data.Int
12import Data.Int
12import System.Environment (getArgs, withArgs)
13import System.Environment (getArgs, withArgs)
14−import qualified Data.ByteString.Lazy as BS
16+import qualified Data.ByteString.Char8 as BS
16-- | This performs a benchmark on the TCP transport to measure how long
18-- | This performs a benchmark on the TCP transport to measure how long
17-- it takes to transfer a number of bytes. This can be compiled using:
19-- it takes to transfer a number of bytes. This can be compiled using:
@@ -67,17 +69,19 @@ main = do
67 send sourceEndPing [serialize sourceAddrPong]
69 send sourceEndPing [serialize sourceAddrPong]
69 -- benchmark the data
71 -- 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'
71 withArgs args' $ defaultMain [ benchSend sourceEndPing targetEndPong bs ]
75 withArgs args' $ defaultMain [ benchSend sourceEndPing targetEndPong bs ]
73-- | The effect of `ping sourceEndPing targetEndPong bs` is to send the
77-- | The effect of `ping sourceEndPing targetEndPong bs` is to send the
74-- `ByteString` `bs` using `sourceEndPing`, and to then receive
78-- `ByteString` `bs` using `sourceEndPing`, and to then receive
75-- a single byte back from `targetEndPong`.
79-- a single byte back from `targetEndPong`.
76−ping :: SourceEnd -> TargetEnd -> ByteString -> IO Word8
80+ping :: SourceEnd -> TargetEnd -> ByteString -> IO Bool
77ping sourceEndPing targetEndPong bs = do
81ping sourceEndPing targetEndPong bs = do
78 send sourceEndPing [bs]
82 send sourceEndPing [bs]
79− [cs] <- receive targetEndPong
83+ cs <- receive targetEndPong
80− return $ decode cs
84+ return $! BS.head bs == BS.head (head cs)
82-- | This function takes a `TargetEnd` for the pings, and a `SourceEnd` for
86-- | This function takes a `TargetEnd` for the pings, and a `SourceEnd` for
83-- pongs. Whenever a ping is received from the `TargetEnd`, a pong is sent
87-- pongs. Whenever a ping is received from the `TargetEnd`, a pong is sent
@@ -85,7 +89,7 @@ ping sourceEndPing targetEndPong bs = do
85pong :: TargetEnd -> SourceEnd -> IO ()
89pong :: TargetEnd -> SourceEnd -> IO ()
86pong targetEndPing sourceEndPong = do
90pong targetEndPing sourceEndPong = do
87 bs <- receive targetEndPing
91 bs <- receive targetEndPing
88− send sourceEndPong [encode (0 :: Word8)]
92+ send sourceEndPong bs
90-- | The effect of `benchSend sourceEndPing targetEndPong bs` is to send
94-- | The effect of `benchSend sourceEndPing targetEndPong bs` is to send
91-- `bs` pings down `sourceEndPing` using the `ping` function. The time
95-- `bs` pings down `sourceEndPing` using the `ping` function. The time