Peter Brown
Peter Brown

Reputation: 51697

Benchmarking Rails Model Methods

Is there something similar to Ruby Benchmark within Rails? I have used Ruby benchmark in the past to compare different bits of code, but none of it was Rails related. I would like to use my application models in some benchmarking to do something along the lines of...

#!/usr/bin/ruby
require 'benchmark'

Benchmark.bmbm do |x|
  x.report("Benchmark 1") do 
    1_000_000.times do
      # do something here...
    end
  end

  x.report("Benchmark 2") do
    1_000_000.times do
      # Do something else here...
    end
  end
end

Which gives me some output like this:

Rehearsal -----------------------------------------------
Benchmark 1   0.070000   0.000000   0.070000 (  0.069236)
Benchmark 2   0.070000   0.000000   0.070000 (  0.069227)
-------------------------------------- total: 0.140000sec

                  user     system      total        real
Benchmark 1   0.070000   0.000000   0.070000 (  0.069793)
Benchmark 2   0.070000   0.000000   0.070000 (  0.069203)

I looked into script/performance/benchmarker and script/performance/profiler, but I couldn't figure out a way to compare methods. I also looked into doing it within a test, but I don't have any sort of assertions, so that didn't seem to make sense.

Upvotes: 9

Views: 6357

Answers (2)

random_user_0891
random_user_0891

Reputation: 2051

Rails added a benchmark generator.

rails g benchmark some_benchmark

it creates the file at

script/benchmarks/some_benchmark.rb

It should automatically add the ips-benchmark gem to your gemfile after running bundle but if it doesn't just add it under the development section in your gemfile.

run it in the terminal by doing

ruby script/benchmarks/some_benchmark.rb

since it loads config/environment.rb in the benchmark file all models are accessible.

Upvotes: 3

Chris
Chris

Reputation: 1836

Performance testing your rails application might be all you need. I think that's all you can do with standalone Rails.

Upvotes: 5

Related Questions