Module: RequireBench

Defined in:
lib/require_bench.rb,
lib/require_bench/version.rb

Overview

Namespace for this gem

Defined Under Namespace

Modules: Version

Constant Summary collapse

TIMINGS =
Hash.new { |h, k| h[k] = 0.0 }
PRINTER =
Printer.new
INCLUDE_PATTERN =
include_pattern
INCLUDE_TOKENS =
include_tokens
LOG_START =
log_start
NO_GROUP_PATTERN =
no_group_pattern
PREFER_NOT_PATH =
prefer_not_path
RESCUED_CLASSES =
rescued_classes
SKIP_PATTERN =
skip_pattern
TIMEOUT =
timeout
TRACKED_METHODS =
tracked_methods

Class Method Summary collapse

Class Method Details

.consume_with_timing(type, file, *args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/require_bench.rb', line 96

def consume_with_timing(type, file, *args)
  $require_bench_semaphore = true
  short_type = type[0]
  ret = nil
  # Not sure if this is actually a useful abstraction...
  prefix = INCLUDE_TOKENS.detect { |t| File.basename(file).match?(t) } if PREFER_NOT_PATH

  seconds = Benchmark.realtime do
    ret = if RequireBench::TIMEOUT.zero?
            Kernel.send("#{type}_without_timing", file, *args)
          else
            # Raise Timeout::Error if more than RequireBench::TIMEOUT seconds are spent in the block
            # This is a giant hammer, and should probably only be used to figure out where an infinite loop might be hiding.
            Timeout.timeout(RequireBench::TIMEOUT) do
              Kernel.send("#{type}_without_timing", file, *args)
            end
          end
  end
  PRINTER.out_consume(seconds, file, short_type)
  if prefix.nil? && (NO_GROUP_PATTERN.nil? || !NO_GROUP_PATTERN.match?(file))
    # This results in grouping all files with the same leading path part (e.g. "models", or "lib")
    #   into the same timing bucket.
    # requires that were fully qualified paths probably need to be identified
    #   by the full path
    prefix = if (match = INCLUDE_PATTERN&.match(file))
               match[0]
             else
               # Generally this will target a library name, e.g. "rspec"
               #    which sums all require timings from a single library together
               file.partition('/').first
             end
  end
  prefix = file if prefix.nil? || prefix.empty?
  RequireBench::TIMINGS[prefix] += seconds
  ret
ensure
  $require_bench_semaphore = nil
end