From e63121700ad3bd1d7120df32528037aae51e5a13 Mon Sep 17 00:00:00 2001 From: "Peter Schilling (aider)" Date: Sun, 8 Sep 2024 07:41:30 -0700 Subject: [PATCH] aider: fix: Handle division by zero error in benchmark script --- benchmark/cool_id_benchmark.rb | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/benchmark/cool_id_benchmark.rb b/benchmark/cool_id_benchmark.rb index 1ade7d2..b2312a0 100644 --- a/benchmark/cool_id_benchmark.rb +++ b/benchmark/cool_id_benchmark.rb @@ -110,7 +110,7 @@ def prepare_sample_ids(count) big_int: BigIntUser.pluck(:id).sample(count), uuid: UuidUser.pluck(:id).sample(count), cool_id: CoolIdUser.pluck(:id).sample(count) - } + }.transform_values { |ids| ids.compact } end # Benchmark queries @@ -118,14 +118,18 @@ def run_benchmark(iterations, sample_ids) Benchmark.bm(20) do |x| [:big_int, :uuid, :cool_id].each do |id_type| x.report("#{id_type.to_s.capitalize} Query:") do - iterations.times do |i| - case id_type - when :big_int - BigIntUser.joins(:big_int_profile).where(id: sample_ids[id_type][i % sample_ids[id_type].size]).first - when :uuid - UuidUser.joins(:uuid_profile).where(id: sample_ids[id_type][i % sample_ids[id_type].size]).first - when :cool_id - CoolIdUser.joins(:cool_id_profile).where(id: sample_ids[id_type][i % sample_ids[id_type].size]).first + if sample_ids[id_type].empty? + puts "No sample IDs for #{id_type}. Skipping benchmark." + else + iterations.times do |i| + case id_type + when :big_int + BigIntUser.joins(:big_int_profile).where(id: sample_ids[id_type].sample).first + when :uuid + UuidUser.joins(:uuid_profile).where(id: sample_ids[id_type].sample).first + when :cool_id + CoolIdUser.joins(:cool_id_profile).where(id: sample_ids[id_type].sample).first + end end end end @@ -143,10 +147,10 @@ def clean_up_data # Parse command-line arguments for sample data size and iterations sample_size = ARGV[0] ? ARGV[0].to_i : 10_000 -iterations = ARGV[1] ? ARGV[1].to_i : 10_000 +iterations = ARGV[1] ? ARGV[1].to_i : [10_000, sample_size].min -# Ensure iterations is not larger than sample size -iterations = [iterations, sample_size].min +# Ensure iterations is not larger than sample size and at least 1 +iterations = [[iterations, sample_size].min, 1].max # Main execution clean_up_data