module Kernel
Monkey patch kernel to ensure that all `require` calls call the same method
Constants
- REQUIRE_STACK
Public Instance Methods
load(file, wrap = false)
click to toggle source
# File lib/derailed_benchmarks/core_ext/kernel_require.rb, line 22 def load(file, wrap = false) measure_memory_impact(file) do |file| original_load(file) end end
Also aliased as: original_load
require(file)
click to toggle source
# File lib/derailed_benchmarks/core_ext/kernel_require.rb, line 28 def require(file) measure_memory_impact(file) do |file| original_require(file) end end
Also aliased as: original_require
require_relative(file)
click to toggle source
# File lib/derailed_benchmarks/core_ext/kernel_require.rb, line 34 def require_relative(file) if Pathname.new(file).absolute? require file else require File.expand_path("../#{file}", caller_locations(1, 1)[0].absolute_path) end end
Also aliased as: original_require_relative
Private Instance Methods
measure_memory_impact(file, &block)
click to toggle source
The core extension we use to measure require time of all requires When a file is required we create a tree node with its file name. We then push it onto a stack, this is because requiring a file can require other files before it is finished.
When a child file is required, a tree node is created and the child file is pushed onto the parents tree. We then repeat the process as child files may require additional files.
When a require returns we remove it from the require stack so we don't accidentally push additional children nodes to it. We then store the memory cost of the require in the tree node.
# File lib/derailed_benchmarks/core_ext/kernel_require.rb, line 56 def measure_memory_impact(file, &block) mem = GetProcessMem.new node = DerailedBenchmarks::RequireTree.new(file) parent = REQUIRE_STACK.last parent << node REQUIRE_STACK.push(node) begin before = mem.mb block.call file ensure REQUIRE_STACK.pop # node after = mem.mb end node.cost = after - before end