Ruby on Rails' Method Benchmark Performance

2019-09-02 19:34发布

问题:

I wonder what the benchmark for few rails methods would look like. Anyone got a website that can run custom methods?:

User.count
#=> 1000000 (Let's say about that)

u = User.where(account_id: 5)
u.count
#=> 100000

u.map |a| a.account_id = 6 end

Is there a way to test this sort of benchmark? How slow or fast is that iteration?

回答1:

You can use ruby benchmark module for this kind of test

require 'benchmark'
Benchmark.bm do |x|
  x.report { User.count }
  x.report { u = User.where(account_id: 5); u.count }
  x.report { u = User.where(account_id: 5); u.map |a| a.account_id = 6 end }
end