2013-01-09 39 views
1

在我的应用程序做我多次请求到外部服务器这样的:Rails的得到响应时间卷曲(JSON)请求

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 

回答

2

您可以使用Ruby的基准测试模块如下:

time = Benchmark.realtime do 
     req.perform 
end 
puts "Time elapsed #{time*1000} milliseconds" 
+1

感谢您的回答!我不知道这个模块的存在 – damoiser