aungkoko
How can I get the response time of a HTTP request in elixir?
I am doing some monitoring for my api endpoints. I’ve already tried out with some http clients like httpoison, and hackney erlang library. But they don’t give me http response time, do they? Did someone experience kind of this? It would be grateful if someone can help me out.
Most Liked
dom
I tend to do something like
{microseconds, result} = :timer.tc(fn -> do_the_request(...) end)
milliseconds = System.convert_time_unit(microseconds, :microseconds, :millisecond)
yurko
I do this
start_ms = System.monotonic_time(:milliseconds)
# do the request and receive response
end_ms = System.monotonic_time(:milliseconds)
diff = end_ms - start_ms
idi527
I don’t know if that would work for you (I’ve seen it being used in knutin/elli), but if all of your work is done in a single process, you can put timings into the process dictionary:
defp t(key) do
:erlang.put({:time, key}, :os.timestamp())
end
and call it in different places inside the same process
t(:start_request)
# request something
t(:done_requesting)
t(:start_parsing_body)
# parse body
t(:done_parsing_body)
and then collect the timings at some point later (you would need to be in the same process though).
Enum.flat_map(:erlang.get(), fn
{{:time, event} = key, time} ->
:erlang.erase(key)
[{event, time}]
_ ->
[]
end)
which would return something like
timings = [start_request: {1518, 275996, 334820}, done_requesting: {1518, 276000, 54915}]
and you can calculate diffs with :timer.now_diff/2
start_request = timings[:start_request]
done_requesting = timings[:done_requesting]
:timer.now_diff(done_requesting, start_request)
#=> 3720095 (microseconds)
You might want to read Two frequently used system calls are ~77% slower on AWS EC2 if you are on xen. I think beam uses these calls if it is compiled with --enable-gettimeofday-as-os-system-time and erl_xcomp_clock_gettime_cpu_time set to yes, but I might be wrong.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









