Skip to content

Commit

Permalink
Optimise starting many users immediately
Browse files Browse the repository at this point in the history
Have the user supervisors loop over the users start
instead of the controller.
  • Loading branch information
NelsonVides committed Jan 15, 2024
1 parent d39a1c4 commit 9df2ea3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/amoc_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,12 @@ handle_start_user(#state{create_users = [], tref = TRef} = State) ->
State#state{tref = maybe_stop_timer(TRef)}.

-spec handle_start_all_users(state()) -> state().
handle_start_all_users(#state{create_users = []} = State) ->
handle_start_user(State);
handle_start_all_users(State) ->
NewState = handle_start_user(State),
handle_start_all_users(NewState).
handle_start_all_users(#state{create_users = AllUsers,
scenario = Scenario,
scenario_state = ScenarioState,
tref = TRef} = State) ->
amoc_users_sup_sup:start_children(Scenario, AllUsers, ScenarioState),
State#state{create_users = [], tref = maybe_stop_timer(TRef)}.

%% ------------------------------------------------------------------
%% helpers
Expand Down
10 changes: 9 additions & 1 deletion src/users/amoc_users_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ handle_cast({start_child, Scenario, Id, ScenarioState}, #state{tid = Tid} = Stat
case amoc_user:start_link(Scenario, Id, ScenarioState) of
{ok, Pid} ->
handle_up_user(Tid, Pid, Id),
{noreply, State#state{}};
{noreply, State};
_ ->
{noreply, State}
end;
handle_cast({start_children, Scenario, Ids, ScenarioState}, #state{tid = Tid} = State) ->
[ case amoc_user:start_link(Scenario, Id, ScenarioState) of
{ok, Pid} ->
handle_up_user(Tid, Pid, Id);
_ ->
ok
end || Id <- Ids],
{noreply, State};
handle_cast({stop_child, ForceRemove}, #state{tid = Tid} = State) ->
Pids = case ets:match_object(Tid, '$1', 1) of
{[{Pid, _Id}], _} -> [Pid];
Expand Down
13 changes: 12 additions & 1 deletion src/users/amoc_users_sup_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

%% API
-export([init_storage/0, incr_no_of_users/0, decr_no_of_users/0, count_no_of_users/0,
start_child/3, stop_children/2, terminate_all_children/0]).
start_child/3, start_children/3, stop_children/2, terminate_all_children/0]).

-record(storage, {
user_count :: atomics:atomics_ref(),
Expand Down Expand Up @@ -66,6 +66,17 @@ start_child(Scenario, Id, ScenarioState) ->
Sup = get_sup_for_user_id(Id),
gen_server:cast(Sup, {start_child, Scenario, Id, ScenarioState}).

-spec start_children(amoc:scenario(), [amoc_scenario:user_id()], any()) -> ok.
start_children(Scenario, UserIds, ScenarioState) ->
KeyFun = fun(UserId) ->
get_sup_for_user_id(UserId)
end,
Assignments = maps:groups_from_list(KeyFun, UserIds),
CastFun = fun (Sup, Users) ->
gen_server:cast(Sup, {start_children, Scenario, Users, ScenarioState})
end,
maps:foreach(CastFun, Assignments).

-spec stop_children(non_neg_integer(), boolean()) -> non_neg_integer().
stop_children(Count, Force) ->
TotalCount = count_no_of_users(),
Expand Down

0 comments on commit 9df2ea3

Please sign in to comment.