LoginSignup
11
12

More than 5 years have passed since last update.

Fluentd の debug_agent 経由で ruby-prof を起動する

Last updated at Posted at 2014-08-01

Fluentd の debug_agent (druby) 経由で RubyProf.start/stop できるようにして、任意のタイミングで外からプロファイラを起動/終了できるようにしたい。

Fluentd の設定

ruby-prof gem を入れておく

$ fluent-gem install ruby-prof

debug_agent を起動するようにしておく

# fluent.conf
<source>
  type debug_agent
  bind 127.0.0.1
  port 24230
</source>

fluent-debug で外からつなぐ

$ /usr/lib/fluent/ruby/bin/fluent-debug -p 24230

irb に次のコードを打ち込んで RubyProf を start できる

code = <<-CODE
require 'ruby-prof'
RubyProf.start
CODE
Engine.method_missing(:instance_eval, code)

終了させ、同時にファイルに吐き出すには次のようにする

code = <<-CODE
result = RubyProf.stop
File.open('/tmp/foo.txt', 'w') {|f|
  RubyProf::FlatPrinter.new(result).print(f)
}
CODE
Engine.method_missing(:instance_eval, code)

fluent-rubyprof

上記のコードを毎回コピペするのも馬鹿らしいので、fluent-rubyprof というコマンドを作った。 => https://github.com/sonots/fluent-rubyprof

$ fluent-gem install fluent-rubyprof

してもらって

$ fluent-rubyprof start -p 24230

で RubyProf を start して、

$ fluent-rubyprof stop -p 24230 -o /tmp/fluent-rubyprof.txt

で停止&出力できる。

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12