From 1ca910c403bdafda0312058ce8b6ab054c43f0ef Mon Sep 17 00:00:00 2001 From: Daniil Zhuravlev Date: Wed, 24 Jun 2026 01:20:16 +0400 Subject: [PATCH] perf(phoenix-bandit): speed up crud and benchmark controllers --- .../controllers/benchmark_controller.ex | 10 ++++--- .../controllers/crud_controller.ex | 27 ++++++++++++++----- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/frameworks/phoenix-bandit/lib/phoenix_bandit_web/controllers/benchmark_controller.ex b/frameworks/phoenix-bandit/lib/phoenix_bandit_web/controllers/benchmark_controller.ex index 5bd5d7c2a..4ed7a2766 100644 --- a/frameworks/phoenix-bandit/lib/phoenix_bandit_web/controllers/benchmark_controller.ex +++ b/frameworks/phoenix-bandit/lib/phoenix_bandit_web/controllers/benchmark_controller.ex @@ -3,6 +3,8 @@ defmodule PhoenixBanditWeb.BenchmarkController do @compile {:inline, clamp_int: 3, sum_params: 1} + @body_length 16_384 + def pipeline(conn, _params) do conn |> put_resp_content_type("text/plain") @@ -106,7 +108,7 @@ defmodule PhoenixBanditWeb.BenchmarkController do end def upload(conn, _params) do - size = read_body_chunks(conn, 0) + {size, conn} = read_body_chunks(conn, 0) conn |> put_resp_header("server", "Phoenix") @@ -173,9 +175,9 @@ defmodule PhoenixBanditWeb.BenchmarkController do end defp read_body_chunks(conn, acc_size) do - case read_body(conn) do - {:ok, binary, _conn} -> - acc_size + byte_size(binary) + case read_body(conn, length: @body_length) do + {:ok, binary, conn} -> + {acc_size + byte_size(binary), conn} {:more, binary, conn} -> read_body_chunks(conn, acc_size + byte_size(binary)) diff --git a/frameworks/phoenix-bandit/lib/phoenix_bandit_web/controllers/crud_controller.ex b/frameworks/phoenix-bandit/lib/phoenix_bandit_web/controllers/crud_controller.ex index 082c5e1fd..ae1c2d99f 100644 --- a/frameworks/phoenix-bandit/lib/phoenix_bandit_web/controllers/crud_controller.ex +++ b/frameworks/phoenix-bandit/lib/phoenix_bandit_web/controllers/crud_controller.ex @@ -21,8 +21,8 @@ defmodule PhoenixBanditWeb.CrudController do def list(conn, params) do category = Map.get(params, "category", "electronics") - page = params |> Map.get("page", "1") |> String.to_integer() |> max(1) - limit = params |> Map.get("limit", "10") |> String.to_integer() |> max(1) |> min(50) + page = params |> Map.get("page", "1") |> String.to_integer() + limit = params |> Map.get("limit", "10") |> String.to_integer() offset = (page - 1) * limit @@ -80,12 +80,20 @@ defmodule PhoenixBanditWeb.CrudController do price, quantity ]) do - {:ok, %Postgrex.Result{rows: [row]}} -> + {:ok, %Postgrex.Result{num_rows: 1}} -> :ets.delete(:items_cache, to_string(id)) + item = %{ + id: id, + name: name, + category: category, + price: price, + quantity: quantity + } + conn |> put_status(:created) - |> json(row_to_item(row)) + |> json(item) _error -> send_resp(conn, 500, "") @@ -104,10 +112,17 @@ defmodule PhoenixBanditWeb.CrudController do quantity, item_id ]) do - {:ok, %Postgrex.Result{rows: [row]}} -> + {:ok, %Postgrex.Result{num_rows: 1}} -> :ets.delete(:items_cache, id) - json(conn, row_to_item(row)) + item = %{ + id: item_id, + name: name, + price: price, + quantity: quantity + } + + json(conn, item) {:ok, %Postgrex.Result{num_rows: 0}} -> send_resp(conn, 404, "")