Created
April 24, 2020 03:11
-
-
Save akoutmos/5af46fbfed76b6f89872ac132e0cf38f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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