Created
February 26, 2021 13:58
-
-
Save jmnsf/9010f2931ff70d532fdf7eb6b487fb8d to your computer and use it in GitHub Desktop.
DynamicSupervisor: always creating a child vs checking first benchmark
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
import ExUnit.Assertions | |
defmodule TestChild do | |
use GenServer | |
def start_link(child_id) do | |
GenServer.start_link(__MODULE__, child_id, name: {:via, Registry, {TestRegistry, child_id}}) | |
end | |
@impl true | |
def init(id) do | |
{:ok, id} | |
end | |
@impl true | |
def handle_call(:id, _from, id) do | |
{:reply, id, id} | |
end | |
end | |
defmodule TestSupervisor do | |
def init(_) do | |
DynamicSupervisor.init(strategy: :one_for_one) | |
end | |
def create_and_call(child_id) do | |
{:ok, pid} = start_child(child_id) | |
GenServer.call(pid, :id) | |
end | |
def test_create_call(child_id) do | |
{:ok, pid} = | |
case Registry.lookup(TestRegistry, child_id) do | |
[] -> start_child(child_id) | |
[{pid, _}] -> {:ok, pid} | |
end | |
GenServer.call(pid, :id) | |
end | |
defp start_child(child_id) do | |
child = %{ | |
id: TestChild, | |
start: {TestChild, :start_link, [child_id]}, | |
restart: :transient | |
} | |
case DynamicSupervisor.start_child(__MODULE__, child) do | |
{:error, {:already_started, pid}} -> {:ok, pid} | |
ret -> ret | |
end | |
end | |
end | |
Registry.start_link(keys: :unique, name: TestRegistry) | |
DynamicSupervisor.start_link(TestSupervisor, nil, name: TestSupervisor) | |
Benchee.run(%{ | |
"test_create_call" => fn -> | |
for id <- 60_000..100_000, | |
do: assert(TestSupervisor.test_create_call(id) == id) | |
end, | |
"create_and_call" => fn -> | |
for id <- 10_000..50_000, | |
do: assert(TestSupervisor.create_and_call(id) == id) | |
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
Operating System: Windows | |
CPU Information: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz | |
Number of Available Cores: 8 | |
Available memory: 15.92 GB | |
Elixir 1.10.4 | |
Erlang 22.1 | |
Benchmark suite executing with the following configuration: | |
warmup: 2 s | |
time: 5 s | |
memory time: 0 ns | |
parallel: 1 | |
inputs: none specified | |
Estimated total run time: 14 s | |
Benchmarking create_and_call... | |
Benchmarking test_create_call... | |
Name ips average deviation median 99th % | |
test_create_call 6.78 147.53 ms ┬▒19.84% 141.00 ms 265.00 ms | |
create_and_call 2.55 391.85 ms ┬▒24.18% 344.00 ms 609.00 ms | |
Comparison: | |
test_create_call 6.78 | |
create_and_call 2.55 - 2.66x slower +244.32 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment