public
Last active

  • Download Gist
bm_ets_list.erl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
-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).

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.