问题
I want to benchmark the time it takes to load a module (a find_by_id(234) call).
Also, how can I track the time it takes to load a page, I know I get this information when I run rails server, but this is in debug mode, I want production speed benchmarks, possible?
回答1:
For a quick check, I would highly recommend checking out Benchmark.
An example would be:
require "benchmark"
time = Benchmark.measure do
a.find_by_id(234)
end
puts time
However for production level benchmarking, I would check out New Relic. They offer unbelievable benchmarking on nearly everything you could think of.
They have a free developer mode here :
http://support.newrelic.com/kb/docs/developer-mode
回答2:
You can use this gem: https://github.com/igorkasyanchuk/benchmark_methods
No more code like this:
t = Time.now
user.calculate_report
puts Time.now - t
Now you can do:
benchmark :calculate_report # in class
And just call your method
user.calculate_report
回答3:
I would highly recommend that you read the Rails Guide on Performance Testing. It's a pretty good overview of what's available within rails. It's great if you want automated tests where you can make code changes and see instant results.
For production server benchmarking, New Relic probably does everything you'd want.
来源:https://stackoverflow.com/questions/5575374/how-can-i-accurately-benchmark-the-time-it-takes-to-load-a-model