Skip to content

Instantly share code, notes, and snippets.

@hansonkd
Last active August 17, 2022 14:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hansonkd/cd34329fe4f346e680b39a17d9988af4 to your computer and use it in GitHub Desktop.
Save hansonkd/cd34329fe4f346e680b39a17d9988af4 to your computer and use it in GitHub Desktop.
Redis vs HTTP benchmark

Redis vs HTTP Benchmarks

See the original blog post

Results:

Name                           ips        average  deviation         median         99th %
redix_pool                   70.44       14.20 ms    ±36.07%       13.30 ms       50.60 ms
run_redix_pipeline           30.56       32.73 ms    ±65.74%       47.26 ms       91.99 ms
redix_pool_pipelined         21.55       46.40 ms     ±3.87%       47.59 ms       48.12 ms
redix                        13.84       72.28 ms     ±9.91%       72.09 ms       80.31 ms
finch_get                     0.55     1814.88 ms     ±2.44%     1814.88 ms     1846.24 ms
finch_post                    0.54     1859.71 ms     ±0.70%     1859.71 ms     1868.97 ms
defmodule Mix.Tasks.Statetrace.Bench do
use Mix.Task
alias Statetrace.Meta
require Logger
@impl Mix.Task
@num_tasks 1000
@payload "12345&?\"678,\n90"
@times 100
# credo:disable-for-next-line
def run(_) do
{:ok, _fpid} = Finch.start_link(name: FinchBench, pools: %{:default => [size: 100]})
{:ok, redixPid} = Redix.start_link(host: "localhost", port: 6379)
pids =
Enum.map(1..100, fn _ ->
{:ok, redixPid} = Redix.start_link(host: "localhost", port: 6379)
redixPid
end)
Benchee.run(
%{
"finch_get" => fn -> run_http_get() end,
"finch_post" => fn -> run_http_post() end,
"run_redix_pipeline" => fn -> run_redis_pipeline(redixPid) end,
"redix" => fn -> run_redis(redixPid) end,
"redix_pool" => fn -> run_redix_pool(pids) end,
"redix_pool_pipelined" => fn -> run_redix_pool_pipelined(pids) end
},
time: 3,
memory_time: 2
)
end
def run_http_get() do
tasks =
Enum.map(1..@num_tasks, fn c ->
Task.async(fn ->
Finch.build(
:get,
"http://localhost:9999/bench?" <>
URI.encode_query(%{"payload" => @payload, "times" => @times})
)
|> Finch.request(FinchBench)
end)
end)
Task.await_many(tasks)
end
def run_http_post() do
tasks =
Enum.map(1..@num_tasks, fn c ->
Task.async(fn ->
Finch.build(
:post,
"http://localhost:9999/bench",
[{"content-type", "application/json"}],
Jason.encode!(%{"payload" => @payload, "times" => @times})
)
|> Finch.request(FinchBench)
end)
end)
Task.await_many(tasks)
end
def run_redis(rpid) do
tasks =
Enum.map(1..@num_tasks, fn c ->
Task.async(fn ->
Redix.command(rpid, ["BENCH", @payload, "#{@times}"])
end)
end)
Task.await_many(tasks)
end
def run_redis_pipeline(rpid) do
tasks =
Enum.chunk_every(1..@num_tasks, 10)
|> Enum.map(fn c ->
Task.async(fn ->
Redix.pipeline(rpid, Enum.map(c, fn _ -> ["BENCH", @payload, "#{@times}"] end))
end)
end)
Task.await_many(tasks)
end
def run_redix_pool(pids) do
tasks =
Enum.zip(1..@num_tasks, Stream.cycle(pids))
|> Enum.map(fn {_, pid} ->
Task.async(fn ->
Redix.command(pid, ["BENCH", @payload, "#{@times}"])
end)
end)
Task.await_many(tasks)
end
def run_redix_pool_pipelined(pids) do
tasks =
Enum.zip(Enum.chunk_every(1..@num_tasks, 10), Stream.cycle(pids))
|> Enum.map(fn {c, pid} ->
Task.async(fn ->
Redix.pipeline(pid, Enum.map(c, fn _ -> ["BENCH", @payload, "#{@times}"] end))
end)
end)
Task.await_many(tasks)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment