-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark_runner.rb
321 lines (270 loc) · 10.5 KB
/
benchmark_runner.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
require_relative 'setup'
class BenchmarkRunner
class << self
def run_batch_processing_comparison
puts PASTEL.bold("\n=== Batch Processing Comparison ===")
results = []
results << measure_and_report("Processing all records at once") do
Post.all.each { |post| post.update(title: "#{post.title} - Updated") }
end
results << measure_and_report("Using find_each with default batch size") do
Post.find_each { |post| post.update(title: "#{post.title} - Updated") }
end
results << measure_and_report("Using find_each with custom batch size") do
Post.find_each(batch_size: 1000) { |post| post.update(title: "#{post.title} - Updated") }
end
results << measure_and_report("Using update_all") do
Post.update_all("title = CONCAT(title, ' - Bulk Updated')")
end
display_comparison(results, "Batch Processing")
end
def run_batch_import_comparison
puts PASTEL.bold("\n=== Batch Import Performance ===")
new_users = 10_000.times.map do |i|
{
name: "Batch User #{i}",
email: "batch_import_#{i}@example.com",
created_at: Time.current,
updated_at: Time.current
}
end
results = []
results << measure_and_report("Individual inserts") do
new_users.take(100).each { |user_data| User.create!(user_data) }
end
results << measure_and_report("Bulk insert with insert_all") do
User.insert_all(new_users.take(100))
end
display_comparison(results, "Batch Import")
end
def run_query_optimization_comparison
puts PASTEL.bold("\n=== Query Optimization Techniques ===")
# Ensure indexes exist
ActiveRecord::Base.connection.add_index :posts, :title unless ActiveRecord::Base.connection.index_exists?(:posts, :title)
ActiveRecord::Base.connection.add_index :posts, [:user_id, :created_at] unless ActiveRecord::Base.connection.index_exists?(:posts, [:user_id, :created_at])
results = []
results << measure_and_report("Query without index") do
Post.where("content LIKE ?", "%Ruby%").to_a
end
results << measure_and_report("Query with index") do
Post.where("title LIKE ?", "%Ruby%").to_a
end
results << measure_and_report("Complex query without optimization") do
User.joins(:posts)
.where(posts: { created_at: 1.week.ago..Time.current })
.group("users.id")
.having("COUNT(posts.id) > 5")
.to_a
end
results << measure_and_report("Complex query with optimization") do
User.joins(:posts)
.where(posts: { created_at: 1.week.ago..Time.current })
.select("users.*, COUNT(posts.id) as posts_count")
.group("users.id")
.having("COUNT(posts.id) > 5")
.to_a
end
display_comparison(results, "Query Optimization")
end
def run_upsert_comparison
puts PASTEL.bold("\n=== Upsert Performance Comparison ===")
# Prepare test data
upsert_users = 1000.times.map do |i|
{
email: "user_#{i}@example.com",
name: "User #{i}",
created_at: Time.current,
updated_at: Time.current
}
end
# Add duplicates
upsert_users += 200.times.map do |i|
{
email: "user_#{i}@example.com",
name: "Updated User #{i}",
created_at: Time.current,
updated_at: Time.current
}
end
results = []
results << measure_and_report("Individual find_or_create_by") do
upsert_users.take(100).each do |user_data|
User.find_or_create_by(email: user_data[:email]) do |user|
user.assign_attributes(user_data)
end
end
end
results << measure_and_report("Bulk upsert with insert_all") do
User.upsert_all(
upsert_users.take(100),
unique_by: :email,
returning: false
)
end
results << measure_and_report("Sequel upsert") do
DB[:users].insert_conflict(
target: :email,
update: {
name: Sequel[:excluded][:name],
updated_at: Time.current
}
).multi_insert(upsert_users.take(100))
end
display_comparison(results, "Upsert")
end
private
def display_comparison(results, title)
puts "\n#{PASTEL.bold.magenta("#{title} Comparison:")}"
# Find baseline (slowest) time for relative comparison
baseline_time = results.map { |r| r[:time] }.max
# Create comparison table
headers = ['Operation', 'Time', 'Queries', 'Rate', 'Relative Speed']
rows = results.map do |result|
relative_speed = baseline_time / result[:time]
speed_text = if relative_speed == 1
"baseline"
else
"#{relative_speed.round(1)}x faster"
end
rate = if result[:operations]
ops_per_sec = result[:operations] / result[:time]
if ops_per_sec >= 1000
"#{format_number((ops_per_sec / 1000.0).round(1))}k/s"
else
"#{format_number(ops_per_sec.round(1))}/s"
end
else
"n/a"
end
[
result[:title],
format_duration(result[:time]),
format_number(result[:queries]),
PASTEL.cyan(rate),
PASTEL.send(relative_speed == 1 ? :white : :green, speed_text)
]
end
table = TTY::Table.new(headers, rows)
puts table.render(:unicode, padding: [0, 1], alignment: [:left, :right, :right, :right, :left])
# Add summary of findings
puts "\n#{PASTEL.yellow('Key Findings:')}"
fastest = results.min_by { |r| r[:time] }
slowest = results.max_by { |r| r[:time] }
most_queries = results.max_by { |r| r[:queries] }
least_queries = results.min_by { |r| r[:queries] }
puts "• #{PASTEL.green('Fastest')}: #{fastest[:title]} (#{format_duration(fastest[:time])})"
puts "• #{PASTEL.red('Slowest')}: #{slowest[:title]} (#{format_duration(slowest[:time])})"
puts "• #{PASTEL.green('Most efficient')}: #{least_queries[:title]} (#{format_number(least_queries[:queries])} queries)"
puts "• #{PASTEL.red('Least efficient')}: #{most_queries[:title]} (#{format_number(most_queries[:queries])} queries)"
end
def measure_and_report(title)
puts "\n#{PASTEL.cyan(title)}:"
queries_count = 0
sequel_queries_count = 0
memory_before = GetProcessMem.new.mb
# Setup Sequel query logging
original_logger = DB.loggers
DB.loggers = [Logger.new(StringIO.new).tap { |logger|
logger.define_singleton_method(:info) { |*| sequel_queries_count += 1 }
}]
time = Benchmark.realtime do
ActiveSupport::Notifications.subscribed(-> (*) { queries_count += 1 }, "sql.active_record") do
yield
end
end
# Restore Sequel logger
DB.loggers = original_logger
# Calculate memory usage
memory_after = GetProcessMem.new.mb
memory_change = memory_after - memory_before
# Use total queries (ActiveRecord + Sequel)
total_queries = queries_count + sequel_queries_count
# Determine operations count based on the benchmark type
operations = case title
when /Processing all records/, /find_each/, /update_all/
Post.count
when /Individual inserts/, /Bulk insert/, /upsert/
100 # We're using take(100) in these cases
else
nil
end
# Calculate rates
rate = operations ? (operations / time).round(2) : nil
queries_per_sec = (total_queries / time).round(2)
table = TTY::Table.new(
[
["#{PASTEL.green('Time taken')}", format_duration(time)],
["#{PASTEL.green('Queries executed')}", format_number(total_queries)],
["#{PASTEL.green('Memory change')}", format_memory(memory_change)],
["#{PASTEL.green('Operations')}", operations ? format_number(operations) : "n/a"],
["#{PASTEL.green('Rate')}", rate ? "#{format_number(rate)}/s" : "n/a"],
["#{PASTEL.green('Queries/sec')}", format_number(queries_per_sec)]
]
)
puts table.render(:unicode, padding: [0, 1])
{
time: time,
queries: total_queries,
title: title,
operations: operations,
memory_change: memory_change,
queries_per_sec: queries_per_sec
}
end
def format_memory(mb)
if mb >= 1024
"%.1f GB" % (mb / 1024.0)
else
"%.1f MB" % mb
end
end
end
end
# Interactive menu
def run_interactive_benchmarks
setup_database
spinner = TTY::Spinner.new("[:spinner] Generating sample data ...", format: :dots)
spinner.auto_spin
generate_sample_data(users: 2000, posts_per_user: 15, comments_per_post: 8)
spinner.success(PASTEL.green("Done!"))
loop do
begin
puts PASTEL.bold("\n=== Ruby PostgreSQL Performance Benchmarks ===\n")
choice = PROMPT.select("Choose a benchmark to run:", per_page: 10, help: "(Press Ctrl+C to exit)") do |menu|
menu.choice "Batch Processing Comparison", 1
menu.choice "Batch Import Performance", 2
menu.choice "Query Optimization Techniques", 3
menu.choice "Upsert Performance Comparison", 4
menu.choice "Run All Benchmarks", 5
menu.choice "Exit", 6
end
case choice
when 1
BenchmarkRunner.run_batch_processing_comparison
when 2
BenchmarkRunner.run_batch_import_comparison
when 3
BenchmarkRunner.run_query_optimization_comparison
when 4
BenchmarkRunner.run_upsert_comparison
when 5
BenchmarkRunner.run_batch_processing_comparison
BenchmarkRunner.run_batch_import_comparison
BenchmarkRunner.run_query_optimization_comparison
BenchmarkRunner.run_upsert_comparison
when 6
puts PASTEL.green("\nThank you for using the benchmark runner!")
return
end
rescue TTY::Reader::InputInterrupt
puts PASTEL.yellow("\nBenchmark runner interrupted. Exiting gracefully...")
return
rescue StandardError => e
puts PASTEL.red("\nError: #{e.message}")
puts PASTEL.red("Backtrace:\n#{e.backtrace.join("\n")}")
return
end
end
end
# Run the interactive benchmarks
run_interactive_benchmarks