Skip to content

Commit

Permalink
Benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
gremerritt committed Sep 21, 2023
1 parent 4fda31a commit c31f493
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/delayed/job_groups/job_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class JobGroup < ActiveRecord::Base

scope :ready, -> { where(queueing_complete: true, blocked: false) }
scope :with_no_open_jobs, -> { left_joins(:active_jobs).group(:id).having('count(delayed_jobs.id) == 0') }
scope :with_no_open_jobs_exists, -> do
where("not exists (#{Delayed::Job.where('delayed_jobs.job_group_id = delayed_job_groups.id').to_sql})")
end

def mark_queueing_complete
with_lock do
Expand Down
41 changes: 41 additions & 0 deletions spec/delayed/job_groups/job_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,47 @@
expect(described_class.with_no_open_jobs).to match_array(job_group_without_jobs)
end
end

describe "with_no_open_jobs_exists" do
let!(:job_group_with_jobs) { create(:delayed_job).job_group }
let!(:job_group_without_jobs) { subject }

it "returns groups with no jobs" do
expect(described_class.with_no_open_jobs_exists).to match_array(job_group_without_jobs)
end
end

describe "perf" do
let(:iterations) { 1000 }
let(:num_job_groups) { 1_000 }

before do
ActiveSupport::Deprecation.silenced = true
require 'benchmark'
@zeros = 0

num_job_groups.times do |i|
puts "Creating job group # #{i}" if i % 100 == 0
job_group = create(:job_group)
n = Integer(rand.round(1) * 100)
if n.zero?
@zeros += 1
else
create_list(:delayed_job, n, job_group: job_group)
end
end
end

it "benchmark" do
Benchmark.bm do |bench|
bench.report(:having) { iterations.times { described_class.with_no_open_jobs.to_a } }
bench.report(:exists) { iterations.times { described_class.with_no_open_jobs_exists.to_a } }
end

expect(described_class.with_no_open_jobs.to_a.size).to eq @zeros
expect(described_class.with_no_open_jobs_exists.to_a.size).to eq @zeros
end
end
end

shared_examples "the job group was completed" do
Expand Down

0 comments on commit c31f493

Please sign in to comment.