Reputation: 13
How can I get a server response time with ruby?
Im using Net::HTTP.get_response(URI.parse(url))
to the response code and response time of the URL. Actually my code is:
start_time = Time.now
@req = Net::HTTP.get_response(URI.parse(url))
end_time = (Time.now - start_time) * 1000
puts ("%.2f" % end_time + "ms")
Is working perfectly, but I'm getting too high response times, e.g.: Twitter.com (630.52ms). If I try to ping twitter.com, I'm getting 70/120ms of response.
Is this the best way to calculate the response time of this server?
Upvotes: 0
Views: 1274
Reputation: 4222
Ruby timings are included in your version of code as @xlembouras said.
I would suggest to use curl
for what you are looking for:
response_time_total = `curl -w \"%{time_total}\" google.com -o /dev/null -s`
You can try it out in terminal:
curl -w \"%{time_total}\n\" google.com -o /dev/null -s
There are different metrics you can get like time_namelookup
, time_connect
, time_starttransfer
, time_redirect
, etc. Example:
response_times = `curl -w \"%{time_connect}:%{time_starttransfer}:%{time_total}\" google.com -o /dev/null -s`
time_connect, time_starttransfer, time_total = response_times.split(':')
All available metrics and detailed information can be found at the cURL manpage
Refs
How do I measure request and response times at once using cURL?
Upvotes: 0
Reputation: 8295
What you implemented does not show the server response time, it shows:
If you need to see only the time that the server took to process the request you need to do it with another way. You can use the HTTP
response headers. The date header in particular can help you with that.
Upvotes: 2