Skip to content

Instantly share code, notes, and snippets.

@urmastalimaa
Last active April 25, 2019 12:40
Show Gist options
  • Save urmastalimaa/bac1d0be881cf57808e7efe37438f5ee to your computer and use it in GitHub Desktop.
Save urmastalimaa/bac1d0be881cf57808e7efe37438f5ee to your computer and use it in GitHub Desktop.
Transporter channel serializer :ets cache benchmark
defmodule Transporter.Test do
@cache_table_name :channel_serializer_cache
def run(payload, %{subscribers_count: subscribers_count, use_cache: use_cache}) do
if :ets.whereis(@cache_table_name) == :undefined do
init_cache()
end
delivery_key = {:node1@somehost, System.monotonic_time()}
time(fn ->
Enum.each(0..subscribers_count, fn _ ->
if use_cache do
encode_with_cache(payload, delivery_key)
else
do_encode(payload)
end
end)
end)
end
def init_cache do
:ets.new(@cache_table_name, [
:set,
:named_table,
:public,
read_concurrency: true,
write_concurrency: true
])
end
defp time(fun) do
start_time = System.monotonic_time()
fun.()
end_time = System.monotonic_time()
System.convert_time_unit(end_time - start_time, :native, :millisecond)
end
defp encode_with_cache(payload, delivery_key) do
case :ets.lookup(@cache_table_name, delivery_key) do
[serialization] ->
serialization
[] ->
serialization = do_encode(payload)
:ets.insert(@cache_table_name, {delivery_key, serialization})
serialization
end
end
def do_encode(payload) do
message = %Phoenix.Socket.Message{
join_ref: "5",
ref: "6",
topic: "omniq:site_teams:df1baa0e-4b7d-4cf1-806a-c6446a3e0a22}",
event: "change",
payload: payload
}
Phoenix.Socket.V2.JSONSerializer.encode!(message)
end
def omniq_teams_payload(%{teams_count: teams_count}) do
%{"teams" => teams(teams_count), "default" => default_status()}
end
defp teams(count) do
Enum.map(0..count, fn _ -> random_team_status() end)
end
defp random_team_status do
%{
"team_id" => Ecto.UUID.generate(),
"status" => "open",
"media" => ["text", "phone"]
}
end
defp default_status do
%{"status" => "open", "media" => ["text", "phone"]}
end
end
@urmastalimaa
Copy link
Author

iex(2)> Test.run(Test.omniq_teams_payload(%{teams_count: 100}), %{subscribers_count: 1000, use_cache: false})
223
iex(3)> Test.run(Test.omniq_teams_payload(%{teams_count: 100}), %{subscribers_count: 1000, use_cache: false})
215
iex(4)> Test.run(Test.omniq_teams_payload(%{teams_count: 100}), %{subscribers_count: 1000, use_cache: false})
236
iex(5)> Test.run(Test.omniq_teams_payload(%{teams_count: 100}), %{subscribers_count: 1000, use_cache: true})
48
iex(6)> Test.run(Test.omniq_teams_payload(%{teams_count: 100}), %{subscribers_count: 1000, use_cache: true})
39
iex(7)> Test.run(Test.omniq_teams_payload(%{teams_count: 100}), %{subscribers_count: 1000, use_cache: true})
38
iex(8)> Test.run(Test.omniq_teams_payload(%{teams_count: 100}), %{subscribers_count: 1000, use_cache: true})
43
iex(9)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1000, use_cache: true})
3
iex(10)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1000, use_cache: true})
3
iex(11)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1000, use_cache: true})
5
iex(12)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1000, use_cache: true})
6
iex(13)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1000, use_cache: true})
7
iex(14)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1000, use_cache: true})
5
iex(15)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1000, use_cache: false})
33
iex(16)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1000, use_cache: false})
37
iex(17)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1000, use_cache: false})
35
iex(18)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1000, use_cache: false})
30
iex(19)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1, use_cache: false})
0
iex(20)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1, use_cache: false})
0
iex(21)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1, use_cache: false})
0
iex(22)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1, use_cache: true})
0
iex(23)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1, use_cache: true})
0
iex(24)> Test.run(Test.omniq_teams_payload(%{teams_count: 10}), %{subscribers_count: 1, use_cache: true})
0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment