Skip to content

Commit 1b2f23a

Browse files
authored
[elixir] Version and code updates (#9198)
* Updates for Phoenix, Elixir, Erlang, and Docker image * Missed on mix file and template should be heex * Add back fortunes with heex * Update plug libraries * Code based performance improvements
1 parent 037639e commit 1b2f23a

File tree

17 files changed

+123
-207
lines changed

17 files changed

+123
-207
lines changed

frameworks/Elixir/phoenix/benchmark_config.json

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,6 @@
22
"framework": "phoenix",
33
"tests": [{
44
"default": {
5-
"json_url": "/json",
6-
"db_url": "/db",
7-
"query_url": "/queries?queries=",
8-
"fortune_url": "/fortunes",
9-
"plaintext_url": "/plaintext",
10-
"update_url": "/updates?queries=",
11-
"cached_query_url": "/cached-queries?count=",
12-
"port": 8080,
13-
"approach": "Realistic",
14-
"classification": "Fullstack",
15-
"database": "Postgres",
16-
"framework": "Phoenix",
17-
"language": "Elixir",
18-
"flavor": "None",
19-
"orm": "full",
20-
"platform": "beam",
21-
"webserver": "cowboy",
22-
"os": "Linux",
23-
"database_os": "Linux",
24-
"display_name": "Phoenix",
25-
"notes": "",
26-
"versus": ""
27-
},
28-
"bandit": {
295
"json_url": "/json",
306
"db_url": "/db",
317
"query_url": "/queries?queries=",
@@ -45,9 +21,8 @@
4521
"webserver": "bandit",
4622
"os": "Linux",
4723
"database_os": "Linux",
48-
"display_name": "Phoenix-Bandit",
49-
"notes": "",
50-
"versus": "default"
24+
"display_name": "Phoenix",
25+
"notes": ""
5126
}
5227
}]
5328
}

frameworks/Elixir/phoenix/config/bandit.exs

Lines changed: 0 additions & 43 deletions
This file was deleted.

frameworks/Elixir/phoenix/config/config.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ config :hello, HelloWeb.Endpoint,
1818
debug_errors: false,
1919
secret_key_base: "Z18ZjzZslFpKd8HB41IljqMavPiOKVF9y1DIQ+S2Ytg7Op0EIauwJgd7mtRStssx"
2020

21+
# Configure cache for world entities
22+
config :hello, Hello.WorldCache,
23+
gc_interval: :timer.hours(1),
24+
max_size: 1_000_000,
25+
allocated_memory: 100_000_000,
26+
gc_cleanup_min_timeout: :timer.seconds(30),
27+
gc_cleanup_max_timeout: :timer.minutes(30)
28+
2129
# Configures Elixir's Logger
2230
config :logger, :console,
2331
format: "$time $metadata[$level] $message\n",

frameworks/Elixir/phoenix/config/prod.exs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import Config
22

33
config :hello, HelloWeb.Endpoint,
4-
url: [host: "0.0.0.0"],
5-
http: [port: 8080, protocol_options: [max_keepalive: :infinity], backlog: 8096],
6-
cache_static_lookup: false,
4+
adapter: Bandit.PhoenixAdapter,
5+
http: [port: 8080, ip: {0, 0, 0, 0}],
6+
http_options: [log_protocol_errors: false],
7+
compress: false,
78
check_origin: false,
89
debug_errors: false,
910
code_reloader: false,
@@ -14,7 +15,7 @@ config :hello, Hello.Repo,
1415
password: "benchmarkdbpass",
1516
database: "hello_world",
1617
hostname: "tfb-database",
17-
pool_size: 40,
18+
pool_size: 50,
1819
queue_target: 5000,
1920
log: false
2021

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule Hello.Application do
66
def start(_type, _args) do
77
children = [
88
Hello.Repo,
9-
{Hello.Cache, []},
9+
{Hello.WorldCache, []},
1010
HelloWeb.Endpoint
1111
]
1212

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

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
defmodule Hello.WorldCache do
2+
use Nebulex.Cache,
3+
otp_app: :hello,
4+
adapter: Nebulex.Adapters.Local
5+
6+
alias Hello.Models.World
7+
alias Hello.Repo
8+
9+
def seed do
10+
if not __MODULE__.has_key?(:seeded) do
11+
World
12+
|> Repo.all()
13+
|> Enum.into([], &{&1.id, &1})
14+
|> __MODULE__.put_all()
15+
16+
__MODULE__.put(:seeded, true)
17+
end
18+
end
19+
20+
def fetch(id) do
21+
case __MODULE__.get(id) do
22+
nil ->
23+
world = Repo.get(World, id)
24+
:ok = __MODULE__.put(id, world)
25+
world
26+
world ->
27+
world
28+
end
29+
end
30+
31+
end

