Skip to content

Instantly share code, notes, and snippets.

@ryo33
Created June 7, 2020 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryo33/1206cc344fb1b67deaf7cf4beeb03747 to your computer and use it in GitHub Desktop.
Save ryo33/1206cc344fb1b67deaf7cf4beeb03747 to your computer and use it in GitHub Desktop.
Performance research for re-implementing Cizen.Dispatcher
defmodule Server do
use GenServer
def init(pid) do
{:ok, pid}
end
def handle_info({:DOWN, _ref, :process, _object, _reason}, state) do
{:stop, :normal, state}
end
end
{time, _} =
:timer.tc(fn ->
1..100_000
|> Enum.reduce({:ok, self()}, fn _, {:ok, pid} ->
GenServer.start(Server, pid)
end)
end)
IO.puts("#{time / 1000 / 100_000 * 1000} ms/1000*GenServer.start_link")
:timer.sleep(1000)
pid =
spawn(fn ->
:timer.sleep(10000)
end)
{time, _} =
:timer.tc(fn ->
1..100_000
|> Enum.reduce(:dummy, fn _, _ -> send(pid, :aaa) end)
end)
Process.exit(pid, :kill)
IO.puts("#{time / 1000 / 100_000 * 1000} ms/1000*send")
defmodule Chain do
use GenServer
def init(parent) do
Process.monitor(parent)
{:ok, parent}
end
def handle_info({:DOWN, _ref, :process, _object, _reason}, state) do
{:stop, :normal, state}
end
end
root =
spawn(fn ->
receive do
_ -> :ok
end
end)
{time, last} =
:timer.tc(fn ->
{:ok, pid} =
1..100_000
|> Enum.reduce({:ok, root}, fn _, {:ok, parent} ->
GenServer.start(Chain, parent)
end)
pid
end)
IO.puts("#{time / 1000 / 100_000 * 1000} ms/start 1000 Chain")
{time, _} =
:timer.tc(fn ->
Process.exit(root, :kill)
Process.monitor(last)
receive do
{:DOWN, _, :process, _, _} ->
:ok
end
end)
IO.puts("#{time / 1000 / 100_000 * 1000} ms/1000*exit 1000 Chain")
pid =
spawn(fn ->
receive do
_ -> :ok
end
end)
{time, _} =
:timer.tc(fn ->
1..100_000
|> Enum.reduce(:dummy, fn _, _ -> Process.monitor(pid) end)
end)
IO.puts("#{time / 1000 / 100_000 * 1000} ms/1000*Process.monitor")
defmodule Chain2 do
use GenServer
def init(child) do
{:ok, child}
end
def handle_cast(:exit, child) do
if child, do: GenServer.cast(child, :exit)
{:stop, :normal, child}
end
end
{:ok, last} = GenServer.start(Chain2, nil)
{time, root} =
:timer.tc(fn ->
{:ok, pid} =
1..100_000
|> Enum.reduce({:ok, last}, fn _, {:ok, child} ->
GenServer.start(Chain2, child)
end)
pid
end)
IO.puts("#{time / 1000 / 100_000 * 1000} ms/start 1000 Chain2")
{time, _} =
:timer.tc(fn ->
GenServer.cast(root, :exit)
Process.monitor(last)
receive do
{:DOWN, _, :process, _, _} ->
:ok
end
end)
IO.puts("#{time / 1000 / 100_000 * 1000} ms/1000*exit 1000 Chain2")
3.22 ms/1000*GenServer.start_link
0.51 ms/1000*send
4.59 ms/start 1000 Chain
2.39 ms/1000*exit 1000 Chain
0.49 ms/1000*Process.monitor
4.33 ms/start 1000 Chain2
2.56 ms/1000*exit 1000 Chain2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment