Instantly share code, notes, and snippets.

Embed
What would you like to do?
#Gemfile
#
# source 'https://rubygems.org'
#
# gem 'activerecord'
# gem 'pg'
# gem 'sequel_pg', require: 'sequel'
# gem 'benchmark-ips'
require "bundler"
Bundler.require
require "active_record"
require "sequel"
require "benchmark/ips"
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "activerecord_unittest")
ActiveRecord::Base.logger = Logger.new(nil)
Sequel.connect("postgres://localhost/activerecord_unittest")
ActiveRecord::Schema.define do
create_table("users", force: true) do |t|
t.string :name, :email
t.timestamps null: false
end
end
class User < ActiveRecord::Base
end
class User2 < Sequel::Model(:users)
end
attributes = {
name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
email: "foobar@email.com"
}
1000.times do
User.create!(attributes)
end
@connection = ActiveRecord::Base.connection.raw_connection
@mapper = PG::BasicTypeMapForResults.new(@connection)
Benchmark.ips do |bm|
bm.report("AR") do
str = ""
User.all.each do |user|
str << "name: #{user.name} email: #{user.email}\n"
end
end
bm.report("Sequel") do
str = ""
User2.all.each do |user|
str << "name: #{user.name} email: #{user.email}\n"
end
end
bm.report("AR with dates") do
str = ""
User.all.each do |user|
str << "name: #{user.name} email: #{user.email} #{user.created_at} #{user.updated_at}\n"
end
end
bm.report("Sequel with dates") do
str = ""
User2.all.each do |user|
str << "name: #{user.name} email: #{user.email} #{user.created_at} #{user.updated_at}\n"
end
end
bm.report("raw") do
str = ""
results = @connection.exec("select * from users")
results.type_map = @mapper
results.ntuples.times do |i|
str << "name: #{results.getvalue(i, 1)} email: #{results.getvalue(i, 2)}\n"
end
end
bm.report("raw with dates") do
str = ""
results = @connection.exec("select * from users")
results.type_map = @mapper
results.ntuples.times do |i|
str << "name: #{results.getvalue(i, 1)} email: #{results.getvalue(i, 2)} #{results.getvalue(i, 3)} #{results.getvalue(i, 4)}\n"
end
end
bm.compare!
end
# raw 393.432 (± 4.3%) i/s - 1.989k in 5.065323s
# Sequel 67.702 (± 4.4%) i/s - 342.000 in 5.062979s
# AR 71.160 (± 5.6%) i/s - 357.000 in 5.035454s
# Sequel with dates 57.551 (± 5.2%) i/s - 290.000 in 5.051495s
# raw with dates 40.150 (± 2.5%) i/s - 201.000 in 5.011677s
# AR with dates 23.351 (± 4.3%) i/s - 118.000 in 5.060813s
raw 393.432 (± 4.3%) i/s - 1.989k in 5.065323s
Sequel 67.702 (± 4.4%) i/s - 342.000 in 5.062979s
AR 71.160 (± 5.6%) i/s - 357.000 in 5.035454s
Sequel with dates 57.551 (± 5.2%) i/s - 290.000 in 5.051495s
raw with dates 40.150 (± 2.5%) i/s - 201.000 in 5.011677s
AR with dates 23.351 (± 4.3%) i/s - 118.000 in 5.060813s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment