51 lines
1.6 KiB
Python
Executable File
51 lines
1.6 KiB
Python
Executable File
#!/usr/bin/python -u
|
|
"""Create sparse files for testing with various patterns."""
|
|
import datetime
|
|
import pathlib
|
|
import os
|
|
import string
|
|
import sys
|
|
|
|
PAGE_SIZE = 4096
|
|
PAGE_HEADER = "<<<Start Of Page>>>\n"
|
|
PAGE_FOOTER = "\n<<<End Of Page>>>\n"
|
|
|
|
line = string.ascii_letters + string.digits + "\n"
|
|
data_len = PAGE_SIZE - len(PAGE_HEADER + PAGE_FOOTER)
|
|
(n_lines, n_chars) = divmod(data_len, len(line))
|
|
page_data = PAGE_HEADER + (line * n_lines) + line[:n_chars] + PAGE_FOOTER
|
|
PAGE_DATA = page_data.encode()
|
|
|
|
FILE_BASE = pathlib.Path(sys.argv[1])
|
|
FILE_SIZE = int(sys.argv[2])
|
|
FILE_SIZE_BYTES = FILE_SIZE * 1024 * 1024
|
|
FILE_N_PAGES = FILE_SIZE_BYTES // PAGE_SIZE
|
|
|
|
|
|
def allocate_testfile(name: str, n_pages: int, hole: bool) -> None:
|
|
"""Allocate a single test file."""
|
|
file = FILE_BASE / f"{FILE_SIZE}M-{name}"
|
|
if file.exists():
|
|
return
|
|
print(f"Allocating file: {file.stem} ...", end="")
|
|
start = datetime.datetime.now()
|
|
with open(file, 'wb') as f:
|
|
f.truncate(FILE_SIZE_BYTES)
|
|
for chunk in range(FILE_N_PAGES // n_pages):
|
|
if hole:
|
|
f.seek(PAGE_SIZE * n_pages, os.SEEK_CUR)
|
|
else:
|
|
f.write(PAGE_DATA * n_pages)
|
|
hole = not hole
|
|
os.fsync(f)
|
|
tdelta = datetime.datetime.now() - start
|
|
time = float(f"{tdelta.seconds}.{tdelta.microseconds}")
|
|
print(f" [Done: {round(time, 2):.2f}s]")
|
|
|
|
|
|
allocate_testfile("data", FILE_N_PAGES, False)
|
|
allocate_testfile("hole", FILE_N_PAGES, True)
|
|
for chunk in [int(c) for c in sys.argv[3].split(",")]:
|
|
allocate_testfile(f"mixed-{chunk}d", chunk, False)
|
|
allocate_testfile(f"mixed-{chunk}h", chunk, True)
|