bench/topic_perf.rb in mini_sql-0.2.4 vs bench/topic_perf.rb in mini_sql-0.2.5
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'pg', github: 'ged/ruby-pg'
@@ -10,10 +12,11 @@
gem 'memory_profiler'
gem 'benchmark-ips'
gem 'sequel', github: 'jeremyevans/sequel'
gem 'sequel_pg', github: 'jeremyevans/sequel_pg', require: 'sequel'
gem 'swift-db-postgres', github: 'deepfryed/swift-db-postgres'
+ gem 'draper'
end
require 'sequel'
require 'active_record'
require 'memory_profiler'
@@ -87,11 +90,10 @@
end
class TopicSequel < Sequel::Model(:topics)
end
-
Topic.transaction do
topic = {
}
Topic.columns.each do |c|
topic[c.name.to_sym] = case c.type
@@ -182,21 +184,21 @@
s = +""
i = 0
r = $mini_sql.query_single(-"select id, title from topics order by id limit 1000")
while i < r.length
s << r[i].to_s
- s << r[i+1]
+ s << r[i + 1]
i += 2
end
s
end
# connects over unix socket
$swift = Swift::DB::Postgres.new(db: "test_db")
-def swift_select_title_id(l=1000)
- s = ""
+def swift_select_title_id(l = 1000)
+ s = +''
i = 0
r = $swift.execute("select id, title from topics order by id limit 1000")
while i < r.selected_rows
s << r.get(i, 0).to_s
s << r.get(i, 1)
@@ -216,12 +218,180 @@
swift_select_title_id
]
exit(-1) unless results.uniq.length == 1
+# https://github.com/drapergem/draper
+class TopicDraper < Draper::Decorator
+ delegate :id
+ def title_bang
+ object.title + '!!!'
+ end
+end
+
+# https://ruby-doc.org/stdlib-2.5.1/libdoc/delegate/rdoc/SimpleDelegator.html
+class TopicSimpleDelegator < SimpleDelegator
+ def title_bang
+ title + '!!!'
+ end
+end
+
+class TopicDecoratorSequel < TopicSequel
+ def title_bang
+ title + '!!!'
+ end
+end
+
+class TopicArModel < Topic
+ def title_bang
+ title + '!!!'
+ end
+end
+
+module TopicDecorator
+ def title_bang
+ title + '!!!'
+ end
+end
+
Benchmark.ips do |r|
+ r.report('query_decorator') do |n|
+ while n > 0
+ $mini_sql.query_decorator(TopicDecorator, 'select id, title from topics order by id limit 1000').each do |obj|
+ obj.title_bang
+ obj.id
+ end
+ n -= 1
+ end
+ end
+ r.report('extend') do |n|
+ while n > 0
+ $mini_sql.query('select id, title from topics order by id limit 1000').each do |obj|
+ d_obj = obj.extend(TopicDecorator)
+ d_obj.title_bang
+ d_obj.id
+ end
+ n -= 1
+ end
+ end
+ r.report('draper') do |n|
+ while n > 0
+ $mini_sql.query('select id, title from topics order by id limit 1000').each do |obj|
+ d_obj = TopicDraper.new(obj)
+ d_obj.title_bang
+ d_obj.id
+ end
+ n -= 1
+ end
+ end
+ r.report('simple_delegator') do |n|
+ while n > 0
+ $mini_sql.query('select id, title from topics order by id limit 1000').each do |obj|
+ d_obj = TopicSimpleDelegator.new(obj)
+ d_obj.title_bang
+ d_obj.id
+ end
+ n -= 1
+ end
+ end
+ r.report('query') do |n|
+ while n > 0
+ $mini_sql.query('select id, title from topics order by id limit 1000').each do |obj|
+ obj.title + '!!!'
+ obj.id
+ end
+ n -= 1
+ end
+ end
+ r.report('ar model') do |n|
+ while n > 0
+ TopicArModel.limit(1000).order(:id).select(:id, :title).each do |obj|
+ obj.title_bang
+ obj.id
+ end
+ n -= 1
+ end
+ end
+ r.report('sequel model') do |n|
+ while n > 0
+ TopicDecoratorSequel.limit(1000).order(:id).select(:id, :title).each do |obj|
+ obj.title_bang
+ obj.id
+ end
+ n -= 1
+ end
+ end
+
+ r.compare!
+end
+
+# Comparison:
+# query: 828.4 i/s
+# query_decorator: 819.3 i/s - same-ish: difference falls within error
+# sequel model: 672.4 i/s - 1.23x slower
+# extend: 519.4 i/s - 1.59x slower
+# simple_delegator: 496.8 i/s - 1.67x slower
+# draper: 416.2 i/s - 1.99x slower
+# ar model: 113.4 i/s - 7.30x slower
+
+Benchmark.ips do |r|
+ r.report('query_hash') do |n|
+ while n > 0
+ $mini_sql.query_hash('select id, title from topics order by id limit 1000').each do |hash|
+ [hash['id'], hash['title']]
+ end
+ n -= 1
+ end
+ end
+ r.report('query_array') do |n|
+ while n > 0
+ $mini_sql.query_array('select id, title from topics order by id limit 1000').each do |id, title|
+ [id, title]
+ end
+ n -= 1
+ end
+ end
+ r.report('query') do |n|
+ while n > 0
+ $mini_sql.query('select id, title from topics order by id limit 1000').each do |obj|
+ [obj.id, obj.title]
+ end
+ n -= 1
+ end
+ end
+
+ r.compare!
+end
+
+# Comparison:
+# query_array: 1351.6 i/s
+# query: 963.8 i/s - 1.40x slower
+# query_hash: 787.4 i/s - 1.72x slower
+
+Benchmark.ips do |r|
+ r.report('query_single') do |n|
+ while n > 0
+ $mini_sql.query_single('select id from topics order by id limit 1000')
+ n -= 1
+ end
+ end
+ r.report('query_array') do |n|
+ while n > 0
+ $mini_sql.query_array('select id from topics order by id limit 1000').flatten
+ n -= 1
+ end
+ end
+
+ r.compare!
+end
+
+# Comparison:
+# query_single: 2368.9 i/s
+# query_array: 1350.1 i/s - 1.75x slower
+
+Benchmark.ips do |r|
r.report("ar select title id") do |n|
while n > 0
ar_title_id
n -= 1
end
@@ -269,12 +439,10 @@
end
end
r.compare!
end
-
-
def wide_topic_ar
Topic.first
end
def wide_topic_pg
@@ -318,11 +486,10 @@
end
end
r.compare!
end
-
# Comparison:
# pg select title id: 1519.7 i/s
# mini_sql query_single title id: 1335.0 i/s - 1.14x slower
# sequel title id pluck: 1261.6 i/s - 1.20x slower
# mini_sql select title id: 1188.6 i/s - 1.28x slower
@@ -336,12 +503,9 @@
# wide topic pg: 7474.0 i/s
# wide topic mini sql: 7355.2 i/s - same-ish: difference falls within error
# wide topic sequel: 5696.8 i/s - 1.31x slower
# wide topic ar: 2515.0 i/s - 2.97x slower
-
-
# to run deep analysis run
# MemoryProfiler.report do
# ar
# end.pretty_print
-