Created
February 27, 2015 04:59
-
-
Save yuroyoro/6f62a83dfb6fbdd6a1a0 to your computer and use it in GitHub Desktop.
Performance regression in ActiveRecord model accessing generated property method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'benchmark/ips' | |
| begin | |
| class CreateModels < ActiveRecord::Migration | |
| def change | |
| create_table :users do |t| | |
| t.string :attribute1 | |
| t.string :attribute2 | |
| t.string :attribute3 | |
| t.string :attribute4 | |
| t.string :attribute5 | |
| t.string :attribute6 | |
| t.string :attribute7 | |
| t.timestamps null: false | |
| end | |
| end | |
| end | |
| CreateModels.new.change | |
| unless User.count >= 1000 | |
| # insert 1000 records via seeds | |
| 1000.times do | |
| User.create! attribute1: 'foo', attribute2: 'foo', attribute3: 'foo', attribute4: 'foo', attribute5: 'foo', attribute6: 'foo', attribute7: 'foo' | |
| end | |
| end | |
| rescue => e | |
| # Tables already exist | |
| # puts e.message | |
| ensure | |
| users1 = User.all.to_a | |
| users2 = User.all.to_a | |
| Benchmark.ips do |x| | |
| x.report('generated method') do | |
| users1.each do |user| | |
| user.attribute1 | |
| user.attribute2 | |
| user.attribute3 | |
| user.attribute4 | |
| user.attribute5 | |
| user.attribute6 | |
| user.attribute7 | |
| end | |
| end | |
| x.report('read_attribute') do | |
| users2.each do |user| | |
| user.read_attribute(:attribute1) | |
| user.read_attribute(:attribute2) | |
| user.read_attribute(:attribute3) | |
| user.read_attribute(:attribute4) | |
| user.read_attribute(:attribute5) | |
| user.read_attribute(:attribute6) | |
| user.read_attribute(:attribute7) | |
| end | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- Rails 3.2 | |
| Calculating ------------------------------------- | |
| generated method 33.000 i/100ms | |
| read_attribute 12.000 i/100ms | |
| ------------------------------------------------- | |
| generated method 346.685 (± 1.7%) i/s - 1.749k | |
| read_attribute 124.214 (± 0.8%) i/s - 624.000 | |
| --- Rails 4.2 edge | |
| -- create_table(:users) | |
| Calculating ------------------------------------- | |
| generated method 19.000 i/100ms | |
| read_attribute 16.000 i/100ms | |
| ------------------------------------------------- | |
| generated method 200.527 (± 2.0%) i/s - 1.007k | |
| read_attribute 160.680 (± 2.5%) i/s - 816.000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment