Last active
November 16, 2016 07:01
-
-
Save rainchen/6c6bcb3fe4fc22512a36e50adb77049b to your computer and use it in GitHub Desktop.
benchmark for active_record_vs_exception
This file contains 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
# ref: http://blog.arkency.com/2016/11/ruby-exceptions-are-4400-times-faster-than-activerecord-base-number-create/ | |
require 'active_record' | |
require 'benchmark/ips' | |
ActiveRecord::Base.logger = nil | |
ActiveRecord::Base.establish_connection adapter: 'sqlite3', | |
database: ':memory:' | |
ActiveRecord::Schema.define do | |
create_table :whatevers do |table| | |
table.column :text, :string | |
end | |
end | |
Whatever = Class.new(ActiveRecord::Base) | |
Benchmark.ips do |bench| | |
bench.report('new record') { Whatever.new(text: 'meh') } | |
bench.report('SQL query: write') { Whatever.create(text: 'meh') } | |
bench.report('SQL query: read') { Whatever.find(1) } | |
bench.report('exception hit') { raise StandardError.new rescue nil } | |
bench.report('exception miss') { raise StandardError.new if false } | |
bench.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
benchmark result: