Last active
December 28, 2018 10:28
-
-
Save hartator/de35b366c184bb552a76c53c34368709 to your computer and use it in GitHub Desktop.
Source code for Benchmark: Deep directory structure vs. flat directory structure to store millions of files on ext4 article (https://medium.com/@hartator/benchmark-deep-directory-structure-vs-flat-directory-structure-to-store-millions-of-files-on-ext4-cac1000ca28)
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
require 'digest' | |
require 'benchmark' | |
hash = {} | |
10_000_000.times do | |
key = Digest::MD5.hexdigest(rand.to_s) | |
value = Digest::MD5.hexdigest(rand.to_s) | |
hash[key] = value | |
end | |
FileUtils.mkdir_p "dir_flat" | |
FileUtils.mkdir_p "dir_deep" | |
puts Benchmark.measure { | |
hash.each do |key,value| | |
File.write "./dir_flat/#{key}", value | |
end | |
} | |
puts Benchmark.measure { | |
hash.each do |key,value| | |
File.read "./dir_flat/#{key}" | |
end | |
} | |
puts Benchmark.measure { | |
hash.keys.each do |key| | |
dir_path = "./dir_deep/#{key[0..1]}/#{key[2..3]}/" | |
FileUtils.mkdir_p dir_path | |
end | |
} | |
puts Benchmark.measure { | |
hash.each do |key,value| | |
dir_path = "./dir_deep/#{key[0..1]}/#{key[2..3]}/" | |
File.write dir_path + key, value | |
end | |
} | |
puts Benchmark.measure { | |
hash.each do |key,value| | |
dir_path = "./dir_deep/#{key[0..1]}/#{key[2..3]}/" | |
File.read dir_path + key | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment