How fast can Erlang send messages?

(This page is a mirrored copy of an article originally posted on the LShift blog; see the archive index here.)

My previous post examined Erlang’s speed of process setup and teardown. Here I’m looking at how quickly messages can be sent and received within a single Erlang node. Roughly speaking, I’m seeing 3.4 million deliveries per second one-way, and 1.4 million roundtrips per second (2.8 million deliveries per second) in a ping-pong setup in the same environment as previously - a 2.8GHz Pentium 4 with 1MB cache.

Here’s the code I’m using - time_diff and dotimes aren’t shown, because they’re the same as the code in the previous post:

-module(ipctest).
-export([oneway/0, consumer/0, pingpong/0]).

oneway() ->
    N = 10000000,
    Pid = spawn(ipctest, consumer, []),
    Start = erlang:now(),
    dotimes(N - 1, fun () -> Pid ! message end),
    Pid ! {done, self()},
    receive ok -> ok end,
    Stop = erlang:now(),
    N / time_diff(Start, Stop).

pingpong() ->
    N = 10000000,
    Pid = spawn(ipctest, consumer, []),
    Start = erlang:now(),
    Message = {ping, self()},
    dotimes(N, fun () ->
               Pid ! Message,
               receive pong -> ok end
           end),
    Stop = erlang:now(),
    N / time_diff(Start, Stop).

consumer() ->
    receive
    message -> consumer();
    {done, Pid} -> Pid ! ok;
    {ping, Pid} ->
        Pid ! pong,
        consumer()
    end.

%% code omitted - see previous post

Comments

On 11 September, 2006 at 8:53 am, matthew wrote:

funs are more expensive to invoke than normal module methods. Can you try this without using dotimes - make the fun a normal recursive method?

On 11 September, 2006 at 11:17 pm, tonyg wrote:

Removing dotimes for oneway produced a speed boost of about 10% - but doing the same for pingpong scarcely affected the speed at all. If anything, it ran slightly slower. It’s possible that the extra arguments to the helper procedure (Count, Pid, Message) for the non-dotimes pingpong costs more than the overhead of using a fun for the dotimes pingpong. The fun can close over (Pid, Message) once, avoiding the need to pass the parameters around.

On 8 April, 2009 at 9:06 am, Roberto wrote:

different benchmark approach than yours, but would like to hear your opinion on this, if you feel so:

http://www.ostinelli.net/boost-message-passing-between-erlang-nodes/

r.

On 24 April, 2009 at 10:09 pm, tonyg wrote:

Roberto, thank you — that is very interesting.

On 19 September, 2009 at 8:57 am, testing wrote:

per second or per minute??