git.iko.soy/distributed-process
on master help sign in
Erlang and remote versions of the benchmarks
commita7c5cb167388c295ebee3b976f19d8ee54732f20 authorEdsko de Vries <edsko@well-typed.com> dateThu 20 Sep 2012, 11:25 (14 years ago) parent4ef07cb
4 files changed+140
distributed-process/benchmarks/erlang/latency.erl+35 distributed-process/benchmarks/erlang/throughput.erl+34 distributed-process/benchmarks/remote/Latency.hs+32 distributed-process/benchmarks/remote/Throughput.hs+39
distributed-process/benchmarks/erlang/latency.erlnew file+35
@@ -0,0 +1,35 @@
1+-module(latency).
2+
3+-export([server/0, client/2, start_server/0, start_client/2]).
4+
5+forever(What) ->
6+ What(),
7+ forever(What).
8+
9+replicate(0, _) ->
10+ ok;
11+replicate(N, What) ->
12+ What(),
13+ replicate(N - 1, What).
14+
15+server() ->
16+ forever(fun () ->
17+ receive
18+ Them -> Them ! pong
19+ end
20+ end).
21+
22+client(N, Them) ->
23+ replicate(N, fun() ->
24+ {pingServer, Them} ! self(),
25+ receive
26+ pong -> ok
27+ end
28+ end),
29+ io:format("Did ~B pings", [N]).
30+
31+start_server() ->
32+ register(pingServer, spawn(latency, server, [])).
33+
34+start_client(N, Them) ->
35+ client(N, Them).
distributed-process/benchmarks/erlang/throughput.erlnew file+34
@@ -0,0 +1,34 @@
1+-module(throughput).
2+
3+-export([start_counter/0, start_count/2, counter/0, count/2]).
4+
5+counter() ->
6+ counter(0).
7+
8+counter(N) ->
9+ receive
10+ nothing ->
11+ counter(N + 1);
12+ {just, Them} ->
13+ Them ! N,
14+ counter(0)
15+ end.
16+
17+replicate(0, _) ->
18+ ok;
19+replicate(N, What) ->
20+ What(),
21+ replicate(N - 1, What).
22+
23+count(N, Them) ->
24+ replicate(N, fun() -> {counter, Them} ! nothing end),
25+ {counter, Them} ! {just, self()},
26+ receive
27+ N -> io:format("Did ~B pings", [N])
28+ end.
29+
30+start_counter() ->
31+ register(counter, spawn(throughput, counter, [])).
32+
33+start_count(N, Them) ->
34+ count(N, Them).
distributed-process/benchmarks/remote/Latency.hsnew file+32
@@ -0,0 +1,32 @@
1+{-# LANGUAGE BangPatterns, TemplateHaskell #-}
2+
3+import Control.Monad
4+import Control.Applicative
5+import Control.Monad.IO.Class (liftIO)
6+import qualified Data.ByteString.Lazy as BSL
7+import Data.Binary (encode, decode)
8+import Remote
9+
10+pingServer :: ProcessM ()
11+pingServer = forever $ do
12+ them <- expect
13+ send them ()
14+
15+pingClient :: Int -> ProcessId -> ProcessM ()
16+pingClient n them = do
17+ us <- getSelfPid
18+ replicateM_ n $ send them us >> (expect :: ProcessM ())
19+ liftIO . putStrLn $ "Did " ++ show n ++ " pings"
20+
21+initialProcess :: String -> ProcessM ()
22+initialProcess "SERVER" = do
23+ us <- getSelfPid
24+ liftIO $ BSL.writeFile "pingServer.pid" (encode us)
25+ pingServer
26+initialProcess "CLIENT" = do
27+ n <- liftIO $ getLine
28+ them <- liftIO $ decode <$> BSL.readFile "pingServer.pid"
29+ pingClient (read n) them
30+
31+main :: IO ()
32+main = remoteInit (Just "config") [] initialProcess
distributed-process/benchmarks/remote/Throughput.hsnew file+39
@@ -0,0 +1,39 @@
1+{-# LANGUAGE BangPatterns, TemplateHaskell #-}
2+
3+import Control.Monad
4+import Control.Applicative
5+import Control.Monad.IO.Class (liftIO)
6+import qualified Data.ByteString.Lazy as BSL
7+import Data.Binary (encode, decode)
8+import Remote
9+
10+counter :: ProcessM ()
11+counter = go 0
12+ where
13+ go :: Int -> ProcessM ()
14+ go !n = do
15+ b <- expect
16+ case b of
17+ Nothing -> go (n + 1)
18+ Just them -> send them n >> go 0
19+
20+count :: Int -> ProcessId -> ProcessM ()
21+count n them = do
22+ us <- getSelfPid
23+ replicateM_ n $ send them (Nothing :: Maybe ProcessId)
24+ send them (Just us)
25+ n' <- expect
26+ liftIO $ print (n == n')
27+
28+initialProcess :: String -> ProcessM ()
29+initialProcess "SERVER" = do
30+ us <- getSelfPid
31+ liftIO $ BSL.writeFile "counter.pid" (encode us)
32+ counter
33+initialProcess "CLIENT" = do
34+ n <- liftIO $ getLine
35+ them <- liftIO $ decode <$> BSL.readFile "counter.pid"
36+ count (read n) them
37+
38+main :: IO ()
39+main = remoteInit (Just "config") [] initialProcess