Skip to content

Instantly share code, notes, and snippets.

@darktef
Created February 20, 2019 14:09
Show Gist options
  • Save darktef/eb6295cd4519bf6156eaed74d41896c4 to your computer and use it in GitHub Desktop.
Save darktef/eb6295cd4519bf6156eaed74d41896c4 to your computer and use it in GitHub Desktop.
RoR - benchmark ActiveRecord Query
require 'benchmark'
n = 5 # Use any n you like
Benchmark.bmbm do |x|
x.report("find by") {
n.times { User.find_by_name("Joe").id }
}
x.report("select") {
n.times { User.find(:first, :select => :id, :conditions => ["name = ?","Joe"]).id }
}
end
# Reference
# 1. [http://ruby-doc.org/stdlib-2.6/libdoc/benchmark/rdoc/Benchmark.html](Rubydoc - Benchmark):
# The times for some benchmarks depend on the order in which items are run.
# These differences are due to the cost of memory allocation and garbage collection.
# To avoid these discrepancies, the bmbm method is provided.
# 2. [https://stackoverflow.com/questions/3419220/benchmarking-rails-activerecord-queries](Benchmarking rails ActiveRecord Queries)
# 3. [Rails 2.3 benchmarker](https://github.com/rails/rails/blob/2-3-stable/railties/lib/commands/performance/benchmarker.rb)
# 4. [Rails 1.2 activerecord benchmarker](https://github.com/rails/rails/blob/1-2-stable/activerecord/benchmarks/benchmark.rb)
# 5. [Find when a file was deleted in git](https://stackoverflow.com/questions/6839398/find-when-a-file-was-deleted-in-git): git log --full-history -- your_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment