Module: RequiredTime

Defined in:
lib/build/required_time.rb

Overview

helper class from gist.github.com/defunkt/278994

Class Method Summary collapse

Class Method Details

.benchmark_require(path, full_backtrace, &block) ⇒ Object



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
# File 'lib/build/required_time.rb', line 19

def benchmark_require(path, full_backtrace, &block)
  output = nil
  backtrace = full_backtrace.reject {|x| x =~ /require|dependencies/ }
  caller = File.expand_path(backtrace[0].split(":")[0])
  parent = required_files.find {|f| f[:fullpath] == caller }
  unless parent
    parent = {
        :index => required_files.size,
        :fullpath => caller,
        :parent => nil,
        :is_root => true
    }
    required_files << parent
  end
  fullpath = find_file(path)
  expanded_path = path; expanded_path = File.expand_path(path) if path =~ /^\//
  new_file = {
      :index => required_files.size,
      :xpath => path,
      :path => expanded_path,
      :fullpath => fullpath,
      :backtrace => full_backtrace,
      :parent => parent,
      :is_root => false
  }
  # add this before the required is required so that anything that is required
  # within the required that's about to be required already has a parent present
  required_files << new_file
  start = Time.now

  output = yield # do the require here

  new_file[:time] = Time.now.to_f - start.to_f
  output
end

.generate_benchmark_reportObject



56
57
58
59
# File 'lib/build/required_time.rb', line 56

def generate_benchmark_report
  @printed_files = []
  generate_benchmark_report_level(@required_files.select {|file| file[:is_root] })
end

.hookObject



5
6
7
8
9
10
11
12
13
# File 'lib/build/required_time.rb', line 5

def hook
  Kernel.class_eval do
    alias_method :__require_benchmarking_old_require, :require
    def require(path, *args)
      RequiredTime.benchmark_require(path, caller) { __require_benchmarking_old_require(path, *args) }
    end
  end
  @hooked = true
end

.hooked?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/build/required_time.rb', line 15

def hooked?
  @hooked
end