Skip to content

Instantly share code, notes, and snippets.

@akoutmos
Created April 24, 2020 03:11
Show Gist options
  • Save akoutmos/5af46fbfed76b6f89872ac132e0cf38f to your computer and use it in GitHub Desktop.
Save akoutmos/5af46fbfed76b6f89872ac132e0cf38f to your computer and use it in GitHub Desktop.
# stress_tester.exs
total_requests = 200
concurrency = 10
url = 'https://httpbin.org/anything'
method = :post
payload = '{"some": "data"}'
:inets.start()
:ssl.start()
average_time =
1..total_requests
|> Task.async_stream(
fn _ ->
start_time = System.monotonic_time()
:httpc.request(method, {url, [], 'application/json', payload}, [], [])
System.monotonic_time() - start_time
end,
max_concurrency: concurrency
)
|> Enum.reduce(0, fn {:ok, req_time}, acc ->
acc + req_time
end)
|> System.convert_time_unit(:native, :millisecond)
|> Kernel./(total_requests)
IO.puts("Average response time from #{url} is #{average_time}ms")
# $ time elixir stress_tester.exs
# Average response time from https://httpbin.org/anything is 55.68ms
# elixir stress_tester.exs 1.78s user 0.75s system 94% cpu 2.673 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment