Created
December 15, 2015 21:46
-
-
Save temochka/4eca3aff430d40e62ec6 to your computer and use it in GitHub Desktop.
Generates a directory with X files with name length A spread by Y directories with name length A with depth Z
This file contains hidden or 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 'securerandom' | |
require 'fileutils' | |
NUM_FILES = 35000 | |
NUM_DIRECTORIES = 5000 | |
NAME_LENGTH = 100 | |
MAX_DIR_DEPTH = 10 | |
MAX_FILES_PER_DIR = 100 | |
MAX_DIRS_PER_DIR = 100 | |
def random_name | |
SecureRandom.hex(NAME_LENGTH / 2) | |
end | |
class GenDir | |
attr_reader :parent | |
def self.create | |
self.new(random_name) | |
end | |
def initialize(name, parent = nil) | |
@name = name | |
@files_count = 0 | |
@dirs_count = 0 | |
@parent = parent | |
FileUtils.mkdir_p(name) | |
end | |
def generate_file | |
return false if @files_count >= MAX_FILES_PER_DIR | |
File.open(File.join(@name, random_name), 'w') do |f| | |
f << 'foobar' | |
end | |
@files_count += 1 | |
end | |
def generate_subdir | |
return false if @dirs_count >= MAX_DIRS_PER_DIR | |
self.class.new(File.join(@name, random_name), self) | |
end | |
end | |
directories = Enumerator.new do |y| | |
num_directories = 0 | |
root = current = GenDir.new('.') | |
y << current | |
while current && num_directories < NUM_DIRECTORIES do | |
if following = current.generate_subdir | |
num_directories += 1 | |
y << following | |
elsif current.parent | |
y << current.parent | |
else | |
current = nil | |
end | |
end | |
end | |
cwd = directories.next | |
NUM_FILES.times do | |
unless cwd.generate_file | |
cwd = directories.next | |
raise 'Ran out of directories to write files to :(' unless cwd.generate_file | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment