DEV: minor bench cleanups

DEV: minor bench cleanups

pg can use the standard pg gem add an optimized AR version for builder perf

diff --git a/bench/builder_perf.rb b/bench/builder_perf.rb
index ebed532..8e8aaf7 100644
--- a/bench/builder_perf.rb
+++ b/bench/builder_perf.rb
@@ -4,12 +4,11 @@ require 'bundler/inline'
 
 gemfile do
   source 'https://rubygems.org'
-  gem 'pg', github: 'ged/ruby-pg'
   gem 'mini_sql', path: '../'
+  gem 'pg'
   gem 'activerecord'
   gem 'activemodel'
   gem 'benchmark-ips'
-  gem 'pry'
 end
 
 require 'active_record'
@@ -44,6 +43,17 @@ def ar_prepared(user_id)
     .load
 end
 
+def ar_prepared_optimized(user_id)
+  @rel ||= Topic
+    .select(User.arel_table[:first_name] , Topic.arel_table[:id].count)
+    .joins(:user, :category)
+    .group(User.arel_table[:id])
+
+  @rel
+    .where(user_id: user_id)
+    .load
+end
+
 def ar_unprepared(user_id)
   Topic
     .select('users.first_name, count(distinct topics.id) topics_count')
@@ -72,6 +82,14 @@ Benchmark.ips do |x|
       n -= 1
     end
   end
+
+  x.report("ar_prepared_optimized") do |n|
+    while n > 0
+      ar_prepared_optimized(rand(100))
+      n -= 1
+    end
+  end
+
   x.report("ar_unprepared") do |n|
     while n > 0
       ar_unprepared(rand(100))
diff --git a/bench/prepared_perf.rb b/bench/prepared_perf.rb
index c0f296c..db8144f 100644
--- a/bench/prepared_perf.rb
+++ b/bench/prepared_perf.rb
@@ -4,12 +4,11 @@ require 'bundler/inline'
 
 gemfile do
   source 'https://rubygems.org'
-  gem 'pg', github: 'ged/ruby-pg'
   gem 'mini_sql', path: '../'
+  gem 'pg'
   gem 'activerecord'
   gem 'activemodel'
   gem 'benchmark-ips'
-  gem 'pry'
 end
 
 require 'active_record'
@@ -57,4 +56,4 @@ end
 #
 # Comparison:
 #                   ps:    10287.2 i/s
-#           without ps:     2970.0 i/s - 3.46x  (± 0.00) slower
\ No newline at end of file
+#           without ps:     2970.0 i/s - 3.46x  (± 0.00) slower
diff --git a/bench/shared/generate_data.rb b/bench/shared/generate_data.rb
index 8622988..a16b0e0 100644
--- a/bench/shared/generate_data.rb
+++ b/bench/shared/generate_data.rb
@@ -18,7 +18,7 @@ class GenerateData
       user: 'postgres',
       host: 'localhost'
     }
-    
+
     db_conn = conn_settings.merge(database: "test_db", adapter: "postgresql")
 
     pg = PG::Connection.new(conn_settings)

GitHub sha: 8ea31885

1 Like