Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
-module(bm_ets).
-export([run/0]).
-include_lib("stdlib/include/ms_transform.hrl").
run() ->
ETS = fill_ets(),
List = fill_list(),
io:format("Initialized, starting~n"),
T1 = erlang:now(),
run_ets(ETS, 5000, false),
T2 = erlang:now(),
run_list(List, 5000, false),
T3 = erlang:now(),
run_ets(ETS, 5000, true),
T4 = erlang:now(),
run_list(List, 5000, true),
T5 = erlang:now(),
io:format("[RO] ETS: ~p, List: ~p~n", [timer:now_diff(T2,T1), timer:now_diff(T3,T2)]),
io:format("[RW] ETS: ~p, List: ~p~n", [timer:now_diff(T4,T3), timer:now_diff(T5,T4)]),
ok.
fill_ets() ->
ETS = ets:new(bm_ets, [set]),
[ets:insert(ETS, Entry) || Entry <- fill_list()],
ETS.
fill_list() ->
[{Num, random_state(), make_ref()} || Num <- lists:seq(1,5000) ].
random_state() ->
Rand = random:uniform(100),
if
Rand > 67 -> active;
Rand > 33 -> starting;
true -> paused
end.
run_ets(_, 0, _) -> ok;
run_ets(ETS, Num, RW) ->
State = active,
MS = ets:fun2ms(fun({Id, S, Pid}) when S == State -> {Id, Pid} end),
List = ets:select(ETS, MS),
[Pid || {_Id,Pid} <- List],
case RW of
true -> ets:update_element(ETS, random:uniform(5000), {2, random_state()});
_ -> ok
end,
run_ets(ETS, Num-1, RW).
run_list(_, 0, _) -> ok;
run_list(Clients, Num, RW) ->
State = active,
[Pid || {_Id, S, Pid} <- Clients, S == State],
Key = random:uniform(5000),
Clients1 = case RW of
true ->
case lists:keyfind(Key, 1, Clients) of
false -> Clients;
Tuple ->
lists:keystore(Key, 1, Clients, setelement(2, Tuple, random_state()))
end;
_ -> Clients
end,
run_list(Clients1, Num-1, RW).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.