Measure response time for an HTTP request using Ruby

I am creating a small website for personal use to test the API that my company does. My boss wants the site on which we can enter the site to request a form using GET or POST, and the number of times to send a request. He wants the request to be registered, the time for the request and the average time for all requests.

Is there a way to measure the response time for a GET or POST request using Ruby?

I looked at the Net :: HTTP library, but did not see anything that returned time.

Are there any sites that already do this? They will need to have a graphical interface so non-technicians can use it. If not, I planned to have a simple form that runs the script, writes the output of this script to a text file or spreadsheet, and then sends it to the user.

Any other suggestions? (A good interface for working with AJAX may work well, but you probably need a database repository. With 10,000 requests per run, it might be enough.

+9
source share
2 answers

As mentioned in the comments, ruby ​​has a control module

require "benchmark" time = Benchmark.measure do #requests end 
+22
source

Net :: HTTP does not track response time, as this is not its work.

Before and after your request, use Time.now to set the start and end times:

 start_time = Time.now # ...do request... elapsed_time = Time.now - start_time 

If you want to average this, elapsed_time values ​​are elapsed_time together and divide by the number of tests:

 total_time += elapsed_time # ... do some stuff... average_time = total_time / num_of_iterations 

Time is measured in microseconds, which should be accurate enough for your needs.

You are on your own for the interface, since this is a completely different subject and will be a book.

+3
source

All Articles