Class: Fluent::Rubyprof

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/rubyprof.rb

Constant Summary collapse

PRINTERS =
{
  'flat' => 'FlatPrinter',
  'flat_with_line_numbers' => 'FlatPrinterWithLineNumbers',
  'graph' => 'GraphPrinter',
  'graph_html' => 'GraphHtmlPrinter',
  'call_tree' => 'CallTreePrinter',
  'call_stack' => 'CallStackPrinter',
  'dot' => 'DotPrinter',
  'multi' => 'MultiPrinter',
}

Instance Method Summary collapse

Instance Method Details

#parse_options(argv = ARGV) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
71
72
73
74
75
76
77
78
# File 'lib/fluent/rubyprof.rb', line 18

def parse_options(argv = ARGV)
  op = OptionParser.new
  op.banner += ' <start/stop> [output_file]'

  (class<<self;self;end).module_eval do
    define_method(:usage) do |msg|
      puts op.to_s
      puts "error: #{msg}" if msg
      exit 1
    end
  end

  opts = {
    host: '127.0.0.1',
    port: 24230,
    unix: nil,
    command: nil, # start or stop
    output: '/tmp/fluent-rubyprof.txt',
    measure_mode: 'PROCESS_TIME',
    printer: 'flat',
  }

  op.on('-h', '--host HOST', "fluent host (default: #{opts[:host]})") {|v|
    opts[:host] = v
  }

  op.on('-p', '--port PORT', "debug_agent tcp port (default: #{opts[:host]})", Integer) {|v|
    opts[:port] = v
  }

  op.on('-u', '--unix PATH', "use unix socket instead of tcp") {|v|
    opts[:unix] = v
  }

  op.on('-o', '--output PATH', "output path (default: #{opts[:output]})") {|v|
    opts[:output] = v
  }

  op.on('-m', '--measure_mode MEASURE_MODE', "ruby-prof measure mode (default: #{opts[:measure_mode]})") {|v|
    opts[:measure_mode] = v
  }

  op.on('-P', '--printer PRINTER', PRINTERS.keys,
            "ruby-prof print format (default: #{opts[:printer]})",
            "currently one of: #{PRINTERS.keys.join(', ')}") {|v|
    opts[:printer] = v
  }
  op.parse!(argv)

  opts[:command] = argv.shift
  unless %w[start stop].include?(opts[:command])
    raise OptionParser::InvalidOption.new("`start` or `stop` must be specified as the 1st argument")
  end

  measure_modes = %w[PROCESS_TIME WALL_TIME CPU_TIME ALLOCATIONS MEMORY GC_RUNS GC_TIME] 
  unless measure_modes.include?(opts[:measure_mode])
    raise OptionParser::InvalidOption.new("-m allows one of #{measure_modes.join(', ')}")
  end

  opts
end

#runObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fluent/rubyprof.rb', line 80

def run
  begin
    opts = parse_options
  rescue OptionParser::InvalidArgument, OptionParser::InvalidOption => 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 = "    require 'ruby-prof'\n    RubyProf.measure_mode = eval(\"RubyProf::\#{opts[:measure_mode]}\")\n    RubyProf.start\n    CODE\n  when 'stop'\n    remote_code = <<-\"CODE\"\n    result = RubyProf.stop\n    File.open('\#{opts[:output]}', 'w') {|f|\n      RubyProf::\#{PRINTERS[opts[:printer]]}.new(result).print(f)\n    }\n    CODE\n  end\n\n  $remote_engine.method_missing(:instance_eval, remote_code)\n\n  case opts[:command]\n  when 'start'\n    $stdout.puts 'fluent-rubyprof: started'\n  when 'stop'\n    $stdout.puts \"fluent-rubyprof: outputs to \#{opts[:output]}\"\n  end\nend\n"