diff options
Diffstat (limited to 'activerecord/examples/performance.rb')
-rw-r--r-- | activerecord/examples/performance.rb | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/activerecord/examples/performance.rb b/activerecord/examples/performance.rb index ad12f8597f..d3546ce948 100644 --- a/activerecord/examples/performance.rb +++ b/activerecord/examples/performance.rb @@ -5,12 +5,12 @@ require 'benchmark/ips' TIME = (ENV['BENCHMARK_TIME'] || 20).to_i RECORDS = (ENV['BENCHMARK_RECORDS'] || TIME*1000).to_i -conn = { :adapter => 'sqlite3', :database => ':memory:' } +conn = { adapter: 'sqlite3', database: ':memory:' } ActiveRecord::Base.establish_connection(conn) class User < ActiveRecord::Base - connection.create_table :users, :force => true do |t| + connection.create_table :users, force: true do |t| t.string :name, :email t.timestamps end @@ -19,7 +19,7 @@ class User < ActiveRecord::Base end class Exhibit < ActiveRecord::Base - connection.create_table :exhibits, :force => true do |t| + connection.create_table :exhibits, force: true do |t| t.belongs_to :user t.string :name t.text :notes @@ -43,6 +43,8 @@ class Exhibit < ActiveRecord::Base def self.feel(exhibits) exhibits.each { |e| e.feel } end end +def progress_bar(int); print "." if (int%100).zero? ; end + puts 'Generating data...' module ActiveRecord @@ -75,30 +77,32 @@ notes = ActiveRecord::Faker::LOREM.join ' ' today = Date.today puts "Inserting #{RECORDS} users and exhibits..." -RECORDS.times do +RECORDS.times do |record| user = User.create( - :created_at => today, - :name => ActiveRecord::Faker.name, - :email => ActiveRecord::Faker.email + created_at: today, + name: ActiveRecord::Faker.name, + email: ActiveRecord::Faker.email ) Exhibit.create( - :created_at => today, - :name => ActiveRecord::Faker.name, - :user => user, - :notes => notes + created_at: today, + name: ActiveRecord::Faker.name, + user: user, + notes: notes ) + progress_bar(record) end +puts "Done!\n" Benchmark.ips(TIME) do |x| ar_obj = Exhibit.find(1) - attrs = { :name => 'sam' } - attrs_first = { :name => 'sam' } - attrs_second = { :name => 'tom' } + attrs = { name: 'sam' } + attrs_first = { name: 'sam' } + attrs_second = { name: 'tom' } exhibit = { - :name => ActiveRecord::Faker.name, - :notes => notes, - :created_at => Date.today + name: ActiveRecord::Faker.name, + notes: notes, + created_at: Date.today } x.report("Model#id") do @@ -117,10 +121,18 @@ Benchmark.ips(TIME) do |x| Exhibit.first.look end + x.report 'Model.take' do + Exhibit.take + end + x.report("Model.all limit(100)") do Exhibit.look Exhibit.limit(100) end + x.report("Model.all take(100)") do + Exhibit.look Exhibit.take(100) + end + x.report "Model.all limit(100) with relationship" do Exhibit.feel Exhibit.limit(100).includes(:user) end @@ -167,6 +179,6 @@ Benchmark.ips(TIME) do |x| end x.report "AR.execute(query)" do - ActiveRecord::Base.connection.execute("Select * from exhibits where id = #{(rand * 1000 + 1).to_i}") + ActiveRecord::Base.connection.execute("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}") end end |