Module: Lingo::Debug

Extended by:
Debug
Included in:
Debug
Defined in:
lib/lingo/debug.rb

Constant Summary collapse

PS_COMMAND =
ENV['LINGO_PS_COMMAND'] || '/bin/ps'
PS_COLUMNS =
ENV['LINGO_PS_COLUMNS'] || 'vsz,rss,sz,%mem,%cpu,time,etime,pid'
PS_RE =
File.executable?(PS_COMMAND) ? %r{\A#{ENV['LINGO_DEBUG_PS']}\z} : nil
PS_NO_HEADING =
Hash.seen

Instance Method Summary collapse

Instance Method Details

#profile(base) ⇒ Object



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
# File 'lib/lingo/debug.rb', line 47

def profile(base)
  return yield unless base

  require 'ruby-prof'

  result = RubyProf.profile { yield }
  result.eliminate_methods! [/\b(?:Gem|HighLine)\b/,
    /\A(?:Benchmark|FileUtils|Pathname|Util)\b/]

  if base.is_a?(IO)
    RubyProf::FlatPrinter.new(result).print(base)
  else
    FileUtils.mkdir_p(File.dirname(base))

    mode = ENV['RUBY_PROF_MEASURE_MODE']
    base += "-#{mode}" if mode && !mode.empty?

    {
      txt:   :FlatPrinter,
      lines: :FlatPrinterWithLineNumbers,
      html:  :GraphHtmlPrinter,
      stack: :CallStackPrinter
    }.each { |ext, name|
      File.open("#{base}.#{ext}", 'a+', encoding: ENCODING) { |f|
        RubyProf.const_get(name).new(result).print(f)
      }
    }
  end
end

#ps(name) ⇒ Object



40
41
42
43
44
45
# File 'lib/lingo/debug.rb', line 40

def ps(name)
  system(PS_COMMAND,
    '-o', PS_COLUMNS,
    "--#{'no-' if PS_NO_HEADING[name]}heading",
    Process.pid.to_s) if name =~ PS_RE
end