-threadedでコンパイルすると何故か遅くなる。
2通りの実装書いたけど、どっちもだいたい同じパフォーマンスと傾向。
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Control.Concurrent (threadDelay)
import Control.Concurrent.Async (async, race_, wait)
import Control.Monad
import qualified Data.ByteString as S
import Data.ByteString.Char8 ()
import Data.Conduit
import Data.Conduit.Network
import Network
import System.Environment
import System.IO
main :: IO ()
main = withSocketsDo $ do
[tn, rn] <- map read <$> getArgs
server `race_` do
threadDelay 1000
mapM_ wait =<< replicateM tn (async $ client rn)
{-
server :: IO ()
server = runTCPServer (ServerSettings 12345 "*") $ \src sink -> src $$ sink
client :: Int -> IO ()
client n = runTCPClient (ClientSettings 12345 "localhost") $ \src sink ->
src $$ cond =$ sink
where
cond = replicateM_ n $ do
yield "hello"
void await
-}
server :: IO ()
server = do
ssock <- listenOn $ PortNumber 12345
forever $ do
(h, _, _) <- accept ssock
let go = do
bs <- S.hGetSome h 1024
when (not $ S.null bs) $ do
S.hPut h bs
hFlush h
go
void $ async go
client :: Int -> IO ()
client n = do
h <- connectTo "localhost" (PortNumber 12345)
replicateM_ n $ do
S.hPut h "hello"
hFlush h
_ <- S.hGetSome h 1024
return ()
hClose h