Created
March 23, 2012 20:14
-
-
Save aarvay/2174519 to your computer and use it in GitHub Desktop.
A small test to measure require's performance
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
# When requiring 10K files ruby slows down like shit. | |
# Ruby before loading a file wants to make sure it's not already require'd. | |
# To do that it loops through a variable called $" and checks if this file is require'd. | |
class FileRequiredTest | |
PATH = "./hackathon/test/gen" | |
def main | |
`mkdir -p #{PATH} &2>1` | |
generate_same_file | |
start = Time.now | |
run | |
same_file = Time.now - start | |
puts "Requiring the same file 10K times ran in #{same_file} seconds." | |
generate_diff_files | |
start = Time.now | |
run | |
diff_file = Time.now - start | |
puts "Requiring 10K diff empty files ran in #{diff_file} seconds." | |
if diff_file > 2*same_file | |
puts "FAILED: 10K file requires takes too long." | |
else | |
puts "PASSED." | |
end | |
end | |
def write_file(path, contents) | |
fh = File.new(path, 'w') | |
fh.write(contents) | |
fh.close | |
end | |
def generate_same_file | |
main = "" | |
10000.times do |i| | |
f = "%05d" % 0 | |
main += "require '#{PATH}/#{f}'\n" | |
end | |
write_file("#{PATH}/main.rb", main) | |
end | |
def generate_diff_files | |
main = "" | |
10000.times do |i| | |
f = "%05d" % i | |
write_file("#{PATH}/#{f}.rb", "") | |
main += "require '#{PATH}/#{f}'\n" | |
end | |
write_file("#{PATH}/main.rb", main) | |
end | |
def run | |
res = `./ruby #{PATH}/main.rb &2>1` | |
raise "Error: #{res}" if res != "" | |
end | |
end | |
FileRequiredTest.new.main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment