#!/usr/bin/env python3 from glob import glob from os import lstat,mkdir from random import randint from os.path import isdir,exists from pathlib import Path from time import sleep from subprocess import run from multiprocessing import Pool def drop_caches(): with open('/proc/sys/vm/drop_caches','w') as f: f.write('3') def enable_bfq(): with open('/sys/block/sda/queue/scheduler','w') as f: f.write('bfq') def sync(): run(('sync')) def prepare_tree(name): def populate(dir, depth=6): if not depth: return for fname in range(1,20): if randint(0,100) > 80: dirname = "{}{}/".format(dir,fname) mkdir(dirname) populate(dirname, depth - 1) continue fname = "{}{}".format(dir, fname) Path(fname).touch(exist_ok=True) if not isdir(name): mkdir(name) if not name.endswith('/'): name = '{}/'.format(name) populate(name) def traverse(dir): drop_caches() for inode in glob("{}/*".format(dir)): if isdir(inode): traverse(inode) else: lstat(inode) if randint(0,10) > 6: sleep(0) def main(): nproc = 16 dirname = 'testdir' if not exists(dirname): prepare_tree(dirname) sync() drop_caches() enable_bfq() drop_caches() with Pool(nproc) as pool: dirs = (dirname,) * nproc pool.map(traverse,dirs) main()