Skip to content

Commit

Permalink
Add example benchmark for job throughput (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon authored Jun 23, 2021
1 parent e750696 commit f7df44a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ AllCops:
- pkg/**/*
- spec/test_app/**/*
- vendor/**/*
- scripts/**/*
NewCops: enable

Layout/EmptyLineAfterMagicComment:
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gem 'pg', platforms: [:mri, :mingw, :x64_mingw]
gem 'rails'

platforms :ruby do
gem "activerecord-explain-analyze"
gem "memory_profiler"
gem "pry-byebug"
gem "rbtrace"
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ GEM
activerecord (6.1.3.2)
activemodel (= 6.1.3.2)
activesupport (= 6.1.3.2)
activerecord-explain-analyze (0.1.0)
activerecord (>= 4)
pg
activerecord-jdbc-adapter (61.0-java)
activerecord (~> 6.1.0)
activerecord-jdbcpostgresql-adapter (61.0-java)
Expand Down Expand Up @@ -107,6 +110,7 @@ GEM
async (~> 1.14)
async-pool (0.3.6)
async (~> 1.25)
benchmark-ips (2.8.4)
better_html (1.0.16)
actionview (>= 4.0)
activesupport (>= 4.0)
Expand Down Expand Up @@ -373,8 +377,10 @@ PLATFORMS
universal-java-11

DEPENDENCIES
activerecord-explain-analyze
activerecord-jdbcpostgresql-adapter
appraisal!
benchmark-ips (= 2.8.4)
capybara
database_cleaner
dotenv
Expand Down
1 change: 1 addition & 0 deletions good_job.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "thor", ">= 0.14.1"
spec.add_dependency "zeitwerk", ">= 2.0"

spec.add_development_dependency "benchmark-ips", "2.8.4" # https://github.com/evanphx/benchmark-ips/pull/115
spec.add_development_dependency "capybara"
spec.add_development_dependency "database_cleaner"
spec.add_development_dependency "dotenv"
Expand Down
51 changes: 51 additions & 0 deletions scripts/benchmark_job_throughput.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# To run:
# bundle exec ruby scripts/benchmark_example.rb
#

ENV['GOOD_JOB_EXECUTION_MODE'] = 'external'

require_relative '../spec/test_app/config/environment'
require_relative '../lib/good_job'
require 'benchmark/ips'
require 'pry'

booleans = [true, false]
priorities = (1..10).to_a
scheduled_minutes = (-60..60).to_a

GoodJob::Job.delete_all
puts "Seeding database"
jobs_data = Array.new(10_000) do |i|
puts "Initializing seed record ##{i}" if (i % 1_000).zero?
{
queue_name: 'default',
priority: priorities.sample,
scheduled_at: booleans.sample ? scheduled_minutes.sample.minutes.ago : nil,
created_at: 90.minutes.ago,
updated_at: 90.minutes.ago,
finished_at: booleans.sample ? scheduled_minutes.sample.minutes.ago : nil,
serialized_params: {},
}
end
puts "Inserting seed records into the database...\n"
GoodJob::Job.insert_all(jobs_data)

# ActiveRecord::Base.connection.execute('SET enable_seqscan = OFF')
# puts GoodJob::Job.unfinished.priority_ordered.only_scheduled(use_coalesce: true).limit(1).advisory_lock.explain(analyze: true)
# exit!

Benchmark.ips do |x|
x.report("with priority") do
GoodJob::Job.unfinished.priority_ordered.only_scheduled(use_coalesce: true).limit(1).with_advisory_lock do |good_jobs|
# good_jobs.first&.destroy!
end
end

x.report("without priority") do
GoodJob::Job.unfinished.only_scheduled(use_coalesce: true).limit(1).with_advisory_lock do |good_jobs|
# good_jobs.first&.destroy!
end
end

x.compare!
end
File renamed without changes.

0 comments on commit f7df44a

Please sign in to comment.