git.iko.soy/distributed-process
on master help sign in
Single receive makes no difference
f6240c3Edsko de Vries
benchmarks/.gitignore+1
@@ -1,2 +1,3 @@
1*.swp
1*.swp
2*.exe
2*.exe
3+*.data
benchmarks/JustPing.gnuplot+2
@@ -3,10 +3,12 @@ set yrange [0:200]
3plot "JustPingHaskell.data" smooth bezier with lines title "JustPingHaskell", \
3plot "JustPingHaskell.data" smooth bezier with lines title "JustPingHaskell", \
4 "JustPingHaskell2.data" smooth bezier with lines title "JustPingHaskell (-threaded)", \
4 "JustPingHaskell2.data" smooth bezier with lines title "JustPingHaskell (-threaded)", \
5 "JustPingNaiveHeader.data" smooth bezier with lines title "JustPingNaiveHeader", \
5 "JustPingNaiveHeader.data" smooth bezier with lines title "JustPingNaiveHeader", \
6+ "JustPingOneRecv.data" smooth bezier with lines title "JustPingOneRecv", \
6 "JustPingC.data" smooth bezier with lines title "JustPingC"
7 "JustPingC.data" smooth bezier with lines title "JustPingC"
7set terminal postscript color
8set terminal postscript color
8set output "JustPing.ps"
9set output "JustPing.ps"
9plot "JustPingHaskell.data" smooth bezier with lines title "JustPingHaskell", \
10plot "JustPingHaskell.data" smooth bezier with lines title "JustPingHaskell", \
10 "JustPingHaskell2.data" smooth bezier with lines title "JustPingHaskell (-threaded)", \
11 "JustPingHaskell2.data" smooth bezier with lines title "JustPingHaskell (-threaded)", \
11 "JustPingNaiveHeader.data" smooth bezier with lines title "JustPingNaiveHeader", \
12 "JustPingNaiveHeader.data" smooth bezier with lines title "JustPingNaiveHeader", \
13+ "JustPingOneRecv.data" smooth bezier with lines title "JustPingOneRecv", \
12 "JustPingC.data" smooth bezier with lines title "JustPingC"
14 "JustPingC.data" smooth bezier with lines title "JustPingC"
benchmarks/JustPingOneRecv.hsnew file+106
@@ -0,0 +1,106 @@
1+{-# LANGUAGE CPP, BangPatterns #-}
2+
3+module Main where
4+
5+import Control.Monad
6+
7+import Data.Int
8+import qualified Data.Serialize as Ser
9+import Data.Word (Word8)
10+import Network.Socket
11+ ( AddrInfoFlag (AI_PASSIVE), HostName, ServiceName, Socket
12+ , SocketType (Stream), SocketOption (ReuseAddr)
13+ , accept, addrAddress, addrFlags, addrFamily, bindSocket, defaultProtocol
14+ , defaultHints
15+ , getAddrInfo, listen, setSocketOption, socket, sClose, withSocketsDo )
16+import System.Environment (getArgs, withArgs)
17+import Data.Time (getCurrentTime, diffUTCTime, NominalDiffTime)
18+import System.IO (withFile, IOMode(..), hPutStrLn, Handle, stderr)
19+import Control.Concurrent (forkIO)
20+import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)
21+import qualified Network.Socket as N
22+import Debug.Trace
23+import Data.ByteString (ByteString)
24+import Data.ByteString.Char8 (pack, unpack)
25+import qualified Data.ByteString as BS
26+import qualified Network.Socket.ByteString as NBS
27+import Data.Time (getCurrentTime, diffUTCTime, NominalDiffTime)
28+
29+main :: IO ()
30+main = do
31+ [pingsStr] <- getArgs
32+ serverReady <- newEmptyMVar
33+
34+ -- Start the server
35+ forkIO $ do
36+ putStrLn "server: creating TCP connection"
37+ serverAddrs <- getAddrInfo
38+ (Just (defaultHints { addrFlags = [AI_PASSIVE] } ))
39+ Nothing
40+ (Just "8080")
41+ let serverAddr = head serverAddrs
42+ sock <- socket (addrFamily serverAddr) Stream defaultProtocol
43+ setSocketOption sock ReuseAddr 1
44+ bindSocket sock (addrAddress serverAddr)
45+
46+ putStrLn "server: awaiting client connection"
47+ putMVar serverReady ()
48+ listen sock 1
49+ (clientSock, clientAddr) <- accept sock
50+
51+ putStrLn "server: listening for pings"
52+ pong clientSock
53+
54+ -- Client
55+ takeMVar serverReady
56+ let pings = read pingsStr
57+ serverAddrs <- getAddrInfo
58+ Nothing
59+ (Just "127.0.0.1")
60+ (Just "8080")
61+ let serverAddr = head serverAddrs
62+ sock <- socket (addrFamily serverAddr) Stream defaultProtocol
63+
64+ N.connect sock (addrAddress serverAddr)
65+
66+ ping sock pings
67+
68+pingMessage :: ByteString
69+pingMessage = pack "ping123"
70+
71+ping :: Socket -> Int -> IO ()
72+ping sock pings = go pings
73+ where
74+ go :: Int -> IO ()
75+ go 0 = do
76+ putStrLn $ "client did " ++ show pings ++ " pings"
77+ go !i = do
78+ before <- getCurrentTime
79+ send sock pingMessage
80+ bs <- recv sock 8
81+ after <- getCurrentTime
82+ -- putStrLn $ "client received " ++ unpack bs
83+ let latency = (1e6 :: Double) * realToFrac (diffUTCTime after before)
84+ hPutStrLn stderr $ show i ++ " " ++ show latency
85+ go (i - 1)
86+
87+pong :: Socket -> IO ()
88+pong sock = do
89+ bs <- recv sock 8
90+ -- putStrLn $ "server received " ++ unpack bs
91+ when (BS.length bs > 0) $ do
92+ send sock bs
93+ pong sock
94+
95+-- | Wrapper around NBS.recv (for profiling)
96+recv :: Socket -> Int -> IO ByteString
97+recv sock i = do
98+ headerAndPayload <- NBS.recv sock (4 + i)
99+ return (BS.drop 4 headerAndPayload)
100+
101+-- | Wrapper around NBS.send (for profiling)
102+send :: Socket -> ByteString -> IO ()
103+send sock bs = do
104+ let length :: Int32
105+ length = fromIntegral $ BS.length bs
106+ NBS.sendMany sock [Ser.encode length, bs]
benchmarks/Makefile.JustPing+8−1
@@ -6,7 +6,8 @@ NUMPINGS=100000
6run: JustPingC.data \
6run: JustPingC.data \
7 JustPingHaskell.data \
7 JustPingHaskell.data \
8 JustPingHaskell2.data \
8 JustPingHaskell2.data \
9− JustPingNaiveHeader.data
9+ JustPingNaiveHeader.data \
10+ JustPingOneRecv.data
10 gnuplot ./JustPing.gnuplot
11 gnuplot ./JustPing.gnuplot
11
12
12JustPingC.data: JustPingC
13JustPingC.data: JustPingC
@@ -21,6 +22,9 @@ JustPingHaskell2.data: JustPingHaskell2
21JustPingNaiveHeader.data: JustPingNaiveHeader
22JustPingNaiveHeader.data: JustPingNaiveHeader
22 time ./JustPingNaiveHeader $(NUMPINGS) 2>JustPingNaiveHeader.data
23 time ./JustPingNaiveHeader $(NUMPINGS) 2>JustPingNaiveHeader.data
23
24
25+JustPingOneRecv.data: JustPingOneRecv
26+ time ./JustPingOneRecv $(NUMPINGS) 2>JustPingOneRecv.data
27+
24JustPingC: JustPingC.c
28JustPingC: JustPingC.c
25 gcc -o JustPingC -O2 -std=c99 JustPingC.c
29 gcc -o JustPingC -O2 -std=c99 JustPingC.c
26
30
@@ -33,6 +37,9 @@ JustPingHaskell2: JustPingHaskell.hs
33JustPingNaiveHeader: JustPingNaiveHeader.hs
37JustPingNaiveHeader: JustPingNaiveHeader.hs
34 ghc -o JustPingNaiveHeader -O2 -threaded JustPingNaiveHeader.hs
38 ghc -o JustPingNaiveHeader -O2 -threaded JustPingNaiveHeader.hs
35
39
40+JustPingOneRecv: JustPingOneRecv.hs
41+ ghc -o JustPingOneRecv -O2 -threaded JustPingOneRecv.hs
42+
36.PHONY: clean
43.PHONY: clean
37clean:
44clean:
38 rm -f *.data *.ps *.pdf JustPingC JustPingHaskell JustPingHaskell2 JustPingNaiveHeader
45 rm -f *.data *.ps *.pdf JustPingC JustPingHaskell JustPingHaskell2 JustPingNaiveHeader