如何获得服务器的响应时间?
如何使用ruby获得服务器响应时间?
我使用Net::HTTP.get_response(URI.parse(url))
来响应Net::HTTP.get_response(URI.parse(url))
的响应代码和响应时间。 其实我的代码是:
start_time = Time.now @req = Net::HTTP.get_response(URI.parse(url)) end_time = (Time.now - start_time) * 1000 puts ("%.2f" % end_time + "ms")
工作得很好,但我的响应时间太长,例如:Twitter.com(630.52ms)。 如果我尝试ping twitter.com,我会得到70 / 120ms的响应。
这是计算此服务器响应时间的最佳方法吗?
您实施的内容未显示服务器响应时间,它显示:
- 在ruby中花费的时间来发送请求
- 请求的网络时间
- 服务器的响应时间
- 响应的网络时间
- 花在ruby上处理响应的时间
如果您只需要查看服务器处理请求所花费的时间,则需要以其他方式执行此操作。 您可以使用HTTP
响应标头 。 特别是日期标题可以帮助您。
正如@xlembouras所说,Ruby时序包含在您的代码版本中。
我建议你使用curl
来寻找你想要的东西:
response_time_total = `curl -w \"%{time_total}\" google.com -o /dev/null -s`
你可以在终端试试:
curl -w \"%{time_total}\n\" google.com -o /dev/null -s
您可以获得不同的指标,如time_namelookup
, time_connect
, time_starttransfer
, time_redirect
等。示例:
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(':')
可以在cURL联机帮助页中找到所有可用的指标和详细信息
参考文献
如何使用cURL一次测量请求和响应时间?