12

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Squadrons
  • 2,467
  • 5
  • 25
  • 36
  • 1
    What about using Ruby's Benchmark Module? – jstim Mar 27 '13 at 20:25
  • very good question it is. – Arup Rakshit Mar 27 '13 at 20:26
  • Benchmark seems like it might work - [I found this post](http://blog.segment7.net/2010/05/07/net-http-is-not-slow) where it looks like that's exactly what he used. I'll discuss with my boss, maybe ask around and see if that's likely the best method. – Squadrons Mar 27 '13 at 20:53

2 Answers2

26

As mentioned in the comments, ruby has a benchmark module

require "benchmark"

time = Benchmark.measure do
  #requests
end
ireddick
  • 8,008
  • 2
  • 23
  • 21
7

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.

UpQuark
  • 791
  • 1
  • 11
  • 35
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 3
    While I use benchmark for a lot of things -- you'll find it in many of my answers here, and I probably use it more than anyone handling Ruby answers -- it's overkill for this task. Benchmark is wonderful when I want to test multiple algorithms and need the formatted time detail for user vs. system tasks. In this case they just need to know how long it takes to do a single call and then average the results. Benchmark's formatted results will require reparsing and splitting to get to the same place that a simple average will get. – the Tin Man Mar 28 '13 at 14:27
  • Thanks for the reply. I went with benchmark and it did the job just fine, but I appreciate your input. – Squadrons Mar 28 '13 at 16:54
  • 1
    I like this answer because it reduces the number of dependencies. – Martin Velez Sep 20 '16 at 19:58
  • Thanks. Benchmark is going to add a lot of additional overhead to what should be a very simple task, however some people would rather learn that the hard way. :-) – the Tin Man Sep 20 '16 at 23:42
  • This answer is also correct. For example, you can not use Benchmark with EventMachine. – Anatoliy Kurichev Dec 11 '17 at 08:02
  • See https://stackoverflow.com/a/54472094/5025116 – Sebastián Palma Oct 26 '21 at 20:04