Ruby on rails 3 Rails获取curl(JSON)请求的响应时间
在我的应用程序中,我是否向外部服务器发送多个请求,如下所示:Ruby on rails 3 Rails获取curl(JSON)请求的响应时间,ruby-on-rails-3,curl,response-time,Ruby On Rails 3,Curl,Response Time,在我的应用程序中,我是否向外部服务器发送多个请求,如下所示: req = Curl::Easy.new do |curl| curl.url = "https://www.mysite.com" curl.headers['Content-type'] = 'application/json' end req.perform 是否可以检索响应时间(请求需要多长时间) 我认为一个可行的解决方案是以下(伪代码),但还有更优雅的吗 r
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"
谢谢你的回答!我不知道这个模块的存在