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.
我正在建立一个供个人使用的小型网站来测试我公司制作的API。我的老板想要一个网站,我们可以进入网站,使用GET或POST请求表格,以及发送请求的次数。他希望记录请求,每个请求的时间以及所有请求的平均时间。
Is there a way to measure the response time for a GET or POST request using Ruby?
有没有办法测量使用Ruby的GET或POST请求的响应时间?
I looked through the Net::HTTP library, but didn't see anything that returned the time taken.
我查看了Net :: HTTP库,但没有看到任何返回时间的内容。
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.
有网站已经这样做了吗?他们需要有一个GUI,所以非技术人员可以使用它。如果没有,我计划使用一个运行脚本的简单表单,将该脚本的输出写入文本文件或电子表格,然后将其发送给用户。
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.
还有其他建议吗? (一个漂亮的AJAX外观可能很好用,但可能需要数据库存储。每次运行10000个请求,这可能会很大。
2 个解决方案
#1
19
As mentioned in the comments, ruby has a benchmark module
正如评论中所提到的,ruby有一个基准模块
require "benchmark"
time = Benchmark.measure do
#requests
end
#2
5
Why would Net::HTTP care about response time? That's not its job.
为什么Net :: HTTP会关心响应时间?那不是它的工作。
Before and after your request use Time.now
to set start and end times:
在您的请求之前和之后使用Time.now设置开始和结束时间:
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:
如果要平均,请将elapsed_time值一起添加并除以测试次数:
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.
你是自己的界面,因为这是一个完全不同的主题,并将构成一本书。
And, BTW, you owe me an hour's worth of your wages for doing part of your job for you.
而且,顺便说一下,你为我的部分工作欠你一小时的工资。
#1
19
As mentioned in the comments, ruby has a benchmark module
正如评论中所提到的,ruby有一个基准模块
require "benchmark"
time = Benchmark.measure do
#requests
end
#2
5
Why would Net::HTTP care about response time? That's not its job.
为什么Net :: HTTP会关心响应时间?那不是它的工作。
Before and after your request use Time.now
to set start and end times:
在您的请求之前和之后使用Time.now设置开始和结束时间:
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:
如果要平均,请将elapsed_time值一起添加并除以测试次数:
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.
你是自己的界面,因为这是一个完全不同的主题,并将构成一本书。
And, BTW, you owe me an hour's worth of your wages for doing part of your job for you.
而且,顺便说一下,你为我的部分工作欠你一小时的工资。