Skip to content

Instantly share code, notes, and snippets.

@wstucco
Last active January 21, 2019 22:07
Show Gist options
  • Save wstucco/0d11c78e44fe0bd058aeededf146b13b to your computer and use it in GitHub Desktop.
Save wstucco/0d11c78e44fe0bd058aeededf146b13b to your computer and use it in GitHub Desktop.
defmodule App do
def bench do
:timer.tc(fn -> start() end)
end
defp start do
run()
wait()
end
defp run do
for _ <- 1..900 do
P.start_link(self())
end
end
defp wait(900), do: :ok
defp wait(counter \\ 1) do
IO.inspect(counter)
receive do
:done ->
wait(counter + 1)
end
end
end
defmodule P do
use GenServer
def start_link(parent) do
GenServer.start_link(__MODULE__, parent)
end
def init(parent) do
send(self(), {:do_work, parent})
{:ok, []}
end
def handle_info({:do_work, parent}, state) do
Process.sleep(10)
send(parent, :done)
{:noreply, state}
end
end
{elapsed_time, _} = App.bench()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment