Skip to content

Instantly share code, notes, and snippets.

@shikendon
Last active August 23, 2022 11:54
Show Gist options
  • Save shikendon/8ea80f1132171e6fa14abc95725f55f3 to your computer and use it in GitHub Desktop.
Save shikendon/8ea80f1132171e6fa14abc95725f55f3 to your computer and use it in GitHub Desktop.
# lib/tasks/benchmark.rake
namespace :benchmark do
task run_sidekiq: :environment do
batch = Sidekiq::Batch.new
batch.on(:complete, TestWorker)
batch.jobs do
200.times do
TestWorker.perform_async
end
end
end
end
# app/workers/test_worker.rb
class TestWorker
include Sidekiq::Worker
def perform
response = HTTParty.get("https://www.facebook.com/")
doc = Nokogiri::HTML(response.body)
title = doc.css("title").text
ProjectLog.create!(title: title)
ensure
ActiveRecord::Base.clear_active_connections!
end
def on_complete(status, options)
time_elapsed = Time.now.utc - status.created_at
puts "Elapsed time: #{time_elapsed} seconds"
ensure
ActiveRecord::Base.clear_active_connections!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment