2010-06-12 08:07:35 +00:00
|
|
|
require "active_record"
|
2016-08-06 17:28:46 +00:00
|
|
|
require "benchmark/ips"
|
2009-08-21 04:09:59 +00:00
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
TIME = (ENV["BENCHMARK_TIME"] || 20).to_i
|
2016-10-29 03:05:58 +00:00
|
|
|
RECORDS = (ENV["BENCHMARK_RECORDS"] || TIME * 1000).to_i
|
2012-08-17 12:52:16 +00:00
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
conn = { adapter: "sqlite3", database: ":memory:" }
|
2009-08-21 04:09:59 +00:00
|
|
|
|
|
|
|
ActiveRecord::Base.establish_connection(conn)
|
|
|
|
|
|
|
|
class User < ActiveRecord::Base
|
2013-06-24 05:49:51 +00:00
|
|
|
connection.create_table :users, force: true do |t|
|
2009-08-21 04:09:59 +00:00
|
|
|
t.string :name, :email
|
|
|
|
t.timestamps
|
|
|
|
end
|
|
|
|
|
|
|
|
has_many :exhibits
|
|
|
|
end
|
|
|
|
|
|
|
|
class Exhibit < ActiveRecord::Base
|
2013-06-24 05:49:51 +00:00
|
|
|
connection.create_table :exhibits, force: true do |t|
|
2009-08-21 04:09:59 +00:00
|
|
|
t.belongs_to :user
|
|
|
|
t.string :name
|
|
|
|
t.text :notes
|
|
|
|
t.timestamps
|
|
|
|
end
|
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
def look; attributes end
|
|
|
|
def feel; look; user.name end
|
|
|
|
|
2011-12-15 19:26:33 +00:00
|
|
|
def self.with_name
|
|
|
|
where("name IS NOT NULL")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.with_notes
|
|
|
|
where("notes IS NOT NULL")
|
|
|
|
end
|
|
|
|
|
2014-10-27 16:28:53 +00:00
|
|
|
def self.look(exhibits) exhibits.each(&:look) end
|
|
|
|
def self.feel(exhibits) exhibits.each(&:feel) end
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
|
|
|
|
2016-10-29 03:05:58 +00:00
|
|
|
def progress_bar(int); print "." if (int % 100).zero? ; end
|
2013-07-05 15:20:54 +00:00
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
puts "Generating data..."
|
2011-02-08 03:53:22 +00:00
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
class Faker
|
2016-08-06 18:20:22 +00:00
|
|
|
LOREM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse non aliquet diam. Curabitur vel urna metus, quis malesuada elit.
|
2011-11-29 09:50:02 +00:00
|
|
|
Integer consequat tincidunt felis. Etiam non erat dolor. Vivamus imperdiet nibh sit amet diam eleifend id posuere diam malesuada. Mauris at accumsan sem.
|
|
|
|
Donec id lorem neque. Fusce erat lorem, ornare eu congue vitae, malesuada quis neque. Maecenas vel urna a velit pretium fermentum. Donec tortor enim,
|
|
|
|
tempor venenatis egestas a, tempor sed ipsum. Ut arcu justo, faucibus non imperdiet ac, interdum at diam. Pellentesque ipsum enim, venenatis ut iaculis vitae,
|
|
|
|
varius vitae sem. Sed rutrum quam ac elit euismod bibendum. Donec ultricies ultricies magna, at lacinia libero mollis aliquam. Sed ac arcu in tortor elementum
|
|
|
|
tincidunt vel interdum sem. Curabitur eget erat arcu. Praesent eget eros leo. Nam magna enim, sollicitudin vehicula scelerisque in, vulputate ut libero.
|
2016-08-06 18:20:22 +00:00
|
|
|
Praesent varius tincidunt commodo".split
|
2011-11-29 09:50:02 +00:00
|
|
|
|
2011-02-08 03:53:22 +00:00
|
|
|
def self.name
|
2016-08-06 17:28:46 +00:00
|
|
|
LOREM.grep(/^\w*$/).sort_by { rand }.first(2).join " "
|
2011-02-08 03:53:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.email
|
2016-08-06 17:28:46 +00:00
|
|
|
LOREM.grep(/^\w*$/).sort_by { rand }.first(2).join("@") + ".com"
|
2011-02-08 03:53:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# pre-compute the insert statements and fake data compilation,
|
|
|
|
# so the benchmarks below show the actual runtime for the execute
|
|
|
|
# method, minus the setup steps
|
|
|
|
|
|
|
|
# Using the same paragraph for all exhibits because it is very slow
|
|
|
|
# to generate unique paragraphs for all exhibits.
|
2016-08-06 17:28:46 +00:00
|
|
|
notes = ActiveRecord::Faker::LOREM.join " "
|
2011-02-08 03:53:22 +00:00
|
|
|
today = Date.today
|
|
|
|
|
2012-08-17 12:52:16 +00:00
|
|
|
puts "Inserting #{RECORDS} users and exhibits..."
|
2013-07-05 15:20:54 +00:00
|
|
|
RECORDS.times do |record|
|
2011-02-08 03:53:22 +00:00
|
|
|
user = User.create(
|
2013-06-24 05:49:51 +00:00
|
|
|
created_at: today,
|
|
|
|
name: ActiveRecord::Faker.name,
|
|
|
|
email: ActiveRecord::Faker.email
|
2011-02-08 03:53:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
Exhibit.create(
|
2013-06-24 05:49:51 +00:00
|
|
|
created_at: today,
|
|
|
|
name: ActiveRecord::Faker.name,
|
|
|
|
user: user,
|
|
|
|
notes: notes
|
2011-02-08 03:53:22 +00:00
|
|
|
)
|
2013-07-05 15:20:54 +00:00
|
|
|
progress_bar(record)
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
2013-07-05 15:20:54 +00:00
|
|
|
puts "Done!\n"
|
2009-08-21 04:09:59 +00:00
|
|
|
|
2012-08-17 12:52:16 +00:00
|
|
|
Benchmark.ips(TIME) do |x|
|
2011-02-08 03:53:22 +00:00
|
|
|
ar_obj = Exhibit.find(1)
|
2016-08-06 17:28:46 +00:00
|
|
|
attrs = { name: "sam" }
|
|
|
|
attrs_first = { name: "sam" }
|
|
|
|
attrs_second = { name: "tom" }
|
2011-02-08 03:53:22 +00:00
|
|
|
exhibit = {
|
2013-06-24 05:49:51 +00:00
|
|
|
name: ActiveRecord::Faker.name,
|
|
|
|
notes: notes,
|
2013-06-26 17:49:54 +00:00
|
|
|
created_at: Date.today
|
2011-02-08 03:53:22 +00:00
|
|
|
}
|
2009-08-21 04:09:59 +00:00
|
|
|
|
2012-08-17 10:41:50 +00:00
|
|
|
x.report("Model#id") do
|
|
|
|
ar_obj.id
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
x.report "Model.new (instantiation)" do
|
2012-08-17 10:41:50 +00:00
|
|
|
Exhibit.new
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
x.report "Model.new (setting attributes)" do
|
2012-08-17 10:41:50 +00:00
|
|
|
Exhibit.new(attrs)
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
x.report "Model.first" do
|
2012-08-17 10:41:50 +00:00
|
|
|
Exhibit.first.look
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:55:02 +00:00
|
|
|
x.report "Model.take" do
|
2013-07-05 15:20:54 +00:00
|
|
|
Exhibit.take
|
|
|
|
end
|
2016-08-06 17:55:02 +00:00
|
|
|
|
2012-08-17 12:52:16 +00:00
|
|
|
x.report("Model.all limit(100)") do
|
|
|
|
Exhibit.look Exhibit.limit(100)
|
|
|
|
end
|
|
|
|
|
2013-07-05 15:20:54 +00:00
|
|
|
x.report("Model.all take(100)") do
|
|
|
|
Exhibit.look Exhibit.take(100)
|
|
|
|
end
|
|
|
|
|
2012-08-17 12:52:16 +00:00
|
|
|
x.report "Model.all limit(100) with relationship" do
|
|
|
|
Exhibit.feel Exhibit.limit(100).includes(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
x.report "Model.all limit(10,000)" do
|
|
|
|
Exhibit.look Exhibit.limit(10000)
|
|
|
|
end
|
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
x.report "Model.named_scope" do
|
2012-08-17 10:41:50 +00:00
|
|
|
Exhibit.limit(10).with_name.with_notes
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
x.report "Model.create" do
|
2012-08-17 10:41:50 +00:00
|
|
|
Exhibit.create(exhibit)
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
x.report "Resource#attributes=" do
|
2012-08-17 10:41:50 +00:00
|
|
|
e = Exhibit.new(attrs_first)
|
|
|
|
e.attributes = attrs_second
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
x.report "Resource#update" do
|
|
|
|
Exhibit.first.update(name: "bob")
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
x.report "Resource#destroy" do
|
2012-08-17 10:41:50 +00:00
|
|
|
Exhibit.first.destroy
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
x.report "Model.transaction" do
|
2012-08-17 10:41:50 +00:00
|
|
|
Exhibit.transaction { Exhibit.new }
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
x.report "Model.find(id)" do
|
2012-08-17 10:41:50 +00:00
|
|
|
User.find(1)
|
2010-09-10 20:09:26 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:28:46 +00:00
|
|
|
x.report "Model.find_by_sql" do
|
2012-08-17 10:41:50 +00:00
|
|
|
Exhibit.find_by_sql("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}").first
|
2010-09-10 20:09:26 +00:00
|
|
|
end
|
|
|
|
|
2012-08-17 10:41:50 +00:00
|
|
|
x.report "Model.log" do
|
|
|
|
Exhibit.connection.send(:log, "hello", "world") {}
|
2010-09-10 20:09:26 +00:00
|
|
|
end
|
|
|
|
|
2012-08-17 10:41:50 +00:00
|
|
|
x.report "AR.execute(query)" do
|
2013-07-04 17:16:45 +00:00
|
|
|
ActiveRecord::Base.connection.execute("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}")
|
2010-09-10 20:09:26 +00:00
|
|
|
end
|
2009-08-21 04:09:59 +00:00
|
|
|
end
|