frameworks/Elixir/phoenix/lib/hello_web.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ defmodule HelloWeb do
6262
defp html_helpers do
6363
quote do
6464
# Use all HTML functionality (forms, tags, etc)
65-
use Phoenix.HTML
66-
# Core UI Components and translation
67-
import HelloWeb.Gettext
65+
import Phoenix.HTML
6866

6967
# Routes generation with the ~p sigil
7068
unquote(verified_routes())

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

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
defmodule HelloWeb.PageController do
2-
alias Hello.Models.{Fortune, World}
32

43
use HelloWeb, :controller
54

5+
alias Hello.Models.Fortune
6+
alias Hello.Models.World
67
alias Hello.Repo
7-
alias Hello.Cache
8+
alias Hello.WorldCache
89

910
@random_max 10_000
1011

@@ -29,7 +30,7 @@ defmodule HelloWeb.PageController do
2930
worlds =
3031
Stream.repeatedly(&random_id/0)
3132
|> Stream.uniq()
32-
|> Stream.map(fn idx -> Repo.get(World, idx) end)
33+
|> Stream.map(&Repo.get(World, &1))
3334
|> Enum.take(size(params["queries"]))
3435

3536
json(conn, worlds)
@@ -41,11 +42,11 @@ defmodule HelloWeb.PageController do
4142
message: "Additional fortune added at request time."
4243
}
4344

44-
fortunes = [additional_fortune | Repo.all(Fortune)]
45+
fortunes =
46+
[additional_fortune | Repo.all(Fortune)]
47+
|> Enum.sort_by(& &1.message)
4548

46-
render(conn, :fortunes,
47-
fortunes: Enum.sort(fortunes, fn f1, f2 -> f1.message < f2.message end)
48-
)
49+
render(conn, :fortunes, fortunes: fortunes)
4950
end
5051

5152
def updates(conn, params) do
@@ -54,9 +55,13 @@ defmodule HelloWeb.PageController do
5455
worlds =
5556
Stream.repeatedly(&random_id/0)
5657
|> Stream.uniq()
57-
|> Stream.map(fn idx -> Repo.get(World, idx) end)
58+
|> Stream.map(&Repo.get(World, &1))
5859
|> Stream.map(fn world -> %{id: world.id, randomnumber: :rand.uniform(@random_max)} end)
5960
|> Enum.take(size(params["queries"]))
61+
# If this is not sorted it sometimes generates
62+
# FAIL for http://tfb-server:8080/updates/20
63+
# Only 20470 executed queries in the database out of roughly 20480 expected.
64+
|> Enum.sort_by(& &1.id)
6065

6166
Repo.insert_all(
6267
World,
@@ -75,28 +80,17 @@ defmodule HelloWeb.PageController do
7580

7681
def cached(conn, params) do
7782
:rand.seed(:exsp)
83+
WorldCache.seed()
7884

7985
worlds =
8086
Stream.repeatedly(&random_id/0)
8187
|> Stream.uniq()
82-
|> Stream.map(&get_cached_world/1)
88+
|> Stream.map(&WorldCache.fetch(&1))
8389
|> Enum.take(size(params["count"]))
8490

8591
json(conn, worlds)
8692
end
8793

88-
defp get_cached_world(idx) do
89-
case Cache.get(idx) do
90-
nil ->
91-
world = Repo.get(World, idx)
92-
:ok = Cache.put(idx, world)
93-
world
94-
95-
world ->
96-
world
97-
end
98-
end
99-
10094
defp random_id() do
10195
:rand.uniform(@random_max)
10296
end

0 commit comments

Comments
 (0)