Created
September 14, 2012 06:22
-
-
Save tanakh/3720170 to your computer and use it in GitHub Desktop.
threadedにすると何故か遅い ref: http://qiita.com/items/6755af56e53ce184d327
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment