[erlang-questions] Benchmarking Erlang: Deathmatch of gb_trees, dict, ets, mnesia ... and registered names

Joel Reymont <>
Thu Oct 9 13:12:22 CEST 2008


%%% mnesia disc_only_copies

-module(map5).
-behaviour(gen_server).

-export([init/1, handle_call/3, handle_cast/2,
	 handle_info/2, terminate/2, code_change/3]).

-export([start/0, test/1, populate/2, lookup/2]).

-record(xref, {
           key,
           val
          }).

start() ->
     gen_server:start(map5, [], []).

init([]) ->
     {ok, none}.

terminate(_, _) ->
     ok.

handle_cast({'ADD', Key, Val}, Data) ->
     F = fun() -> mnesia:write(#xref{ key = Key, val = Val}) end,
     mnesia:transaction(F),
     {noreply, Data};

handle_cast(stop, Data) ->
     {stop, normal, Data}.

handle_call({'LOOKUP', Key}, _From, Data) ->
     F = fun() -> mnesia:read({xref, Key}) end,
     {atomic, [Value]} = mnesia:transaction(F),
     {reply, Value, Data}.

handle_info(Info, Data) ->
     error_logger:info_report([{module, ?MODULE},
			      {line, ?LINE},
			      {self, self()},
			      {message, Info}]),
     {noreply, Data}.

code_change(_, Data, _) ->
     {ok, Data}.

test(N) ->
     mnesia:stop(),
     mnesia:delete_schema([node()]),
     mnesia:create_schema([node()]),
     mnesia:start(),
     {atomic, ok} =
         mnesia:create_table(xref,
                             [
                              {disc_only_copies, [node()]},
                              {type, set},
                              {attributes, record_info(fields, xref)}
                             ]),
     {ok, Pid} = start(),
     {T1, _} = timer:tc(map5, populate, [Pid, N]),
     {T2, _} = timer:tc(map5, lookup, [Pid, N]),
     io:format("Populate: ~.4. f~n", [T1 / 1000000]),
     io:format("Lookup:   ~.4. f~n", [T2 / 1000000]),
     gen_server:cast(Pid, stop).

populate(_, 0) ->
     ok;

populate(Pid, N) ->
     gen_server:cast(Pid, {'ADD', N, N}),
     populate(Pid, N - 1).

lookup(_, 0) ->
     ok;

lookup(Pid, N) ->
     gen_server:call(Pid, {'LOOKUP', N}),
     lookup(Pid, N - 1).





More information about the erlang-questions mailing list