Skip to content

Instantly share code, notes, and snippets.

@larskanis
Created June 5, 2018 07:24
Show Gist options
  • Save larskanis/569231d055d8dae03bed1d9a5deca6cb to your computer and use it in GitHub Desktop.
Save larskanis/569231d055d8dae03bed1d9a5deca6cb to your computer and use it in GitHub Desktop.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'pg', "1.0.0"
gem 'activerecord', '5.2.0'
gem 'sequel', '5.8.0'
# gem 'sequel_pg', :require=>'sequel'
gem 'memory_profiler'
gem 'benchmark-ips'
end
require 'active_record'
require 'sequel'
require 'memory_profiler'
require 'benchmark/ips'
system "createdb test_db"
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:database => "test_db"
)
DB = Sequel.postgres("test_db")
DB.extension :pg_array
at_exit do
ActiveRecord::Base.remove_connection
DB.disconnect
system "dropdb test_db"
end
pg = ActiveRecord::Base.connection.raw_connection
pg.async_exec <<SQL
drop table if exists topics
SQL
pg.async_exec <<SQL
CREATE TABLE topics (
id integer NOT NULL,
iary int[],
sary text[],
tary timestamp[]
)
SQL
class Topic < ActiveRecord::Base
end
Topic.transaction do
topic = {
}
Topic.columns.each do |c|
topic[c.name.to_sym] = case [c.type, c.array?]
when [:integer, true] then "{#{20.times.to_a.join(",")}}"
when [:text, true] then "{#{(65..85).map(&:chr).to_a.join(",")}}"
when [:datetime, true] then "{#{(["2018-06-05 12:33:44"] * 10).join(",")}}"
else "HELLO WORLD" * 2
end
end
10.times do |id|
topic[:id] = id
Topic.create!(topic)
end
end
$conn = ActiveRecord::Base.connection.raw_connection
class TopicSequel < Sequel::Model(:topics)
end
def ar_pluck_iary
Topic.limit(1000).pluck(:iary)
end
def sequel_pluck_iary
TopicSequel.limit(1000).select_map(:iary)
end
def ar_pluck_sary
Topic.limit(1000).pluck(:sary)
end
def sequel_pluck_sary
TopicSequel.limit(1000).select_map(:sary)
end
def ar_pluck_tary
Topic.limit(1000).pluck(:tary)
end
def sequel_pluck_tary
TopicSequel.limit(1000).select_map(:tary)
end
def test(method)
puts
puts "-"*50
puts method.to_s
puts "-"*50
send method
MemoryProfiler.report do
send method
end.pretty_print(detailed_report: false, retained_strings: false, allocated_strings: false)
end
tests = %i{
sequel_pluck_iary
sequel_pluck_sary
sequel_pluck_tary
ar_pluck_iary
ar_pluck_sary
ar_pluck_tary
}
# tests.each do |t|
# test t
# end
Benchmark.ips do |b|
tests.each do |t|
b.report(t.to_s) do |i|
while i > 0
send t
i -= 1
end
end
end
end
# to run deep analysis run
# MemoryProfiler.report do
# ar
# end.pretty_print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment