Last active
August 23, 2022 11:54
-
-
Save shikendon/8ea80f1132171e6fa14abc95725f55f3 to your computer and use it in GitHub Desktop.
Used in RubyConf Taiwan 2016 https://www.slideshare.net/shikendon/crawler-from-zero
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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