Browse Source

feat(elixir/phoenix): add caching test (#7611)

Chris Garvis 2 years ago
parent
commit
962830af60

+ 1 - 0
frameworks/Elixir/phoenix/benchmark_config.json

@@ -8,6 +8,7 @@
             "fortune_url": "/fortunes",
             "fortune_url": "/fortunes",
             "plaintext_url": "/plaintext",
             "plaintext_url": "/plaintext",
             "update_url": "/updates?queries=",
             "update_url": "/updates?queries=",
+            "cached_query_url": "/cached-queries?count=",
             "port": 8080,
             "port": 8080,
             "approach": "Realistic",
             "approach": "Realistic",
             "classification": "Fullstack",
             "classification": "Fullstack",

+ 1 - 0
frameworks/Elixir/phoenix/config.toml

@@ -8,6 +8,7 @@ urls.db = "/db"
 urls.query = "/queries?queries="
 urls.query = "/queries?queries="
 urls.update = "/updates?queries="
 urls.update = "/updates?queries="
 urls.fortune = "/fortunes"
 urls.fortune = "/fortunes"
+urls.cached-query = "/cached-queries?count="
 approach = "Realistic"
 approach = "Realistic"
 classification = "Fullstack"
 classification = "Fullstack"
 database = "Postgres"
 database = "Postgres"

+ 1 - 2
frameworks/Elixir/phoenix/lib/hello/application.ex

@@ -5,9 +5,8 @@ defmodule Hello.Application do
   # for more information on OTP Applications
   # for more information on OTP Applications
   def start(_type, _args) do
   def start(_type, _args) do
     children = [
     children = [
-      # Start the Ecto repository
       Hello.Repo,
       Hello.Repo,
-      # Start the endpoint when the application starts
+      Hello.Cache,
       HelloWeb.Endpoint
       HelloWeb.Endpoint
     ]
     ]
 
 

+ 5 - 0
frameworks/Elixir/phoenix/lib/hello/cache.ex

@@ -0,0 +1,5 @@
+defmodule Hello.Cache do
+  use Nebulex.Cache,
+    otp_app: :hello,
+    adapter: Nebulex.Adapters.Local
+end

+ 30 - 2
frameworks/Elixir/phoenix/lib/hello_web/controllers/page_controller.ex

@@ -3,10 +3,12 @@ defmodule HelloWeb.PageController do
 
 
   use HelloWeb, :controller
   use HelloWeb, :controller
 
 
+  alias Hello.Cache
+
   @json "application/json"
   @json "application/json"
   @plain "text/plain"
   @plain "text/plain"
   @random_max 10_000
   @random_max 10_000
-
+  @all_ids 1..10_000
 
 
   def index(conn, _params) do
   def index(conn, _params) do
     resp = Jason.encode!(%{"TE Benchmarks\n" => "Started"})
     resp = Jason.encode!(%{"TE Benchmarks\n" => "Started"})
@@ -95,6 +97,32 @@ defmodule HelloWeb.PageController do
     |> send_resp(200, "Hello, world!")
     |> send_resp(200, "Hello, world!")
   end
   end
 
 
+  def cached(conn, params) do
+    :rand.seed(:exsp)
+
+    resp =
+      @all_ids
+      |> Enum.take_random(size(params["count"]))
+      |> parallel(&get_cached_world/1)
+      |> Jason.encode_to_iodata!()
+
+    conn
+    |> put_resp_content_type(@json, nil)
+    |> send_resp(200, resp)
+  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_but(not_this_value) do
   defp random_but(not_this_value) do
     case :rand.uniform(@random_max) do
     case :rand.uniform(@random_max) do
       new_value when new_value == not_this_value ->
       new_value when new_value == not_this_value ->
@@ -108,7 +136,7 @@ defmodule HelloWeb.PageController do
   defp parallel(collection, func) do
   defp parallel(collection, func) do
     collection
     collection
     |> Enum.map(&Task.async(fn -> func.(&1) end))
     |> Enum.map(&Task.async(fn -> func.(&1) end))
-    |> Enum.map(&Task.await(&1))
+    |> Task.await_many()
   end
   end
 
 
   defp size(nil), do: 1
   defp size(nil), do: 1

+ 1 - 0
frameworks/Elixir/phoenix/lib/hello_web/router.ex

@@ -8,6 +8,7 @@ defmodule HelloWeb.Router do
     get("/fortunes", PageController, :fortunes)
     get("/fortunes", PageController, :fortunes)
     get("/updates", PageController, :updates)
     get("/updates", PageController, :updates)
     get("/plaintext", PageController, :plaintext)
     get("/plaintext", PageController, :plaintext)
+    get("/cached-queries", PageController, :cached)
     get("/", PageController, :index)
     get("/", PageController, :index)
   end
   end
 end
 end

+ 2 - 1
frameworks/Elixir/phoenix/mix.exs

@@ -36,7 +36,8 @@ defmodule Hello.Mixfile do
       {:postgrex, "~> 0.15"},
       {:postgrex, "~> 0.15"},
       {:plug_cowboy, "~> 2.0"},
       {:plug_cowboy, "~> 2.0"},
       {:jason, "~> 1.2"},
       {:jason, "~> 1.2"},
-      {:phoenix_html, "~> 3.0"}
+      {:phoenix_html, "~> 3.0"},
+      {:nebulex, "~> 2.4"}
     ]
     ]
   end
   end
 end
 end

+ 1 - 0
frameworks/Elixir/phoenix/mix.lock

@@ -9,6 +9,7 @@
   "ecto_sql": {:hex, :ecto_sql, "3.8.3", "a7d22c624202546a39d615ed7a6b784580391e65723f2d24f65941b4dd73d471", [:mix], [{:db_connection, "~> 2.5 or ~> 2.4.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.8.4", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 0.16.0 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", "348cb17fb9e6daf6f251a87049eafcb57805e2892e5e6a0f5dea0985d367329b"},
   "ecto_sql": {:hex, :ecto_sql, "3.8.3", "a7d22c624202546a39d615ed7a6b784580391e65723f2d24f65941b4dd73d471", [:mix], [{:db_connection, "~> 2.5 or ~> 2.4.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.8.4", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 0.16.0 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", "348cb17fb9e6daf6f251a87049eafcb57805e2892e5e6a0f5dea0985d367329b"},
   "jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
   "jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
   "mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
   "mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
+  "nebulex": {:hex, :nebulex, "2.4.1", "d06a1c3380010d6663511e3a630755ff07f7fd7a04fd0a3acd25834b3b296b17", [:mix], [{:decorator, "~> 1.4", [hex: :decorator, repo: "hexpm", optional: true]}, {:shards, "~> 1.0", [hex: :shards, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "e19e925b70f041e2b3b6068d82ab7613fa5a28e4c94a96dfe16dc37eabe6b3b5"},
   "phoenix": {:hex, :phoenix, "1.6.12", "f8f8ac077600f84419806dd53114b2e77aedde7a502e74181a7d886355aa0643", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2d6cf5583c9c20f7103c40e6014ef802d96553b8e5d6585ad6e627bd5ddb0d12"},
   "phoenix": {:hex, :phoenix, "1.6.12", "f8f8ac077600f84419806dd53114b2e77aedde7a502e74181a7d886355aa0643", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2d6cf5583c9c20f7103c40e6014ef802d96553b8e5d6585ad6e627bd5ddb0d12"},
   "phoenix_ecto": {:hex, :phoenix_ecto, "4.4.0", "0672ed4e4808b3fbed494dded89958e22fb882de47a97634c0b13e7b0b5f7720", [:mix], [{:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "09864e558ed31ee00bd48fcc1d4fc58ae9678c9e81649075431e69dbabb43cc1"},
   "phoenix_ecto": {:hex, :phoenix_ecto, "4.4.0", "0672ed4e4808b3fbed494dded89958e22fb882de47a97634c0b13e7b0b5f7720", [:mix], [{:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "09864e558ed31ee00bd48fcc1d4fc58ae9678c9e81649075431e69dbabb43cc1"},
   "phoenix_html": {:hex, :phoenix_html, "3.2.0", "1c1219d4b6cb22ac72f12f73dc5fad6c7563104d083f711c3fcd8551a1f4ae11", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "36ec97ba56d25c0136ef1992c37957e4246b649d620958a1f9fa86165f8bc54f"},
   "phoenix_html": {:hex, :phoenix_html, "3.2.0", "1c1219d4b6cb22ac72f12f73dc5fad6c7563104d083f711c3fcd8551a1f4ae11", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "36ec97ba56d25c0136ef1992c37957e4246b649d620958a1f9fa86165f8bc54f"},