require 'optparse' require 'drb/drb' module Fluent class Rubyprof PRINTERS = { 'flat' => 'FlatPrinter', 'flat_with_line_numbers' => 'FlatPrinterWithLineNumbers', 'graph' => 'GraphPrinter', 'graph_html' => 'GraphHtmlPrinter', 'call_tree' => 'CallTreePrinter', 'call_stack' => 'CallStackPrinter', 'dot' => 'DotPrinter', 'multi' => 'MultiPrinter', } def parse_options(argv = ARGV) op = OptionParser.new op.banner += ' [output_file]' (class< e usage e.message end unless opts[:unix].nil? uri = "drbunix:#{opts[:unix]}" else uri = "druby://#{opts[:host]}:#{opts[:port]}" end $remote_engine = DRb::DRbObject.new_with_uri(uri) case opts[:command] when 'start' remote_code = <<-CODE require 'ruby-prof' RubyProf.measure_mode = eval("RubyProf::#{opts[:measure_mode]}") RubyProf.start CODE when 'stop' remote_code = <<-"CODE" result = RubyProf.stop File.open('#{opts[:output]}', 'w') {|f| RubyProf::#{PRINTERS[opts[:printer]]}.new(result).print(f) } CODE end $remote_engine.method_missing(:instance_eval, remote_code) case opts[:command] when 'start' $stdout.puts 'fluent-rubyprof: started' when 'stop' $stdout.puts "fluent-rubyprof: outputs to #{opts[:output]}" end end end end