Skip to content

Instantly share code, notes, and snippets.

@arktisklada
Created April 8, 2016 19:40
Show Gist options
  • Save arktisklada/f9ed9072474a2696da6e84794a463abb to your computer and use it in GitHub Desktop.
Save arktisklada/f9ed9072474a2696da6e84794a463abb to your computer and use it in GitHub Desktop.
require 'benchmark'
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '4.2.3'
gem 'pg'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'postgresql', host: 'localhost', username: 'claytonliggitt', password: '', database: 'rails', encoding: 'utf8')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :check_ins, force: true do |t|
t.string :patient_id
end
create_table :progresses, force: true do |t|
t.belongs_to :check_in
t.timestamp :completed_at
end
end
class CheckIn < ActiveRecord::Base
has_many :progresses
end
class Progress < ActiveRecord::Base
belongs_to :check_in
end
class BugTest < Minitest::Test
def test_benchmark
10000.times do
check_in = CheckIn.create(patient_id: "1")
Progress.create(check_in: check_in, completed_at: Time.now)
end
Benchmark.bm do |x|
x.report("1") do
1000.times do
CheckIn.joins(:progresses).where(patient_id: "1").where("extract(year from progresses.completed_at) = ?", Time.now.year).length
ActiveRecord::Base.connection.query_cache.clear
end
end
x.report("2") do
1000.times do
CheckIn.joins(:progresses).where(patient_id: "1").where("progresses.completed_at >= ?", Time.now.beginning_of_year).length
ActiveRecord::Base.connection.query_cache.clear
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment