Skip to content

Commit 55c1436

Browse files
authored
[Elixir/phoenix] Implementing suggestions from @josevalim (#9302)
* Implementing suggestions from @josevalim * We don't need to worry about 'accepts' plug in this case * Prematurely removed accepts plug (still needed for 'fortunes' test) * Avoid checking a list we don't need to * Using explicit content-type for plain-text * Updating ecto to use pool count (also tested different count/size groupings) * db pools set for the target machine
1 parent d8c3011 commit 55c1436

File tree

7 files changed

+56
-75
lines changed

7 files changed

+56
-75
lines changed

frameworks/Elixir/phoenix/config/prod.exs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ config :hello, Hello.Repo,
2121
password: "benchmarkdbpass",
2222
database: "hello_world",
2323
hostname: "tfb-database",
24-
pool_size: 50,
24+
pool_count: 56,
25+
pool_size: 15,
2526
queue_target: 5000,
2627
log: false
2728

@@ -33,17 +34,3 @@ config :logger,
3334
],
3435
level: :error,
3536
backends: []
36-
37-
# ## SSL Support
38-
#
39-
# To get SSL working, you will need to add the `https` key
40-
# to the previous section:
41-
#
42-
# config:hello, Hello.Endpoint,
43-
# ...
44-
# https: [port: 443,
45-
# keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
46-
# certfile: System.get_env("SOME_APP_SSL_CERT_PATH")]
47-
#
48-
# Where those two env variables point to a file on
49-
# disk for the key and cert.

frameworks/Elixir/phoenix/lib/hello/world_cache.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ defmodule Hello.WorldCache do
2323
world = Repo.get(World, id)
2424
:ok = __MODULE__.put(id, world)
2525
world
26+
2627
world ->
2728
world
2829
end
2930
end
30-
3131
end

frameworks/Elixir/phoenix/lib/hello_web.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ defmodule HelloWeb do
9595
end
9696

9797
@doc """
98-
When used, dispatch to the appropriate controller/view/etc.
99-
"""
98+
When used, dispatch to the appropriate controller/view/etc.
99+
"""
100100
defmacro __using__(which) when is_atom(which) do
101101
apply(__MODULE__, which, [])
102102
end

frameworks/Elixir/phoenix/lib/hello_web/controllers/page_controller.ex

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ defmodule HelloWeb.PageController do
88

99
@random_max 10_000
1010

11+
plug :accepts, ~w(html json) when action == :fortunes
12+
1113
def index(conn, _params) do
1214
json(conn, %{"TE Benchmarks\n" => "Started"})
1315
end
@@ -24,17 +26,11 @@ defmodule HelloWeb.PageController do
2426
end
2527

2628
def queries(conn, params) do
27-
{:ok, worlds} =
28-
Repo.transaction(fn ->
29-
:rand.seed(:exsp)
30-
31-
worlds =
32-
Stream.repeatedly(&random_id/0)
33-
|> Stream.uniq()
34-
|> Stream.map(&Repo.get(World, &1))
35-
|> Enum.take(size(params["queries"]))
36-
37-
worlds
29+
worlds =
30+
Repo.checkout(fn ->
31+
params["queries"]
32+
|> random_ids_sample()
33+
|> Enum.map(&Repo.get(World, &1))
3834
end)
3935

4036
json(conn, worlds)
@@ -48,54 +44,53 @@ defmodule HelloWeb.PageController do
4844

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

5349
render(conn, :fortunes, fortunes: fortunes)
5450
end
5551

5652
def updates(conn, params) do
57-
{:ok, worlds} =
58-
Repo.transaction(fn ->
59-
:rand.seed(:exsp)
60-
61-
worlds =
62-
Stream.repeatedly(&random_id/0)
63-
|> Stream.uniq()
64-
|> Stream.map(&Repo.get(World, &1))
65-
|> Stream.map(fn world -> %{id: world.id, randomnumber: :rand.uniform(@random_max)} end)
66-
|> Enum.take(size(params["queries"]))
67-
# If this is not sorted it sometimes generates
68-
# FAIL for http://tfb-server:8080/updates/20
69-
# Only 20470 executed queries in the database out of roughly 20480 expected.
70-
|> Enum.sort_by(& &1.id)
71-
72-
Repo.insert_all(
73-
World,
74-
worlds,
75-
on_conflict: {:replace_all_except, [:id]},
76-
conflict_target: [:id],
77-
returning: false
78-
)
79-
80-
worlds
53+
world_updates =
54+
Repo.checkout(fn ->
55+
params["queries"]
56+
|> random_ids_sample()
57+
|> Enum.sort()
58+
#
59+
# If this is not sorted it will intermittently generate:
60+
#
61+
# FAIL for http://tfb-server:8080/updates/20
62+
# Only 20470 executed queries in the database out of roughly 20480 expected.
63+
#
64+
|> Enum.map(fn id ->
65+
world = Repo.get(World, id)
66+
%{id: world.id, randomnumber: :rand.uniform(@random_max)}
67+
end)
8168
end)
8269

83-
json(conn, worlds)
70+
Repo.insert_all(
71+
World,
72+
world_updates,
73+
on_conflict: {:replace_all_except, [:id]},
74+
conflict_target: [:id],
75+
returning: false
76+
)
77+
78+
json(conn, world_updates)
8479
end
8580

86-
def plaintext(conn, _params) do
87-
text(conn, "Hello, World!")
81+
def plaintext(conn, _params) do
82+
conn
83+
|> put_resp_header("content-type", "text/plain")
84+
|> send_resp(200, "Hello, World!")
8885
end
8986

9087
def cached(conn, params) do
91-
:rand.seed(:exsp)
9288
WorldCache.seed()
9389

9490
worlds =
95-
Stream.repeatedly(&random_id/0)
96-
|> Stream.uniq()
97-
|> Stream.map(&WorldCache.fetch(&1))
98-
|> Enum.take(size(params["count"]))
91+
params["count"]
92+
|> random_ids_sample()
93+
|> Enum.map(&WorldCache.fetch(&1))
9994

10095
json(conn, worlds)
10196
end
@@ -104,11 +99,17 @@ defmodule HelloWeb.PageController do
10499
:rand.uniform(@random_max)
105100
end
106101

107-
defp size(nil), do: 1
108-
defp size(""), do: 1
102+
defp random_ids_sample(count) do
103+
# Use the fastest rand algorithm
104+
:rand.seed(:exsp)
105+
106+
Stream.repeatedly(&random_id/0)
107+
|> Stream.uniq()
108+
|> Enum.take(size(count))
109+
end
109110

110-
defp size(queries) when is_bitstring(queries) do
111-
case Integer.parse(queries) do
111+
defp size(param_count) when is_bitstring(param_count) do
112+
case Integer.parse(param_count) do
112113
{count, _} -> max(1, min(500, count))
113114
_ -> 1
114115
end

frameworks/Elixir/phoenix/lib/hello_web/endpoint.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ defmodule HelloWeb.Endpoint do
2020
plug HelloWeb.HeadersPlug
2121
plug HelloWeb.Router
2222
end
23-

frameworks/Elixir/phoenix/lib/hello_web/router.ex

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
defmodule HelloWeb.Router do
22
use HelloWeb, :router
33

4-
pipeline :browser do
5-
plug :accepts, ~w(html json)
6-
end
7-
84
scope "/", HelloWeb do
9-
pipe_through [:browser]
10-
115
get "/json", PageController, :_json
126
get "/db", PageController, :db
137
get "/queries", PageController, :queries

frameworks/Elixir/phoenix/mix.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"cowlib": {:hex, :cowlib, "2.13.0", "db8f7505d8332d98ef50a3ef34b34c1afddec7506e4ee4dd4a3a266285d282ca", [:make, :rebar3], [], "hexpm", "e1e1284dc3fc030a64b1ad0d8382ae7e99da46c3246b815318a4b848873800a4"},
77
"db_connection": {:hex, :db_connection, "2.7.0", "b99faa9291bb09892c7da373bb82cba59aefa9b36300f6145c5f201c7adf48ec", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "dcf08f31b2701f857dfc787fbad78223d61a32204f217f15e881dd93e4bdd3ff"},
88
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
9-
"ecto": {:hex, :ecto, "3.11.2", "e1d26be989db350a633667c5cda9c3d115ae779b66da567c68c80cfb26a8c9ee", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3c38bca2c6f8d8023f2145326cc8a80100c3ffe4dcbd9842ff867f7fc6156c65"},
10-
"ecto_sql": {:hex, :ecto_sql, "3.11.3", "4eb7348ff8101fbc4e6bbc5a4404a24fecbe73a3372d16569526b0cf34ebc195", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.11.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e5f36e3d736b99c7fee3e631333b8394ade4bafe9d96d35669fca2d81c2be928"},
9+
"ecto": {:hex, :ecto, "3.12.4", "267c94d9f2969e6acc4dd5e3e3af5b05cdae89a4d549925f3008b2b7eb0b93c3", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ef04e4101688a67d061e1b10d7bc1fbf00d1d13c17eef08b71d070ff9188f747"},
10+
"ecto_sql": {:hex, :ecto_sql, "3.12.1", "c0d0d60e85d9ff4631f12bafa454bc392ce8b9ec83531a412c12a0d415a3a4d0", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.12", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "aff5b958a899762c5f09028c847569f7dfb9cc9d63bdb8133bff8a5546de6bf5"},
1111
"expo": {:hex, :expo, "1.0.0", "647639267e088717232f4d4451526e7a9de31a3402af7fcbda09b27e9a10395a", [:mix], [], "hexpm", "18d2093d344d97678e8a331ca0391e85d29816f9664a25653fd7e6166827827c"},
1212
"gettext": {:hex, :gettext, "0.25.0", "98a95a862a94e2d55d24520dd79256a15c87ea75b49673a2e2f206e6ebc42e5d", [:mix], [{:expo, "~> 0.5.1 or ~> 1.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "38e5d754e66af37980a94fb93bb20dcde1d2361f664b0a19f01e87296634051f"},
1313
"hpax": {:hex, :hpax, "1.0.0", "28dcf54509fe2152a3d040e4e3df5b265dcb6cb532029ecbacf4ce52caea3fd2", [:mix], [], "hexpm", "7f1314731d711e2ca5fdc7fd361296593fc2542570b3105595bb0bc6d0fad601"},

0 commit comments

Comments
 (0)