1

在我的应用程序中,我是否像这样向外部服务器发出多个请求:

    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
4

1 回答 1

3

您可以按如下方式使用 Ruby 的 Benchmark 模块:

time = Benchmark.realtime do
      req.perform
end
puts "Time elapsed #{time*1000} milliseconds"
于 2013-01-09T13:00:44.057 回答