From c848c3b5e55c82a4c5ee0ca9b9733e6278c3a7aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 30 Sep 2024 00:26:54 +0200 Subject: [PATCH] [elixir/plug] Clean up boilerplate when encoding to Jason --- .../framework_benchmarks/handlers/cached-world.ex | 4 ++-- .../plug/lib/framework_benchmarks/handlers/db.ex | 6 ++---- .../plug/lib/framework_benchmarks/handlers/json.ex | 2 +- .../lib/framework_benchmarks/handlers/query.ex | 14 +++----------- .../lib/framework_benchmarks/handlers/update.ex | 14 +++----------- .../plug/lib/framework_benchmarks/models/world.ex | 1 + 6 files changed, 12 insertions(+), 29 deletions(-) diff --git a/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/cached-world.ex b/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/cached-world.ex index c80b71053e7..60b441557c5 100644 --- a/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/cached-world.ex +++ b/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/cached-world.ex @@ -11,10 +11,10 @@ defmodule FrameworkBenchmarks.Handlers.CachedWorld do :rand.uniform(10_000) end) - {:ok, json} = + json = ids |> Enum.map(&FrameworkBenchmarks.CachedWorld.get/1) - |> Jason.encode() + |> Jason.encode_to_iodata!() conn |> Plug.Conn.put_resp_content_type("application/json") diff --git a/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/db.ex b/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/db.ex index b1abba5dd8b..101b7e5f8fd 100644 --- a/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/db.ex +++ b/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/db.ex @@ -5,11 +5,9 @@ defmodule FrameworkBenchmarks.Handlers.DB do def handle(conn) do id = :rand.uniform(10_000) - {:ok, json} = + json = FrameworkBenchmarks.Repo.get(FrameworkBenchmarks.Models.World, id) - |> Map.from_struct() - |> Map.drop([:__meta__]) - |> Jason.encode() + |> Jason.encode_to_iodata!() conn |> Plug.Conn.put_resp_content_type("application/json") diff --git a/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/json.ex b/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/json.ex index c013110a6de..3a7ada06cdb 100644 --- a/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/json.ex +++ b/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/json.ex @@ -3,7 +3,7 @@ defmodule FrameworkBenchmarks.Handlers.JSON do This is the handle for the /json route """ def handle(conn) do - {:ok, json} = Jason.encode(%{message: "Hello, World!"}) + json = Jason.encode_to_iodata!(%{message: "Hello, World!"}) conn |> Plug.Conn.put_resp_content_type("application/json") diff --git a/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/query.ex b/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/query.ex index b4e83026a82..6ad2af9061a 100644 --- a/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/query.ex +++ b/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/query.ex @@ -5,7 +5,7 @@ defmodule FrameworkBenchmarks.Handlers.Query do def handle(conn) do number_of_queries = FrameworkBenchmarks.Handlers.Helpers.parse_queries(conn, "queries") - records = + json = 1..number_of_queries |> Enum.map(fn _ -> :rand.uniform(10_000) @@ -15,16 +15,8 @@ defmodule FrameworkBenchmarks.Handlers.Query do FrameworkBenchmarks.Repo.get(FrameworkBenchmarks.Models.World, &1) end) ) - |> Enum.map(&Task.await(&1)) - - {:ok, json} = - records - |> Enum.map(fn record -> - record - |> Map.from_struct() - |> Map.drop([:__meta__]) - end) - |> Jason.encode() + |> Enum.map(&Task.await(&1, :infinity)) + |> Jason.encode_to_iodata!() conn |> Plug.Conn.put_resp_content_type("application/json") diff --git a/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/update.ex b/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/update.ex index d06744adfde..0b5ad54c4b8 100644 --- a/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/update.ex +++ b/frameworks/Elixir/plug/lib/framework_benchmarks/handlers/update.ex @@ -22,7 +22,7 @@ defmodule FrameworkBenchmarks.Handlers.Update do :rand.uniform(10_000) end) - records = + json = ids |> Enum.map( &Task.async(fn -> @@ -38,16 +38,8 @@ defmodule FrameworkBenchmarks.Handlers.Update do |> FrameworkBenchmarks.Repo.update!() end) ) - |> Enum.map(&Task.await(&1)) - - {:ok, json} = - records - |> Enum.map(fn record -> - record - |> Map.from_struct() - |> Map.drop([:__meta__]) - end) - |> Jason.encode() + |> Enum.map(&Task.await(&1, :infinity)) + |> Jason.encode_to_iodata!() conn |> Plug.Conn.put_resp_content_type("application/json") diff --git a/frameworks/Elixir/plug/lib/framework_benchmarks/models/world.ex b/frameworks/Elixir/plug/lib/framework_benchmarks/models/world.ex index 549d8415a9e..d3db33611c1 100644 --- a/frameworks/Elixir/plug/lib/framework_benchmarks/models/world.ex +++ b/frameworks/Elixir/plug/lib/framework_benchmarks/models/world.ex @@ -1,6 +1,7 @@ defmodule FrameworkBenchmarks.Models.World do use Ecto.Schema + @derive {Jason.Encoder, only: [:id, :randomnumber]} schema "world" do field(:randomnumber, :integer) end