-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve loadtest app part two (#450)
* Select loadtests target server by its public URL in workflow * Create another ets table to store the in-game websockets' clients * Add genserver that logs current clients waiting for a game and current players in match * Spawn a new client when a player finishes it match. This is to make the players in game constant * Make ArenaLoadTest release start its app only * Add LoadtestManager genserver that logs current opened websockets and also terminates the loadtest after given time * Format big int * Add LoadtestManager genserver that logs current opened websockets and also terminates the loadtest after given time * Add documentation for the modules and functions * Remove docs in callbacks * Move ets tables creation to the supervisor init
- Loading branch information
1 parent
941d9f0
commit aba6660
Showing
8 changed files
with
101 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
apps/arena_load_test/lib/arena_load_test/loadtest_manager.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule ArenaLoadTest.LoadtestManager do | ||
@moduledoc """ | ||
Genserver that interacts with the running loadtest for the user needs. | ||
""" | ||
use GenServer | ||
require Logger | ||
alias ArenaLoadTest.SocketSupervisor | ||
|
||
# API | ||
def start_link(_) do | ||
GenServer.start_link(__MODULE__, [], name: __MODULE__) | ||
end | ||
|
||
# Callbacks | ||
@impl true | ||
def init(_) do | ||
{:ok, %{}} | ||
end | ||
|
||
@impl true | ||
def handle_info(:clients_log, state) do | ||
Logger.info("Clients waiting for a game: #{:ets.info(:clients, :size)}") | ||
Logger.info("Players in match: #{:ets.info(:players, :size)}") | ||
Process.send_after(self(), :clients_log, 1_000) | ||
{:noreply, state} | ||
end | ||
|
||
@impl true | ||
def handle_info(:loadtest_finished, state) do | ||
SocketSupervisor.terminate_children() | ||
Logger.info("Loadtest finished.") | ||
{:stop, :normal, state} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 43 additions & 16 deletions
59
apps/arena_load_test/lib/arena_load_test/socket_supervisor.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,77 @@ | ||
defmodule ArenaLoadTest.SocketSupervisor do | ||
@moduledoc """ | ||
Socket Supervisor | ||
Dynamic Supervisor for the websockets connections. | ||
""" | ||
use DynamicSupervisor | ||
alias ArenaLoadTest.SocketHandler | ||
alias ArenaLoadTest.GameSocketHandler | ||
alias ArenaLoadTest.LoadtestManager | ||
require Logger | ||
|
||
def start_link(args) do | ||
DynamicSupervisor.start_link(__MODULE__, args, name: __MODULE__, max_restarts: 1) | ||
end | ||
|
||
@impl true | ||
def init(_opts) do | ||
create_ets_table(:clients) | ||
create_ets_table(:players) | ||
DynamicSupervisor.init(strategy: :one_for_one) | ||
end | ||
|
||
@doc """ | ||
Initializes a websocket that handles the client connection in the game waiting queue. | ||
""" | ||
def add_new_client(client_id) do | ||
true = :ets.insert(:clients, {client_id, client_id}) | ||
|
||
DynamicSupervisor.start_child( | ||
__MODULE__, | ||
{SocketHandler, client_id} | ||
) | ||
end | ||
|
||
@doc """ | ||
Initializes a websocket that handles the client connection in-game. | ||
""" | ||
def add_new_player(client_id, game_id) do | ||
DynamicSupervisor.start_child( | ||
__MODULE__, | ||
{GameSocketHandler, {client_id, game_id}} | ||
) | ||
end | ||
|
||
@impl true | ||
def init(_opts) do | ||
DynamicSupervisor.init(strategy: :one_for_one) | ||
end | ||
|
||
# Creates `num_clients` clients to join a game | ||
def spawn_players(num_clients) do | ||
case :ets.whereis(:clients) do | ||
:undefined -> :ets.new(:clients, [:set, :named_table, :public]) | ||
_table_exists_already -> nil | ||
end | ||
@doc """ | ||
Loadtests entrypoint. | ||
Creates given amount of clients that will join and play a game for the given duration. | ||
""" | ||
def spawn_players(num_clients, playtime_duration_ms \\ 999_999) do | ||
send(LoadtestManager, :clients_log) | ||
Process.send_after(LoadtestManager, :loadtest_finished, playtime_duration_ms) | ||
|
||
Enum.each(1..num_clients, fn client_number -> | ||
Logger.info("Iteration: #{client_number}") | ||
{:ok, _pid} = add_new_client(client_number) | ||
true = :ets.insert(:clients, {client_number, "1"}) | ||
Logger.info("Clients alive: #{:ets.info(:clients, :size)}") | ||
end) | ||
end | ||
|
||
def get_server_url("Brazil"), do: System.get_env("BRAZIL_HOST") | ||
def get_server_url("Europe"), do: System.get_env("EUROPE_HOST") | ||
@doc """ | ||
Terminates all the websocket connections. | ||
""" | ||
def terminate_children() do | ||
children = DynamicSupervisor.which_children(__MODULE__) | ||
|
||
Enum.each(children, fn {_, child_pid, _, _} -> | ||
DynamicSupervisor.terminate_child(__MODULE__, child_pid) | ||
end) | ||
end | ||
|
||
# Create a public ets table by given name. | ||
# Table is not created if it exists already. | ||
defp create_ets_table(table_name) do | ||
case :ets.whereis(table_name) do | ||
:undefined -> :ets.new(table_name, [:set, :named_table, :public]) | ||
_table_exists_already -> nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters