Skip to content
2 changes: 1 addition & 1 deletion frameworks/Elixir/phoenix/lib/hello/world_cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ defmodule Hello.WorldCache do
world = Repo.get(World, id)
:ok = __MODULE__.put(id, world)
world

world ->
world
end
end

end
4 changes: 2 additions & 2 deletions frameworks/Elixir/phoenix/lib/hello_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ defmodule HelloWeb do
end

@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule HelloWeb.PageController do

@random_max 10_000

plug :accepts, ~w(html json) when action == :fortunes

def index(conn, _params) do
json(conn, %{"TE Benchmarks\n" => "Started"})
end
Expand All @@ -24,17 +26,11 @@ defmodule HelloWeb.PageController do
end

def queries(conn, params) do
{:ok, worlds} =
Repo.transaction(fn ->
:rand.seed(:exsp)

worlds =
Stream.repeatedly(&random_id/0)
|> Stream.uniq()
|> Stream.map(&Repo.get(World, &1))
|> Enum.take(size(params["queries"]))

worlds
worlds =
Repo.checkout(fn ->
params["queries"]
|> random_ids_sample()
|> Enum.map(&Repo.get(World, &1))
end)

json(conn, worlds)
Expand All @@ -48,54 +44,51 @@ defmodule HelloWeb.PageController do

fortunes =
[additional_fortune | Repo.all(Fortune)]
|> Enum.sort_by(& &1.message)
|> Enum.sort(fn a, b -> a.message < b.message end)

render(conn, :fortunes, fortunes: fortunes)
end

def updates(conn, params) do
{:ok, worlds} =
Repo.transaction(fn ->
:rand.seed(:exsp)

worlds =
Stream.repeatedly(&random_id/0)
|> Stream.uniq()
|> Stream.map(&Repo.get(World, &1))
|> Stream.map(fn world -> %{id: world.id, randomnumber: :rand.uniform(@random_max)} end)
|> Enum.take(size(params["queries"]))
# If this is not sorted it sometimes generates
# FAIL for http://tfb-server:8080/updates/20
# Only 20470 executed queries in the database out of roughly 20480 expected.
|> Enum.sort_by(& &1.id)

Repo.insert_all(
World,
worlds,
on_conflict: {:replace_all_except, [:id]},
conflict_target: [:id],
returning: false
)

worlds
world_updates =
Repo.checkout(fn ->
params["queries"]
|> random_ids_sample()
|> Enum.sort()
#
# If this is not sorted it will intermittently generate:
#
# FAIL for http://tfb-server:8080/updates/20
# Only 20470 executed queries in the database out of roughly 20480 expected.
#
|> Enum.map(fn id ->
world = Repo.get(World, id)
%{id: world.id, randomnumber: :rand.uniform(@random_max)}
end)
end)

json(conn, worlds)
Repo.insert_all(
World,
world_updates,
on_conflict: {:replace_all_except, [:id]},
conflict_target: [:id],
returning: false
)

json(conn, world_updates)
end

def plaintext(conn, _params) do
text(conn, "Hello, World!")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text/2 includes "charset=utf-8" in the content type and the other benchmarks seem to not do it (and this will matter on the plain text one numbers). So it probably makes sense for us to set the content-type explicitly instead:

Suggested change
text(conn, "Hello, World!")
conn
|> put_resp_header("content-type", "text/plain")
|> send_resp(200, "Hello, World!")

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. Adding that now.

end

def cached(conn, params) do
:rand.seed(:exsp)
WorldCache.seed()

worlds =
Stream.repeatedly(&random_id/0)
|> Stream.uniq()
|> Stream.map(&WorldCache.fetch(&1))
|> Enum.take(size(params["count"]))
params["count"]
|> random_ids_sample()
|> Enum.map(&WorldCache.fetch(&1))

json(conn, worlds)
end
Expand All @@ -104,11 +97,17 @@ defmodule HelloWeb.PageController do
:rand.uniform(@random_max)
end

defp size(nil), do: 1
defp size(""), do: 1
defp random_ids_sample(count) do
# Use the fastest rand algorithm
:rand.seed(:exsp)

Stream.repeatedly(&random_id/0)
|> Stream.uniq()
|> Enum.take(size(count))
end

defp size(queries) when is_bitstring(queries) do
case Integer.parse(queries) do
defp size(param_count) when is_bitstring(param_count) do
case Integer.parse(param_count) do
{count, _} -> max(1, min(500, count))
_ -> 1
end
Expand Down
1 change: 0 additions & 1 deletion frameworks/Elixir/phoenix/lib/hello_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ defmodule HelloWeb.Endpoint do
plug HelloWeb.HeadersPlug
plug HelloWeb.Router
end

6 changes: 0 additions & 6 deletions frameworks/Elixir/phoenix/lib/hello_web/router.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
defmodule HelloWeb.Router do
use HelloWeb, :router

pipeline :browser do
plug :accepts, ~w(html json)
end

scope "/", HelloWeb do
pipe_through [:browser]

get "/json", PageController, :_json
get "/db", PageController, :db
get "/queries", PageController, :queries
Expand Down
Loading