@@ -48,10 +48,12 @@ readBenchmark sndSizeV sndSize addr = do
4848 (\ sd -> do
4949 atomically $ putTMVar sndSizeV sndSize
5050 Socket. connect sd addr
51- bearer <- getBearer makeSocketBearer sduTimeout activeTracer sd
51+ withReadBufferIO (\ buffer -> do
52+ bearer <- getBearer makeSocketBearer sduTimeout activeTracer sd buffer
5253
53- let chan = bearerAsChannel bearer (MiniProtocolNum 42 ) InitiatorDir
54- doRead (totalPayloadLen sndSize) chan 0
54+ let chan = bearerAsChannel bearer (MiniProtocolNum 42 ) InitiatorDir
55+ doRead (totalPayloadLen sndSize) chan 0
56+ )
5557 )
5658 where
5759 doRead :: Int64 -> ByteChannel IO -> Int64 -> IO ()
@@ -72,15 +74,17 @@ readDemuxerBenchmark sndSizeV sndSize addr = do
7274 atomically $ putTMVar sndSizeV sndSize
7375
7476 Socket. connect sd addr
75- bearer <- getBearer makeSocketBearer sduTimeout activeTracer sd
76- ms42 <- mkMiniProtocolState 42
77- ms41 <- mkMiniProtocolState 41
78- withAsync (demuxer [ms41, ms42] bearer) $ \ aid -> do
79- withAsync (doRead 42 (totalPayloadLen sndSize) (miniProtocolIngressQueue ms42) 0 ) $ \ aid42 -> do
80- withAsync (doRead 41 (totalPayloadLen 10 ) (miniProtocolIngressQueue ms41) 0 ) $ \ aid41 -> do
81- _ <- waitBoth aid42 aid41
82- cancel aid
83- return ()
77+ withReadBufferIO (\ buffer -> do
78+ bearer <- getBearer makeSocketBearer sduTimeout activeTracer sd buffer
79+ ms42 <- mkMiniProtocolState 42
80+ ms41 <- mkMiniProtocolState 41
81+ withAsync (demuxer [ms41, ms42] bearer) $ \ aid -> do
82+ withAsync (doRead 42 (totalPayloadLen sndSize) (miniProtocolIngressQueue ms42) 0 ) $ \ aid42 -> do
83+ withAsync (doRead 41 (totalPayloadLen 10 ) (miniProtocolIngressQueue ms41) 0 ) $ \ aid41 -> do
84+ _ <- waitBoth aid42 aid41
85+ cancel aid
86+ return ()
87+ )
8488 )
8589 where
8690
@@ -111,37 +115,39 @@ readDemuxerBenchmark sndSizeV sndSize addr = do
111115startServer :: StrictTMVar IO Int64 -> Socket -> IO ()
112116startServer sndSizeV ad = forever $ do
113117 (sd, _) <- Socket. accept ad
114- bearer <- getBearer makeSocketBearer sduTimeout activeTracer sd
115- sndSize <- atomically $ takeTMVar sndSizeV
116-
117- let chan = bearerAsChannel bearer (MiniProtocolNum 42 ) ResponderDir
118- payload = BL. replicate sndSize 0xa5
119- maxData = totalPayloadLen sndSize
120- numberOfSdus = fromIntegral $ maxData `div` sndSize
121- replicateM_ numberOfSdus $ do
122- send chan payload
123-
118+ withReadBufferIO (\ buffer -> do
119+ bearer <- getBearer makeSocketBearer sduTimeout activeTracer sd buffer
120+ sndSize <- atomically $ takeTMVar sndSizeV
121+
122+ let chan = bearerAsChannel bearer (MiniProtocolNum 42 ) ResponderDir
123+ payload = BL. replicate sndSize 0xa5
124+ maxData = totalPayloadLen sndSize
125+ numberOfSdus = fromIntegral $ maxData `div` sndSize
126+ replicateM_ numberOfSdus $ do
127+ send chan payload
128+ )
124129-- | Like startServer but it uses the `writeMany` function
125130-- for vector IO.
126131startServerMany :: StrictTMVar IO Int64 -> Socket -> IO ()
127132startServerMany sndSizeV ad = forever $ do
128133 (sd, _) <- Socket. accept ad
129- bearer <- getBearer makeSocketBearer sduTimeout activeTracer sd
130- sndSize <- atomically $ takeTMVar sndSizeV
131-
132- let maxData = totalPayloadLen sndSize
133- numberOfSdus = fromIntegral $ maxData `div` sndSize
134- numberOfCalls = numberOfSdus `div` 10
135- runtSdus = numberOfSdus `mod` 10
136-
137- withTimeoutSerial $ \ timeoutFn -> do
138- replicateM_ numberOfCalls $ do
139- let sdus = replicate 10 $ wrap $ BL. replicate sndSize 0xa5
140- void $ writeMany bearer timeoutFn sdus
141- when (runtSdus > 0 ) $ do
142- let sdus = replicate runtSdus $ wrap $ BL. replicate sndSize 0xa5
143- void $ writeMany bearer timeoutFn sdus
144-
134+ withReadBufferIO (\ buffer -> do
135+ bearer <- getBearer makeSocketBearer sduTimeout activeTracer sd buffer
136+ sndSize <- atomically $ takeTMVar sndSizeV
137+
138+ let maxData = totalPayloadLen sndSize
139+ numberOfSdus = fromIntegral $ maxData `div` sndSize
140+ numberOfCalls = numberOfSdus `div` 10
141+ runtSdus = numberOfSdus `mod` 10
142+
143+ withTimeoutSerial $ \ timeoutFn -> do
144+ replicateM_ numberOfCalls $ do
145+ let sdus = replicate 10 $ wrap $ BL. replicate sndSize 0xa5
146+ void $ writeMany bearer timeoutFn sdus
147+ when (runtSdus > 0 ) $ do
148+ let sdus = replicate runtSdus $ wrap $ BL. replicate sndSize 0xa5
149+ void $ writeMany bearer timeoutFn sdus
150+ )
145151 where
146152 -- wrap a 'ByteString' as 'SDU'
147153 wrap :: BL. ByteString -> SDU
@@ -163,41 +169,43 @@ startServerMany sndSizeV ad = forever $ do
163169startServerEgresss :: StrictTMVar IO Int64 -> Socket -> IO ()
164170startServerEgresss sndSizeV ad = forever $ do
165171 (sd, _) <- Socket. accept ad
166- bearer <- getBearer makeSocketBearer sduTimeout activeTracer sd
167- sndSize <- atomically $ takeTMVar sndSizeV
168- eq <- atomically $ newTBQueue 100
169- w42 <- newTVarIO BL. empty
170- w41 <- newTVarIO BL. empty
171-
172- let maxData = totalPayloadLen sndSize
173- numberOfSdus = fromIntegral $ maxData `div` sndSize
174- numberOfCalls = numberOfSdus `div` 10 :: Int
175- runtSdus = numberOfSdus `mod` 10 :: Int
176-
177- withAsync (muxer eq bearer) $ \ aid -> do
178-
179- replicateM_ numberOfCalls $ do
180- let payload42s = replicate 10 $ BL. replicate sndSize 42
181- let payload41s = replicate 10 $ BL. replicate 10 41
182- mapM_ (sendToMux w42 eq (MiniProtocolNum 42 ) ResponderDir ) payload42s
183- mapM_ (sendToMux w41 eq (MiniProtocolNum 41 ) ResponderDir ) payload41s
184- when (runtSdus > 0 ) $ do
185- let payload42s = replicate runtSdus $ BL. replicate sndSize 42
186- let payload41s = replicate runtSdus $ BL. replicate 10 41
187- mapM_ (sendToMux w42 eq (MiniProtocolNum 42 ) ResponderDir ) payload42s
188- mapM_ (sendToMux w41 eq (MiniProtocolNum 41 ) ResponderDir ) payload41s
189-
190- -- Wait for the egress queue to empty
191- atomically $ do
192- r42 <- readTVar w42
193- r41 <- readTVar w42
194- unless (BL. null r42 || BL. null r41) retry
195-
196- -- when the client is done they will close the socket
197- -- and we will read zero bytes.
198- _ <- Socket. recv sd 128
199-
200- cancel aid
172+ withReadBufferIO (\ buffer -> do
173+ bearer <- getBearer makeSocketBearer sduTimeout activeTracer sd buffer
174+ sndSize <- atomically $ takeTMVar sndSizeV
175+ eq <- atomically $ newTBQueue 100
176+ w42 <- newTVarIO BL. empty
177+ w41 <- newTVarIO BL. empty
178+
179+ let maxData = totalPayloadLen sndSize
180+ numberOfSdus = fromIntegral $ maxData `div` sndSize
181+ numberOfCalls = numberOfSdus `div` 10 :: Int
182+ runtSdus = numberOfSdus `mod` 10 :: Int
183+
184+ withAsync (muxer eq bearer) $ \ aid -> do
185+
186+ replicateM_ numberOfCalls $ do
187+ let payload42s = replicate 10 $ BL. replicate sndSize 42
188+ let payload41s = replicate 10 $ BL. replicate 10 41
189+ mapM_ (sendToMux w42 eq (MiniProtocolNum 42 ) ResponderDir ) payload42s
190+ mapM_ (sendToMux w41 eq (MiniProtocolNum 41 ) ResponderDir ) payload41s
191+ when (runtSdus > 0 ) $ do
192+ let payload42s = replicate runtSdus $ BL. replicate sndSize 42
193+ let payload41s = replicate runtSdus $ BL. replicate 10 41
194+ mapM_ (sendToMux w42 eq (MiniProtocolNum 42 ) ResponderDir ) payload42s
195+ mapM_ (sendToMux w41 eq (MiniProtocolNum 41 ) ResponderDir ) payload41s
196+
197+ -- Wait for the egress queue to empty
198+ atomically $ do
199+ r42 <- readTVar w42
200+ r41 <- readTVar w42
201+ unless (BL. null r42 || BL. null r41) retry
202+
203+ -- when the client is done they will close the socket
204+ -- and we will read zero bytes.
205+ _ <- Socket. recv sd 128
206+
207+ cancel aid
208+ )
201209 where
202210 sendToMux :: StrictTVar IO BL. ByteString -> EgressQueue IO -> MiniProtocolNum -> MiniProtocolDir
203211 -> BL. ByteString -> IO ()
0 commit comments