Skip to content
edgurgel edited this page Sep 20, 2014 · 1 revision

Here's a really simple and naive benchmark for Poxa:

defmodule Worker do
  use GenServer

  def start_link(parent) do
    GenServer.start_link(__MODULE__, parent)
  end

  def init(parent) do
    send self, :start
    {:ok, %{parent: parent, client: nil}}
  end

  def handle_info(:start, state) do
    {:ok, pid} = PusherClient.start_link("ws://localhost:8080", "app_key", "secret", stream_to: self)
    {:noreply, %{state | client: pid}}
  end

  def handle_info(%{event: "pusher:connection_established"}, state = %{client: pid}) do
    PusherClient.subscribe!(pid, "channel")
    {:noreply, state}
  end

  def handle_info(%{event: "pusher:subscription_succeeded"}, state = %{parent: parent}) do
    send parent, :subscribed
    {:noreply, state}
  end

  def handle_info(%{event: "bench_event"}, state = %{parent: parent}) do
    send parent, :ok
    {:noreply, state}
  end

  def terminate(reason, state) do
    IO.puts "Terminated"
    :ok
  end
end

arg = System.argv |> List.first
n = 100
if arg do
  {n, _} = Integer.parse(arg)
end
IO.puts "Running with n = #{n}"
Application.ensure_all_started(:pusher)
Application.ensure_all_started(:logger)

Pusher.configure!("localhost", 8080, "app_id", "app_key", "secret")
parent = self
t1 = :erlang.now
for _i <- (0..n) do
  {:ok, pid} = Worker.start_link(parent)
  pid
end

for _i <- (0..n) do
  receive do
    :subscribed -> :ok
  end
end

t2 = :erlang.now
IO.write "#{n} process connected and subscribed. "
IO.puts "It took #{:timer.now_diff(t2, t1)/1000} milliseconds"

for _i <- (0..10), do: Pusher.trigger("bench_event", %{}, "channel")

for _i <- (0..n*10) do
  receive do
    :ok -> :ok
  end
end
t3 = :erlang.now
IO.write "#{10*n} Events received. "
IO.puts "It took #{:timer.now_diff(t3, t2)/1000} milliseconds"

You can run using the following command on Poxa repo:

MIX_ENV=test mix run --no-start connect.exs 200

Where N (in this case 200) is the number of spawned processes.