Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure response time for a HTTP request using Ruby

Tags:

ruby

net-http

I'm building a small website for personal use to test an API my company makes. My boss wants a website where we can enter a website, request a form using GET or POST, and the number of times to send the request. He wants the request logged, the time per request, and the average time for all the requests.

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

I looked through the Net::HTTP library, but didn't see anything that returned the time taken.

Are there any websites that already do this? They'd need to have a GUI so non-techies can use it. If not, I was planning on having a simple form that runs a script, writes the ouput of that script to a text file or spreadsheet, and then sends it to the user.

Any other suggestions? (A nice AJAX looking interface might work nicely, but would probably require database storage. At 10000 requests per run, that could get hefty.

like image 610
Squadrons Avatar asked Mar 27 '13 20:03

Squadrons


2 Answers

Net::HTTP doesn't track response time, since it's not its job.

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

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

If you want to average that, add the elapsed_time values together and divide by the number of tests:

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

Time measures down to the micro-second, which should be plenty accurate for your needs.

You're on your own for the interface as that's an entirely different subject and would constitute a book.

like image 165
the Tin Man Avatar answered Oct 17 '22 00:10

the Tin Man


As mentioned in the comments, ruby has a benchmark module

require "benchmark"

time = Benchmark.measure do
  #requests
end
like image 32
ireddick Avatar answered Oct 16 '22 22:10

ireddick