|
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 |