66
77module Main where
88
9- import Control.Concurrent (forkIO )
10- import Control.Concurrent.Async (forConcurrently_ )
9+ import Control.Concurrent.Async (forConcurrently_ , link , wait , withAsync )
1110import Control.Concurrent.MVar (newEmptyMVar , putMVar , takeMVar )
1211import Control.Exception (finally , throwIO )
13- import Control.Monad (forM_ , replicateM , void , when )
12+ import Control.Monad (forM_ , forever , replicateM , void , when )
1413import qualified Data.ByteString as BS
15- import Data.IORef (
16- atomicModifyIORef' ,
17- newIORef ,
18- )
14+ import Data.IORef
15+ ( atomicModifyIORef' ,
16+ newIORef ,
17+ )
1918import Data.List.NonEmpty (NonEmpty (.. ))
20- import Network.Transport (
21- Connection (send ),
22- EndPoint (address , connect , receive ),
23- Event (ConnectionOpened , Received ),
24- Reliability (ReliableOrdered ),
25- Transport (closeTransport , newEndPoint ),
26- defaultConnectHints ,
27- )
19+ import qualified Network.Socket as N
20+ import Network.Transport
21+ ( Connection (send ),
22+ EndPoint (address , connect , receive ),
23+ Event (ConnectionOpened , ErrorEvent , Received ),
24+ Reliability (ReliableOrdered ),
25+ Transport (closeTransport , newEndPoint ),
26+ defaultConnectHints ,
27+ )
2828import qualified Network.Transport.QUIC as QUIC
2929import qualified Network.Transport.TCP as TCP
3030import System.FilePath ((</>) )
31- import Test.Tasty (TestTree )
32- import Test.Tasty.Bench (bench , bgroup , defaultMain , nfIO )
31+ import System.Timeout (timeout )
32+ import Test.Tasty (localOption )
33+ import Test.Tasty.Bench (Benchmark , TimeMode (WallTime ), bench , bgroup , defaultMain , nfIO )
3334
3435data TransportConfig = TransportConfig
35- { transportName :: String
36- , mkTransport :: IO Transport
36+ { transportName :: String ,
37+ mkTransport :: IO Transport
3738 }
3839
3940tcpConfig :: TransportConfig
4041tcpConfig =
4142 TransportConfig
42- { transportName = " TCP"
43- , mkTransport = do
43+ { transportName = " TCP" ,
44+ mkTransport = do
4445 Right t <- TCP. createTransport (TCP. defaultTCPAddr " 127.0.0.1" " 0" ) TCP. defaultTCPParameters
4546 pure t
4647 }
4748
4849quicConfig :: TransportConfig
4950quicConfig =
5051 TransportConfig
51- { transportName = " QUIC"
52- , mkTransport =
52+ { transportName = " QUIC" ,
53+ mkTransport =
5354 QUIC. credentialLoadX509
5455 -- Generate a self-signed x509v3 certificate using this nifty tool:
5556 -- https://certificatetools.com/
@@ -59,103 +60,125 @@ quicConfig =
5960 Left errmsg -> throwIO $ userError errmsg
6061 Right credentials ->
6162 QUIC. createTransport
62- ( QUIC. QUICTransportConfig
63- { hostName = " 127.0.0.1 "
64- , serviceName = " 0 "
65- , credentials = credentials :| []
66- , -- credentials are self-signed
67- validateCredentials = False
63+ ( ( QUIC. defaultQUICTransportConfig " 127.0.0.1 " (credentials :| [] ))
64+ { QUIC. serviceName = " 0 " ,
65+ QUIC. validateCredentials = False ,
66+ -- For benchmarks with lots of streams and tiny messages, we can easily
67+ -- overflow the receive buffer
68+ QUIC. socketOptions = [( N. RecvBuffer , 4 * 1024 * 1024 )]
6869 }
6970 )
7071 }
7172
7273data BenchParams = BenchParams
73- { messageSize :: ! Int
74- , messageCount :: ! Int
75- , connectionCount :: ! Int
74+ { messageSize :: ! Int ,
75+ messageCount :: ! Int ,
76+ connectionCount :: ! Int
7677 }
7778
7879smallMessages , mediumMessages , largeMessages :: BenchParams
79- smallMessages = BenchParams {messageSize = 64 , messageCount = 10_000 , connectionCount = 1 }
80- mediumMessages = BenchParams {messageSize = 1024 , messageCount = 1_000 , connectionCount = 1 }
81- largeMessages = BenchParams {messageSize = 4096 , messageCount = 100 , connectionCount = 1 }
80+ smallMessages = BenchParams {messageSize = 64 , messageCount = 10_000 , connectionCount = 1 }
81+ mediumMessages = BenchParams {messageSize = 1024 , messageCount = 1_000 , connectionCount = 1 }
82+ largeMessages = BenchParams {messageSize = 4096 , messageCount = 100 , connectionCount = 1 }
8283
8384multiConn :: Int -> BenchParams -> BenchParams
84- multiConn n p = p{connectionCount = n}
85+ multiConn n p = p {connectionCount = n}
8586
8687throughputBench :: TransportConfig -> BenchParams -> IO ()
87- throughputBench TransportConfig {mkTransport} BenchParams {messageSize, messageCount, connectionCount} = do
88+ throughputBench cfg params =
89+ timeout 30_000_000 (throughputBench' cfg params)
90+ >>= maybe (throwIO $ userError " benchmark stalled: timed out waiting for messages" ) pure
91+
92+ throughputBench' :: TransportConfig -> BenchParams -> IO ()
93+ throughputBench' TransportConfig {mkTransport} BenchParams {messageSize, messageCount, connectionCount} = do
8894 transport <- mkTransport
89- flip finally (closeTransport transport) $ do
95+ -- Closing is bounded as well: it can block if a connection was lost, and that
96+ -- would hide the failure we are trying to report.
97+ flip finally (void $ timeout 5_000_000 (closeTransport transport)) $ do
9098 Right senderEP <- newEndPoint transport
9199 Right receiverEP <- newEndPoint transport
92100
93101 let payload = BS. replicate messageSize 0x42
94102 totalMessages = messageCount * connectionCount
95103
96104 receiverReady <- newEmptyMVar
97- receiverDone <- newEmptyMVar
98-
99- void $ forkIO $ do
100- connsEstablished <- newIORef (0 :: Int )
101- let waitForConnections = do
102- event <- receive receiverEP
103- case event of
104- ConnectionOpened {} -> do
105- n <- atomicModifyIORef' connsEstablished (\ x -> (x + 1 , x + 1 ))
106- when (n < connectionCount) waitForConnections
107- _ -> waitForConnections
108- waitForConnections
109- putMVar receiverReady ()
110-
111- msgsReceived <- newIORef (0 :: Int )
112- let recvLoop = do
113- event <- receive receiverEP
114- case event of
115- Received _ _ -> do
116- n <- atomicModifyIORef' msgsReceived (\ x -> (x + 1 , x + 1 ))
117- when (n < totalMessages) recvLoop
118- _ -> recvLoop
119- recvLoop
120- putMVar receiverDone ()
121-
122- let receiverAddr = address receiverEP
123- connections <-
124- replicateM
125- connectionCount
126- (connect senderEP receiverAddr ReliableOrdered defaultConnectHints >>= either throwIO pure )
127-
128- takeMVar receiverReady
129-
130- forConcurrently_ connections $ \ conn ->
131- forM_ [0 .. messageCount] $ \ _ -> send conn [payload]
132-
133- takeMVar receiverDone
134-
135- benchTransport :: TransportConfig -> TestTree
136- benchTransport cfg@ TransportConfig {transportName} =
105+
106+ let receiver = do
107+ connsEstablished <- newIORef (0 :: Int )
108+ let waitForConnections = do
109+ event <- receive receiverEP
110+ case event of
111+ ConnectionOpened {} -> do
112+ n <- atomicModifyIORef' connsEstablished (\ x -> (x + 1 , x + 1 ))
113+ when (n < connectionCount) waitForConnections
114+ ErrorEvent err -> throwIO err
115+ _ -> waitForConnections
116+ waitForConnections
117+ putMVar receiverReady ()
118+
119+ msgsReceived <- newIORef (0 :: Int )
120+ let recvLoop = do
121+ event <- receive receiverEP
122+ case event of
123+ Received _ _ -> do
124+ n <- atomicModifyIORef' msgsReceived (\ x -> (x + 1 , x + 1 ))
125+ when (n < totalMessages) recvLoop
126+ ErrorEvent err -> throwIO err
127+ _ -> recvLoop
128+ recvLoop
129+
130+ let watchSender = forever $ do
131+ event <- receive senderEP
132+ case event of
133+ ErrorEvent err -> throwIO err
134+ _ -> pure ()
135+
136+ withAsync receiver $ \ receiverAsync -> withAsync watchSender $ \ senderAsync -> do
137+ link receiverAsync
138+ link senderAsync
139+
140+ let receiverAddr = address receiverEP
141+ connections <-
142+ replicateM
143+ connectionCount
144+ (connect senderEP receiverAddr ReliableOrdered defaultConnectHints >>= either throwIO pure )
145+
146+ takeMVar receiverReady
147+
148+ forConcurrently_ connections $ \ conn ->
149+ forM_ [0 .. messageCount] $ \ _ -> send conn [payload] >>= either throwIO pure
150+
151+ wait receiverAsync
152+
153+ benchTransport :: TransportConfig -> Benchmark
154+ benchTransport cfg@ TransportConfig {transportName} =
137155 bgroup
138156 transportName
139157 [ bgroup
140158 " throughput"
141159 [ bgroup
142160 " single-connection"
143- [ bench " small-msg" $ nfIO $ throughputBench cfg smallMessages
144- , bench " default-msg" $ nfIO $ throughputBench cfg mediumMessages
145- , bench " large-msg" $ nfIO $ throughputBench cfg largeMessages
146- ]
147- , bgroup
161+ [ bench " small-msg" $ nfIO $ throughputBench cfg smallMessages,
162+ bench " default-msg" $ nfIO $ throughputBench cfg mediumMessages,
163+ bench " large-msg" $ nfIO $ throughputBench cfg largeMessages
164+ ],
165+ bgroup
148166 " multi-connection"
149- [ bench " 2-conn" $ nfIO $ throughputBench cfg smallMessages{connectionCount = 2 , messageCount = 10_000 }
150- , bench " 5-conn" $ nfIO $ throughputBench cfg smallMessages{connectionCount = 5 , messageCount = 10_000 }
151- , bench " 10-conn" $ nfIO $ throughputBench cfg smallMessages{connectionCount = 10 , messageCount = 5_000 }
167+ [ bench " 2-conn" $ nfIO $ throughputBench cfg smallMessages {connectionCount = 2 , messageCount = 10_000 },
168+ bench " 5-conn" $ nfIO $ throughputBench cfg smallMessages {connectionCount = 5 , messageCount = 10_000 },
169+ bench " 10-conn" $ nfIO $ throughputBench cfg smallMessages {connectionCount = 10 , messageCount = 5_000 },
170+ bench " 50-conn" $ nfIO $ throughputBench cfg smallMessages {connectionCount = 50 , messageCount = 100 },
171+ bench " 100-conn" $ nfIO $ throughputBench cfg smallMessages {connectionCount = 100 , messageCount = 50 }
152172 ]
153173 ]
154174 ]
155175
156176main :: IO ()
157177main =
158178 defaultMain
159- [ benchTransport tcpConfig
160- , benchTransport quicConfig
179+ -- QUIC is a userspace networking protocol,
180+ -- so CPU time isn't the appropriate comparison
181+ -- to make with TCP
182+ [ localOption WallTime (benchTransport tcpConfig),
183+ localOption WallTime (benchTransport quicConfig)
161184 ]
0 commit comments