0
@@ -13,6 +13,44 @@ include Passenger
0
include Passenger::Utils
0
+# A thread or a process, depending on the Ruby VM implementation.
0
+ attr_accessor :channel
0
+ def initialize(name, &block)
0
+ if RUBY_PLATFORM == "java"
0
+ a, b = UNIXSocket.pair
0
+ @thread = Thread.new do
0
+ block.call(true, MessageChannel.new(b))
0
+ @channel = MessageChannel.new(a)
0
+ a, b = UNIXSocket.pair
0
+ @pid = safe_fork(name) do
0
+ block.call(false, MessageChannel.new(b))
0
+ @channel = MessageChannel.new(a)
0
+ if RUBY_PLATFORM == "java"
0
+ Process.kill('SIGKILL', @pid) rescue nil
0
+ Process.waitpid(@pid) rescue nil
0
@options = parse_options
0
@@ -105,30 +143,25 @@ class StressTester
0
# Start crawler processes.
0
GC.start if GC.copy_on_write_friendly?
0
@options[:concurrency].times do |i|
0
- STDOUT.write("Starting crawler #{i + 1} of #{@options[:concurrency]}...\
r")
0
+ STDOUT.write("Starting crawler #{i + 1} of #{@options[:concurrency]}...\
n")
0
- a, b = UNIXSocket.pair
0
- pid = safe_fork("crawler #{i + 1}") do
0
- $0 = "Passenger Crawler #{i + 1}"
0
+ process = Subprocess.new("crawler #{i + 1}") do |is_thread, channel|
0
+ if !is_thread && @options[:nice]
0
system("renice 1 #{Process.pid} >/dev/null 2>/dev/null")
0
- crawl!(i + 1,
MessageChannel.new(b))
0
+ crawl!(i + 1,
channel)
0
- :channel => MessageChannel.new(a),
0
+ :channel => process.channel,
0
@@ -136,19 +169,22 @@ class StressTester
0
+ if RUBY_PLATFORM != "java"
0
+ # 'sleep' b0rks when running in JRuby?
0
$0 = "Passenger Crawler: control process"
0
- @processes.each do |process|
0
- io_to_process[process[:channel].io] = process
0
- ios << process[:channel].io
0
+ @crawlers.each do |crawler|
0
+ io_to_crawler[crawler[:channel].io] = crawler
0
+ ios << crawler[:channel].io
0
- # Tell each crawler process to start crawling.
0
- @processes.each do |process|
0
- process[:channel].write("start")
0
+ # Tell each crawler to start crawling.
0
+ @crawlers.each do |crawler|
0
+ crawler[:channel].write("start")
0
# Show progress periodically.
0
@@ -158,19 +194,18 @@ class StressTester
0
apache_restarter = Thread.new(&method(:restart_apache))
0
@next_app_restart = Time.now + @options[:app_restart_interval] * 60
0
app_restarter = Thread.new(&method(:restart_app))
0
- note_progress(ios, io_to_
process)
0
+ note_progress(ios, io_to_
crawler)
0
puts "Shutting down..."
0
- @processes.each do |process|
0
- STDOUT.write("Stopping crawler #{process[:id]} of #{@options[:concurrency]}...\r")
0
+ @crawlers.each do |crawler|
0
+ STDOUT.write("Stopping crawler #{crawler[:id]} of #{@options[:concurrency]}...\r")
0
- Process.kill('SIGKILL', process[:pid]) rescue nil
0
- Process.waitpid(process[:pid]) rescue nil
0
- process[:channel].close
0
+ crawler[:process].stop
0
progress_reporter.join if progress_reporter
0
apache_restarter.join if apache_restarter
0
@@ -179,13 +214,13 @@ class StressTester
0
- def note_progress(ios, io_to_
process)
0
+ def note_progress(ios, io_to_
crawler)
0
select(ios)[0].each do |io|
0
- process = io_to_process[io]
0
- uri = process[:channel].read[0]
0
- process[:mutex].synchronize do
0
- process[:current_uri] = uri
0
- process[:crawled] += 1
0
+ crawler = io_to_crawler[io]
0
+ uri = crawler[:channel].read[0]
0
+ crawler[:mutex].synchronize do
0
+ crawler[:current_uri] = uri
0
+ crawler[:crawled] += 1
0
@@ -194,12 +229,12 @@ class StressTester
0
output = "\n" * @terminal_height
0
output << "### Running for #{duration(Time.now.to_i - @start_time.to_i)}\n"
0
- @processes.each do |process|
0
- process[:mutex].synchronize do
0
+ @crawlers.each do |crawler|
0
+ crawler[:mutex].synchronize do
0
line = sprintf("Crawler %-2d: %-3d -> %s",
0
- process[:current_uri])
0
+ crawler[:current_uri])
0
output << sprintf("%-#{@terminal_width}s\n", line)
0
@@ -264,10 +299,19 @@ class StressTester
0
channel.write(uri, referer, response)
0
- Process.kill('SIGKILL', Process.pid)
0
+ if RUBY_PLATFORM == "java"
0
+ Thread.current.terminate
0
+ Process.kill('SIGKILL', Process.pid)
0
crawler = Hawler.new(@options[:host], progress_reporter)
0
+ if RUBY_PLATFORM == "java"
0
+ raise Interrupt, "Interrupted"
0
crawler.depth = @options[:depth]
Comments
No one has commented yet.