Class: CodeMetrics::Profiler
- Inherits:
-
Object
- Object
- CodeMetrics::Profiler
- Defined in:
- lib/code_metrics/profiler.rb
Constant Summary collapse
- Error =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path, mode = nil) ⇒ Profiler
constructor
A new instance of Profiler.
- #profile_requires ⇒ Object
Constructor Details
#initialize(path, mode = nil) ⇒ Profiler
Returns a new instance of Profiler.
7 8 9 10 11 12 13 14 |
# File 'lib/code_metrics/profiler.rb', line 7 def initialize(path, mode=nil) assert_ruby_file_exists(path) @path, @mode = path, mode Gem.refresh # H/T https://github.com/pry/pry/blob/b02d0a4863/lib/pry/plugins.rb#L72 Gem::Specification.respond_to?(:each) ? Gem::Specification.all : Gem.source_index require 'benchmark' end |
Instance Attribute Details
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
6 7 8 |
# File 'lib/code_metrics/profiler.rb', line 6 def mode @mode end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/code_metrics/profiler.rb', line 6 def path @path end |
Instance Method Details
#profile_requires ⇒ Object
16 17 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 |
# File 'lib/code_metrics/profiler.rb', line 16 def profile_requires GC.start before_rss = `ps -o rss= -p #{Process.pid}`.to_i if mode require 'ruby-prof' RubyProf.measure_mode = RubyProf.const_get(mode.upcase) RubyProf.start else Object.instance_eval { include RequireProfiler } end elapsed = Benchmark.realtime { require path } results = RubyProf.stop if mode GC.start after_rss = `ps -o rss= -p #{Process.pid}`.to_i if mode if printer = ARGV.shift RubyProf.const_get("#{printer.to_s.classify}Printer").new(results).print($stdout) elsif RubyProf.const_defined?(:CallStackPrinter) File.open("#{File.basename(path, '.rb')}.#{mode}.html", 'w') do |out| RubyProf::CallStackPrinter.new(results).print(out) end else File.open("#{File.basename(path, '.rb')}.#{mode}.callgrind", 'w') do |out| RubyProf::CallTreePrinter.new(results).print(out) end end end RequireProfiler.stats.each do |file, depth, sec| if sec puts "%8.1f ms %s%s" % [sec * 1000, ' ' * depth, file] else puts "#{' ' * (13 + depth)}#{file}" end end puts "%8.1f ms %d KB RSS" % [elapsed * 1000, after_rss - before_rss] end |