在我的应用程序中,我是否像这样向外部服务器发送多个请求:
req = Curl::Easy.new do |curl|
curl.url = "https://www.mysite.com"
curl.headers['Content-type'] = 'application/json'
end
req.perform
是否可以检索响应时间(请求需要多长时间)?
一个解决方案,我认为是工作是以下(伪代码),但有更优雅的东西?
req = Curl::Easy.new do |curl|
curl.url = "https://www.mysite.com"
curl.headers['Content-type'] = 'application/json'
end
timeBefore = Time.now
req.perform
timeAfter = Time.now
responseTime = timeAfter - timeBefore
你可以这样使用Ruby的基准测试模块:
time = Benchmark.realtime do
req.perform
end
puts "Time elapsed #{time*1000} milliseconds"