2013-01-09 69 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