Class: ABProf::ABWorker
- Inherits:
-
Object
- Object
- ABProf::ABWorker
- Defined in:
- lib/abprof.rb
Overview
This class is used by programs that are being profiled. It’s necessarily a singleton since it needs to control STDIN. The bare mode can do without it, but it’s needed for harness processes.
Class Method Summary collapse
- .debug(string) ⇒ Object
- .iteration(&block) ⇒ Object
- .iteration_with_return_value(&block) ⇒ Object
- .n_iterations_with_return_value(&block) ⇒ Object
- .read_once ⇒ Object
- .run_n(n) ⇒ Object
- .start ⇒ Object
Instance Method Summary collapse
Class Method Details
.debug(string) ⇒ Object
25 26 27 |
# File 'lib/abprof.rb', line 25 def self.debug string STDERR.puts(string) if ENV['ABDEBUG'] == "true" end |
.iteration(&block) ⇒ Object
29 30 31 32 |
# File 'lib/abprof.rb', line 29 def self.iteration(&block) @iter_block = block @return = :none end |
.iteration_with_return_value(&block) ⇒ Object
34 35 36 37 |
# File 'lib/abprof.rb', line 34 def self.iteration_with_return_value(&block) @iter_block = block @return = :per_iteration end |
.n_iterations_with_return_value(&block) ⇒ Object
39 40 41 42 |
# File 'lib/abprof.rb', line 39 def self.n_iterations_with_return_value(&block) @iter_block = block @return = :per_n_iterations end |
.read_once ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/abprof.rb', line 72 def self.read_once debug "WORKER #{Process.pid}: read loop" @input ||= "" @input += (STDIN.gets || "") debug "WORKER #{Process.pid}: Input #{@input.inspect}" if @input["\n"] command, @input = @input.split("\n", 2) debug "WORKER #{Process.pid}: command: #{command.inspect}" if command == "QUIT" exit 0 elsif command["ITERS"] iters = command[5..-1].to_i values = run_n iters STDOUT.flush # Why does this synchronous file descriptor not flush when given a string with a newline? Ugh! debug "WORKER #{Process.pid}: finished command ITERS: OK" else STDERR.puts "Unrecognized ABProf command: #{command.inspect}!" exit -1 end end end |
.run_n(n) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/abprof.rb', line 44 def self.run_n(n) debug "WORKER #{Process.pid}: running #{n} times [#{@return.inspect}]" case @return when :none n.times do @iter_block.call end STDOUT.write "OK\n" when :per_iteration values = (0..(n-1)).map { |i| @iter_block.call.to_f } STDOUT.write "VALUES #{values.inspect}\n" when :per_n_iterations value = @iter_block.call(n) if value.respond_to?(:each) # Return array of numbers debug "WORKER #{Process.pid}: Sent to controller: VALUES #{value.to_a.inspect}" STDOUT.write "VALUES #{value.to_a.inspect}\n" else # Return single number debug "WORKER #{Process.pid}: Sent to controller: VALUE #{value.to_f}" STDOUT.write "VALUE #{value.to_f}\n" end else raise "Unknown @return value #{@return.inspect} inside abprof!" end end |
.start ⇒ Object
94 95 96 97 98 |
# File 'lib/abprof.rb', line 94 def self.start loop do read_once end end |
Instance Method Details
#debug(string) ⇒ Object
22 23 24 |
# File 'lib/abprof.rb', line 22 def debug string STDERR.puts(string) if ENV['ABDEBUG'] == "true" end |