Skip to content

Instantly share code, notes, and snippets.

@pedro
Last active December 18, 2015 03:18
Show Gist options
  • Save pedro/f889ff41ed2769c12c5f to your computer and use it in GitHub Desktop.
Save pedro/f889ff41ed2769c12c5f to your computer and use it in GitHub Desktop.
Testing ActiveRecord performance
require "rubygems"
require "bundler"
Bundler.require
version = ENV["AR"].to_i
if version == 0
abort("specify ActiveRecord version with AR. eg: AR=2 bundle install")
end
ENV["DB"] ||= "sqlite3"
puts "ActiveRecord v#{ActiveRecord::VERSION::STRING}, #{ENV["DB"]}"
if ENV["DB"] == "postgres"
config = {
adapter: "postgresql",
encoding: "utf8",
host: "localhost",
database: "ar-bench",
min_messages: "warning",
}
else
config = {
adapter: "sqlite3",
database: ":memory:",
verbosity: "quiet",
}
end
ActiveRecord::Base.establish_connection(config)
conn = ActiveRecord::Base.connection
conn.execute "DROP TABLE IF EXISTS foobars"
conn.create_table :foobars do |t|
t.text :name
end
class Foobar < ActiveRecord::Base
end
if ENV["IPS"]
Benchmark.ips do |x|
x.report("Write") do
Foobar.create(name: rand(99999))
end
x.report("Read") do
Foobar.first(order: "RANDOM()")
end
end
else
puts "Create:"
puts Benchmark.realtime {
1000.times { Foobar.create(name: rand(99999)) }
}
puts "Read:"
puts Benchmark.realtime {
if version == 2
1000.times { Foobar.first(order: "RANDOM()") }
else
1000.times { Foobar.order("RANDOM()").first }
end
}
end
source "https://rubygems.org"
gem "pg"
gem "sqlite3"
unless version = ENV["AR"]
abort("specify ActiveRecord version with AR. eg: AR=2 bundle install")
else
gem "activerecord", "~> #{version}", require: "active_record"
end
$ ruby -v
ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.0.0]
postgressqlite
readwritereadwrite
benchipsbenchipsbenchipsbenchips
AR20.60729487.7 (±5.9%) i/s - 2499 in 5.146891s1.172106841.7 (±8.4%) i/s - 4233 in 5.067509s1.073255105.9 (±6.6%) i/s - 560 in 5.315018s0.6417991559.3 (±6.6%) i/s - 7854 in 5.058836s
AR30.881564450.7 (±6.2%) i/s - 2254 in 5.022325s1.537033793.8 (±8.2%) i/s - 4015 in 5.092558s1.938538123.0 (±7.3%) i/s - 624 in 5.106388s0.799721313.7 (±7.8%) i/s - 6545 in 5.010759s
AR40.98531448.2 (±7.1%) i/s - 2250 in 5.049966s1.23552729.2 (±11.5%) i/s - 3600 in 5.008097s1.473696117.7 (±11.9%) i/s - 592 in 5.144383s1.4736961378.2 (±10.9%) i/s - 6834 in 5.028393s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment