Question

In my app do I multiple request to an external server like this:

    req = Curl::Easy.new do |curl| 
        curl.url = "https://www.mysite.com"
        curl.headers['Content-type'] = 'application/json'
    end
    req.perform

It is possible to retrieve the response time (how long takes the request)?

A solution that I think is working is the following (pseudocode), but there is something more elegant?

    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
Était-ce utile?

La solution

You can use Ruby's Benchmark module as follows:

time = Benchmark.realtime do
      req.perform
end
puts "Time elapsed #{time*1000} milliseconds"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top