Skip to content

Commit

Permalink
[elixir] Version and code updates (#9198)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
atavistock authored Aug 13, 2024
1 parent 037639e commit 1b2f23a
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 207 deletions.
29 changes: 2 additions & 27 deletions frameworks/Elixir/phoenix/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,6 @@
"framework": "phoenix",
"tests": [{
"default": {
"json_url": "/json",
"db_url": "/db",
"query_url": "/queries?queries=",
"fortune_url": "/fortunes",
"plaintext_url": "/plaintext",
"update_url": "/updates?queries=",
"cached_query_url": "/cached-queries?count=",
"port": 8080,
"approach": "Realistic",
"classification": "Fullstack",
"database": "Postgres",
"framework": "Phoenix",
"language": "Elixir",
"flavor": "None",
"orm": "full",
"platform": "beam",
"webserver": "cowboy",
"os": "Linux",
"database_os": "Linux",
"display_name": "Phoenix",
"notes": "",
"versus": ""
},
"bandit": {
"json_url": "/json",
"db_url": "/db",
"query_url": "/queries?queries=",
Expand All @@ -45,9 +21,8 @@
"webserver": "bandit",
"os": "Linux",
"database_os": "Linux",
"display_name": "Phoenix-Bandit",
"notes": "",
"versus": "default"
"display_name": "Phoenix",
"notes": ""
}
}]
}
43 changes: 0 additions & 43 deletions frameworks/Elixir/phoenix/config/bandit.exs

This file was deleted.

8 changes: 8 additions & 0 deletions frameworks/Elixir/phoenix/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ config :hello, HelloWeb.Endpoint,
debug_errors: false,
secret_key_base: "Z18ZjzZslFpKd8HB41IljqMavPiOKVF9y1DIQ+S2Ytg7Op0EIauwJgd7mtRStssx"

# Configure cache for world entities
config :hello, Hello.WorldCache,
gc_interval: :timer.hours(1),
max_size: 1_000_000,
allocated_memory: 100_000_000,
gc_cleanup_min_timeout: :timer.seconds(30),
gc_cleanup_max_timeout: :timer.minutes(30)

# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
Expand Down
9 changes: 5 additions & 4 deletions frameworks/Elixir/phoenix/config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Config

config :hello, HelloWeb.Endpoint,
url: [host: "0.0.0.0"],
http: [port: 8080, protocol_options: [max_keepalive: :infinity], backlog: 8096],
cache_static_lookup: false,
adapter: Bandit.PhoenixAdapter,
http: [port: 8080, ip: {0, 0, 0, 0}],
http_options: [log_protocol_errors: false],
compress: false,
check_origin: false,
debug_errors: false,
code_reloader: false,
Expand All @@ -14,7 +15,7 @@ config :hello, Hello.Repo,
password: "benchmarkdbpass",
database: "hello_world",
hostname: "tfb-database",
pool_size: 40,
pool_size: 50,
queue_target: 5000,
log: false

Expand Down
2 changes: 1 addition & 1 deletion frameworks/Elixir/phoenix/lib/hello/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Hello.Application do
def start(_type, _args) do
children = [
Hello.Repo,
{Hello.Cache, []},
{Hello.WorldCache, []},
HelloWeb.Endpoint
]

Expand Down
5 changes: 0 additions & 5 deletions frameworks/Elixir/phoenix/lib/hello/cache.ex

This file was deleted.

31 changes: 31 additions & 0 deletions frameworks/Elixir/phoenix/lib/hello/world_cache.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Hello.WorldCache do
use Nebulex.Cache,
otp_app: :hello,
adapter: Nebulex.Adapters.Local

alias Hello.Models.World
alias Hello.Repo

def seed do
if not __MODULE__.has_key?(:seeded) do
World
|> Repo.all()
|> Enum.into([], &{&1.id, &1})
|> __MODULE__.put_all()

__MODULE__.put(:seeded, true)
end
end

def fetch(id) do
case __MODULE__.get(id) do
nil ->
world = Repo.get(World, id)
:ok = __MODULE__.put(id, world)
world
world ->
world
end
end

end
4 changes: 1 addition & 3 deletions frameworks/Elixir/phoenix/lib/hello_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ defmodule HelloWeb do
defp html_helpers do
quote do
# Use all HTML functionality (forms, tags, etc)
use Phoenix.HTML
# Core UI Components and translation
import HelloWeb.Gettext
import Phoenix.HTML

# Routes generation with the ~p sigil
unquote(verified_routes())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
defmodule HelloWeb.PageController do
alias Hello.Models.{Fortune, World}

use HelloWeb, :controller

alias Hello.Models.Fortune
alias Hello.Models.World
alias Hello.Repo
alias Hello.Cache
alias Hello.WorldCache

@random_max 10_000

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

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

fortunes = [additional_fortune | Repo.all(Fortune)]
fortunes =
[additional_fortune | Repo.all(Fortune)]
|> Enum.sort_by(& &1.message)

render(conn, :fortunes,
fortunes: Enum.sort(fortunes, fn f1, f2 -> f1.message < f2.message end)
)
render(conn, :fortunes, fortunes: fortunes)
end

def updates(conn, params) do
Expand All @@ -54,9 +55,13 @@ defmodule HelloWeb.PageController do
worlds =
Stream.repeatedly(&random_id/0)
|> Stream.uniq()
|> Stream.map(fn idx -> Repo.get(World, idx) end)
|> 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,
Expand All @@ -75,28 +80,17 @@ defmodule HelloWeb.PageController do

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

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

json(conn, worlds)
end

defp get_cached_world(idx) do
case Cache.get(idx) do
nil ->
world = Repo.get(World, idx)
:ok = Cache.put(idx, world)
world

world ->
world
end
end

defp random_id() do
:rand.uniform(@random_max)
end
Expand Down
16 changes: 8 additions & 8 deletions frameworks/Elixir/phoenix/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defmodule Hello.Mixfile do
def project do
[
app: :hello,
version: "0.1.0",
elixir: "~> 1.14",
version: "1.1.1",
elixir: "~> 1.17",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
deps: deps()
Expand All @@ -29,17 +29,17 @@ defmodule Hello.Mixfile do
# Type `mix help deps` for examples and options
defp deps do
[
{:bandit, "~> 1.0.0-pre.5"},
{:bandit, "~> 1.0.0"},
{:gettext, "~> 0.20"},
{:ecto_sql, "~> 3.7"},
{:ecto_sql, "~> 3.10"},
{:jason, "~> 1.2"},
{:phoenix, "~> 1.7"},
{:phoenix_ecto, "~> 4.4"},
{:phoenix_html, "~> 3.2"},
{:phoenix_live_view, "~> 0.18"},
{:phoenix_ecto, "~> 4.4"},
{:phoenix_html, "~> 4.1"},
{:plug_cowboy, "~> 2.5"},
{:postgrex, "~> 0.15"},
{:nebulex, "~> 2.4"}
{:postgrex, ">= 0.0.0"},
{:nebulex, "~> 2.6"}
]
end
end
Loading

0 comments on commit 1b2f23a

Please sign in to comment.