Created
November 11, 2012 23:37
-
-
Save scvalex/4056726 to your computer and use it in GitHub Desktop.
Test the effective speed of a network link
This file contains 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
module Main where | |
import Control.Concurrent | |
import Control.Exception | |
import Control.Monad | |
import qualified Data.ByteString as BS | |
import Network.Socket hiding ( recv ) | |
import Network.Socket.ByteString ( recv ) | |
import Text.Printf ( printf ) | |
main :: IO () | |
main = do | |
bracketOnError | |
(socket AF_INET Stream defaultProtocol) | |
sClose | |
(\lsock -> do | |
setSocketOption lsock ReuseAddr 1 | |
bindSocket lsock (SockAddrInet 12345 iNADDR_ANY) | |
listen lsock maxListenQueue | |
forever $ do | |
(sock, addr) <- accept lsock | |
_ <- printf "Incoming connection from %s\n" (show addr) | |
_ <- forkIO $ finally | |
(forever $ do | |
text <- recv sock (2 * 1024) | |
when (BS.null text) (sClose sock)) | |
(sClose sock) | |
return ()) |
This file contains 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
module Main where | |
import Control.Exception | |
import Control.Monad | |
import qualified Data.ByteString as BS | |
import Data.Time.Clock | |
import Network.Socket | |
import Network.Socket.ByteString | |
import Text.Printf ( printf ) | |
main :: IO () | |
main = do | |
addrInfos <- getAddrInfo (Just (defaultHints { addrFamily = AF_INET })) | |
(Just "localhost") | |
(Just "12345") | |
bracketOnError | |
(socket AF_INET Stream defaultProtocol) | |
sClose | |
(\sock -> do | |
connect sock (addrAddress $ head addrInfos) | |
sendLoop sock) | |
sendLoop :: Socket -> IO () | |
sendLoop sock = do | |
let bs = BS.replicate (1024 * 1024 * 1024) (fromIntegral (23 :: Int)) | |
let text = bs `seq` bs | |
startTime <- getCurrentTime | |
forM_ [1..50 :: Int] $ \i -> do | |
sendAll sock text | |
now <- getCurrentTime | |
printf "Sent %d GiB in %d x %s\n" i i (show (diffUTCTime now startTime / fromIntegral i)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment