Module: RequireProf

Defined in:
lib/require-prof.rb

Constant Summary collapse

(print_live = ENV['RUBY_REQUIRE_PRINT_LIVE']) && (print_live != 'false')
@@orig_require =
method(:require)
@@orig_load =
method(:load)
@@global_start =
Time.now
@@level =
0
@@lower_level_progress =
0
@@timing_info =
[]

Class Method Summary collapse

Class Method Details

.backend_requiring(orig_method, name, args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/require-prof.rb', line 11

def self.backend_requiring(orig_method, name, args)
  initial_lower_level_progress = @@lower_level_progress
  spacing = ' ' * @@level
  start_at = Time.now
  cumulative_duration = start_at - @@global_start

  if @@print_live
    $stderr.puts "#{spacing}[#{cumulative_duration}s] BEGIN #{name} #{args.inspect}..."
  end

  @@level += 1
  orig_method.call(*args)
  @@level -= 1

  end_at = Time.now
  cumulative_duration = end_at - @@global_start
  total_duration = end_at - start_at
  my_duration = total_duration - (@@lower_level_progress - initial_lower_level_progress)

  if @@print_live
    print_timing_entry(my_duration, total_duration, cumulative_duration, spacing, "END #{name}", args)
  end

  @@timing_info << [my_duration, total_duration, cumulative_duration, spacing, name, args]
  @@lower_level_progress += my_duration
end

.load(*args) ⇒ Object



42
43
44
# File 'lib/require-prof.rb', line 42

def self.load(*args)
  backend_requiring(@@orig_load, 'loading', args)
end


53
54
55
56
# File 'lib/require-prof.rb', line 53

def self.print_timing_infos
  @@timing_info.each { |entry| print_timing_entry(*entry) }
  nil
end


46
47
48
49
50
51
# File 'lib/require-prof.rb', line 46

def self.print_timing_infos_for_optimization
  @@timing_info.sort_by { |timing| timing.first }.each do |my_duration, _, _, _, name, args|
    $stderr.puts "#{my_duration} -- #{name} #{args.inspect}"
  end
  nil
end

.require(*args) ⇒ Object



38
39
40
# File 'lib/require-prof.rb', line 38

def self.require(*args)
  backend_requiring(@@orig_require, 'requiring', args)
end