Last active
August 29, 2015 13:59
-
-
Save SamSaffron/10821252 to your computer and use it in GitHub Desktop.
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
# benching https://bitbucket.org/sam_saffron/ruby-pg/commits/66a8e1e9c6984c0207d14f81ae8ef8ac69256d84 | |
# | |
# automatic casting for pg | |
$: << '/home/sam/Source/ruby-pg/lib' | |
require 'pg' | |
require 'benchmark' | |
require 'active_record' | |
ActiveRecord::Base.establish_connection( | |
adapter: "postgresql", | |
database: "test" | |
) | |
puts ActiveRecord.version | |
class CreateProducts < ActiveRecord::Migration | |
def change | |
create_table :products do |t| | |
t.string :string | |
t.text :text | |
t.integer :int | |
t.float :float | |
t.boolean :bool1 | |
t.boolean :bool2 | |
t.timestamps | |
end | |
end | |
end | |
ActiveRecord::Migrator.new(:up, [CreateProducts.new]).migrate | |
class Product < ActiveRecord::Base | |
end | |
class ProductPoro | |
attr_accessor :id, :string, :text, :int, | |
:float, :bool1, :bool2, :updated_at, | |
:created_at | |
end | |
if Product.count == 0 | |
Product.create!( | |
string: "str", | |
text: "x"*100, | |
int: 1, | |
float: 1.1, | |
bool1: true, | |
bool2: true | |
) | |
end | |
@p = Product.first | |
def to_string(p) | |
"#{p.id} #{p.string} #{p.text} #{p.int} #{p.float} #{p.bool1} #{p.bool2} #{p.updated_at} #{p.created_at}" | |
end | |
def lookup_ar | |
Product.find(@p.id) | |
end | |
def lookup_ar_and_string_it | |
Product.find(@p.id) | |
end | |
@c = ActiveRecord::Base.connection.raw_connection | |
def lookup_pg | |
vals = @c.exec("select * from products where id = #{@p.id}").values | |
p = ProductPoro.new | |
p.id,p.string,p.text,p.int,p.float,p.bool1,p.bool2,p.created_at,p.updated_at = vals[0] | |
p | |
end | |
def to_native!(p) | |
p.id = ActiveRecord::ConnectionAdapters::Column.value_to_integer(p.id) | |
p.int = ActiveRecord::ConnectionAdapters::Column.value_to_integer(p.int) | |
p.bool1 = ActiveRecord::ConnectionAdapters::Column.value_to_boolean(p.bool1) | |
p.bool2 = ActiveRecord::ConnectionAdapters::Column.value_to_boolean(p.bool2) | |
p.float = p.float.to_f | |
p.created_at = ActiveRecord::ConnectionAdapters::Column.string_to_time(p.created_at) | |
p.updated_at = ActiveRecord::ConnectionAdapters::Column.string_to_time(p.updated_at) | |
p | |
end | |
@n = 10000 | |
Benchmark.bmbm do |x| | |
x.report("auto cast pg get all data") do | |
PG::Result.auto_cast = true | |
@n.times do | |
to_string(lookup_pg) | |
end | |
PG::Result.auto_cast = false | |
end | |
x.report("no auto cast pg get all data") do | |
@n.times do | |
to_string(to_native!(lookup_pg)) | |
end | |
end | |
x.report("auto cast AR get all data") do | |
PG::Result.auto_cast = true | |
@n.times do | |
to_string(lookup_ar) | |
end | |
PG::Result.auto_cast = false | |
end | |
x.report("no auto cast AR get all data") do | |
@n.times do | |
to_string(lookup_ar) | |
end | |
end | |
x.report("auto cast pg lookup only") do | |
PG::Result.auto_cast = true | |
@n.times do | |
lookup_pg | |
end | |
PG::Result.auto_cast = false | |
end | |
x.report("no auto cast pg lookup only") do | |
@n.times do | |
lookup_pg | |
end | |
end | |
x.report("auto cast AR lookup only") do | |
PG::Result.auto_cast = true | |
@n.times do | |
lookup_ar | |
end | |
PG::Result.auto_cast = false | |
end | |
x.report("no auto cast AR lookup only") do | |
@n.times do | |
lookup_ar | |
end | |
end | |
end | |
# user system total real | |
# auto cast pg get all data 0.030000 0.630000 0.660000 ( 1.264192) | |
# no auto cast pg get all data 0.010000 0.870000 0.880000 ( 1.528680) | |
# | |
# auto cast AR get all data 0.320000 2.040000 2.360000 ( 2.691338) | |
# no auto cast AR get all data 0.350000 2.220000 2.570000 ( 2.938951) | |
# | |
# auto cast pg lookup only 0.020000 0.550000 0.570000 ( 1.181423) | |
# no auto cast pg lookup only 0.040000 0.400000 0.440000 ( 1.078342) | |
# | |
# auto cast AR lookup only 0.140000 1.960000 2.100000 ( 2.432972) | |
# no auto cast AR lookup only 0.240000 1.730000 1.970000 ( 2.301415) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment