Last active
July 20, 2023 10:28
-
-
Save inouire/784eba4d52717e49767759839aaa5f21 to your computer and use it in GitHub Desktop.
A simple way to have a rough idea of where the time is spent for Ruby require
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$requires_index = {} | |
if true | |
# Override the require method to track load times | |
def custom_require(file) | |
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC) | |
result = original_require(file) | |
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC) | |
elapsed = ending - starting | |
$requires_index[file] ||= {:count => 0, :elapsed => 0.0} | |
$requires_index[file][:count] += 1 | |
$requires_index[file][:elapsed] += elapsed | |
if elapsed >= 0.01 | |
puts "Required #{file} in #{elapsed} seconds" | |
end | |
result | |
end | |
# Replace the original 'require' method with our custom implementation | |
alias original_require require | |
alias require custom_require | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment