2015-10-01 19:37:58 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
# Quiet some warnings we see when running in warning mode:
|
|
|
|
# RUBYOPT=-w bundle exec sidekiq
|
|
|
|
$TESTING = false
|
|
|
|
|
2022-01-26 18:33:23 -05:00
|
|
|
# require "ruby-prof"
|
|
|
|
require "bundler/setup"
|
2019-09-10 07:06:26 -04:00
|
|
|
Bundler.require(:default, :load_test)
|
2015-10-07 17:27:47 -04:00
|
|
|
|
2022-01-26 18:33:23 -05:00
|
|
|
require_relative "../lib/sidekiq/cli"
|
|
|
|
require_relative "../lib/sidekiq/launcher"
|
2015-10-01 19:37:58 -04:00
|
|
|
|
|
|
|
Sidekiq.configure_server do |config|
|
2019-08-02 16:04:06 -04:00
|
|
|
config.options[:concurrency] = 10
|
2022-01-26 18:33:23 -05:00
|
|
|
config.redis = {db: 13, port: 6380}
|
|
|
|
# config.redis = { db: 13, port: 6380, driver: :hiredis}
|
|
|
|
config.options[:queues] << "default"
|
2015-10-01 19:37:58 -04:00
|
|
|
config.logger.level = Logger::ERROR
|
|
|
|
config.average_scheduled_poll_interval = 2
|
2015-11-12 17:00:04 -05:00
|
|
|
config.reliable! if defined?(Sidekiq::Pro)
|
2015-10-01 19:37:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class LoadWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
sidekiq_options retry: 1
|
|
|
|
sidekiq_retry_in do |x|
|
|
|
|
1
|
|
|
|
end
|
|
|
|
|
2022-01-26 18:33:23 -05:00
|
|
|
def perform(idx, ts = nil)
|
|
|
|
puts(Time.now.to_f - ts) if !ts.nil?
|
|
|
|
# raise idx.to_s if idx % 100 == 1
|
2015-10-01 19:37:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-07 22:20:17 -04:00
|
|
|
# brew tap shopify/shopify
|
|
|
|
# brew install toxiproxy
|
2019-08-02 16:04:06 -04:00
|
|
|
# run `toxiproxy-server` in a separate terminal window.
|
2022-01-26 18:33:23 -05:00
|
|
|
require "toxiproxy"
|
2015-10-07 22:20:17 -04:00
|
|
|
# simulate a non-localhost network for realer-world conditions.
|
|
|
|
# adding 1ms of network latency has an ENORMOUS impact on benchmarks
|
2019-08-02 16:04:06 -04:00
|
|
|
Toxiproxy.populate([{
|
2022-01-26 18:33:23 -05:00
|
|
|
name: "redis",
|
|
|
|
listen: "127.0.0.1:6380",
|
|
|
|
upstream: "127.0.0.1:6379"
|
2019-08-02 16:04:06 -04:00
|
|
|
}])
|
2015-10-07 22:20:17 -04:00
|
|
|
|
2015-10-01 19:37:58 -04:00
|
|
|
self_read, self_write = IO.pipe
|
2022-01-26 18:33:23 -05:00
|
|
|
%w[INT TERM TSTP TTIN].each do |sig|
|
|
|
|
trap sig do
|
|
|
|
self_write.puts(sig)
|
2015-10-01 19:37:58 -04:00
|
|
|
end
|
2022-01-26 18:33:23 -05:00
|
|
|
rescue ArgumentError
|
|
|
|
puts "Signal #{sig} not supported"
|
2015-10-01 19:37:58 -04:00
|
|
|
end
|
|
|
|
|
2022-01-26 18:33:23 -05:00
|
|
|
Sidekiq.redis { |c| c.flushdb }
|
2015-10-01 19:37:58 -04:00
|
|
|
def handle_signal(launcher, sig)
|
|
|
|
Sidekiq.logger.debug "Got #{sig} signal"
|
|
|
|
case sig
|
2022-01-26 18:33:23 -05:00
|
|
|
when "INT"
|
2015-10-01 19:37:58 -04:00
|
|
|
# Handle Ctrl-C in JRuby like MRI
|
|
|
|
# http://jira.codehaus.org/browse/JRUBY-4637
|
|
|
|
raise Interrupt
|
2022-01-26 18:33:23 -05:00
|
|
|
when "TERM"
|
2019-03-02 12:15:10 -05:00
|
|
|
# Heroku sends TERM and then waits 30 seconds for process to exit.
|
2015-10-01 19:37:58 -04:00
|
|
|
raise Interrupt
|
2022-01-26 18:33:23 -05:00
|
|
|
when "TSTP"
|
2017-01-03 18:41:48 -05:00
|
|
|
Sidekiq.logger.info "Received TSTP, no longer accepting new work"
|
2015-10-06 17:51:06 -04:00
|
|
|
launcher.quiet
|
2022-01-26 18:33:23 -05:00
|
|
|
when "TTIN"
|
2015-10-01 19:37:58 -04:00
|
|
|
Thread.list.each do |thread|
|
2022-01-26 18:33:23 -05:00
|
|
|
Sidekiq.logger.warn "Thread TID-#{(thread.object_id ^ ::Process.pid).to_s(36)} #{thread["label"]}"
|
2015-10-01 19:37:58 -04:00
|
|
|
if thread.backtrace
|
|
|
|
Sidekiq.logger.warn thread.backtrace.join("\n")
|
|
|
|
else
|
|
|
|
Sidekiq.logger.warn "<no backtrace available>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def Process.rss
|
|
|
|
`ps -o rss= -p #{Process.pid}`.chomp.to_i
|
|
|
|
end
|
|
|
|
|
2022-01-26 19:57:43 -05:00
|
|
|
iter = 50
|
2015-10-01 19:37:58 -04:00
|
|
|
count = 10_000
|
|
|
|
|
2015-10-07 17:27:47 -04:00
|
|
|
iter.times do
|
2022-02-07 15:34:05 -05:00
|
|
|
arr = Array.new(count) { |idx| [idx] }
|
2022-01-26 18:33:23 -05:00
|
|
|
Sidekiq::Client.push_bulk("class" => LoadWorker, "args" => arr)
|
2015-10-01 19:37:58 -04:00
|
|
|
end
|
2022-01-26 18:33:23 -05:00
|
|
|
Sidekiq.logger.error "Created #{count * iter} jobs"
|
2015-10-01 19:37:58 -04:00
|
|
|
|
2019-10-01 22:40:02 -04:00
|
|
|
start = Time.now
|
|
|
|
|
2015-10-01 19:37:58 -04:00
|
|
|
Monitoring = Thread.new do
|
2022-01-26 18:33:23 -05:00
|
|
|
while true
|
|
|
|
sleep 0.2
|
|
|
|
qsize = Sidekiq.redis do |conn|
|
|
|
|
conn.llen "queue:default"
|
|
|
|
end
|
|
|
|
total = qsize
|
|
|
|
# Sidekiq.logger.error("RSS: #{Process.rss} Pending: #{total}")
|
|
|
|
if total == 0
|
|
|
|
Sidekiq.logger.error("Done, #{iter * count} jobs in #{Time.now - start} sec")
|
|
|
|
Sidekiq.logger.error("Now here's the latency for three jobs")
|
|
|
|
|
|
|
|
LoadWorker.perform_async(1, Time.now.to_f)
|
|
|
|
LoadWorker.perform_async(2, Time.now.to_f)
|
|
|
|
LoadWorker.perform_async(3, Time.now.to_f)
|
|
|
|
|
2019-10-01 22:40:02 -04:00
|
|
|
sleep 0.2
|
2022-01-26 18:33:23 -05:00
|
|
|
exit(0)
|
2015-10-01 19:37:58 -04:00
|
|
|
end
|
2022-01-26 18:33:23 -05:00
|
|
|
end
|
2015-10-01 19:37:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2022-01-26 18:33:23 -05:00
|
|
|
# RubyProf::exclude_threads = [ Monitoring ]
|
|
|
|
# RubyProf.start
|
2022-01-25 13:04:28 -05:00
|
|
|
events = Sidekiq.options[:lifecycle_events][:startup]
|
|
|
|
events.each(&:call)
|
|
|
|
events.clear
|
|
|
|
|
2019-08-02 16:04:06 -04:00
|
|
|
Sidekiq.logger.error "Simulating 1ms of latency between Sidekiq and redis"
|
|
|
|
Toxiproxy[:redis].downstream(:latency, latency: 1).apply do
|
2015-10-07 19:56:10 -04:00
|
|
|
launcher = Sidekiq::Launcher.new(Sidekiq.options)
|
|
|
|
launcher.run
|
2015-10-01 19:37:58 -04:00
|
|
|
|
2015-10-07 19:56:10 -04:00
|
|
|
while readable_io = IO.select([self_read])
|
|
|
|
signal = readable_io.first[0].gets.strip
|
|
|
|
handle_signal(launcher, signal)
|
|
|
|
end
|
2019-08-02 16:04:06 -04:00
|
|
|
end
|
2015-10-01 19:37:58 -04:00
|
|
|
rescue SystemExit => e
|
2022-01-26 18:33:23 -05:00
|
|
|
# Sidekiq.logger.error("Profiling...")
|
|
|
|
# result = RubyProf.stop
|
|
|
|
# printer = RubyProf::GraphHtmlPrinter.new(result)
|
|
|
|
# printer.print(File.new("output.html", "w"), :min_percent => 1)
|
2015-10-01 19:37:58 -04:00
|
|
|
# normal
|
|
|
|
rescue => e
|
|
|
|
raise e if $DEBUG
|
2022-01-26 18:33:23 -05:00
|
|
|
warn e.message
|
|
|
|
warn e.backtrace.join("\n")
|
2015-10-01 19:37:58 -04:00
|
|
|
exit 1
|
|
|
|
end
|