2025-03-24 00:12:11 +03:00

59 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import shutil
import time
from pathlib import Path
# Настройки теста
DEPTH = 3 # Глубина вложенности
FILES_PER_LAYER = 3 # Количество файлов в каждом слое
FILE_SIZE = 1024 # Размер файла в байтах
def create_nested_structure(base_dir, depth, files_per_layer, file_size):
"""Создаёт вложенную структуру с именами layerX_Y и файлами file_X_Y"""
start_time = time.time()
total_files = 0
for layer in range(depth):
for sublayer in range(files_per_layer):
layer_dir = os.path.join(base_dir, f"layer{layer}_{sublayer}")
os.makedirs(layer_dir, exist_ok=True)
for file_index in range(files_per_layer):
file_path = os.path.join(layer_dir, f"file_{layer}_{sublayer}_{file_index}.txt")
with open(file_path, "wb") as f:
f.write(os.urandom(file_size))
total_files += 1
elapsed_time = time.time() - start_time
iops = total_files / max(elapsed_time, 1e-9) # Предотвращение деления на ноль
return elapsed_time, iops, total_files
def scan_files(base_dir):
"""Сканирует все файлы и возвращает скорость доступа"""
start_time = time.time()
total_files = 0
for root, dirs, files in os.walk(base_dir):
for file in files:
file_path = os.path.join(root, file)
stat = os.stat(file_path) # Получение метаинформации
_ = (stat.st_size, stat.st_ctime, file) # Вес, дата создания, имя файла
total_files += 1
elapsed_time = time.time() - start_time
iops = total_files / max(elapsed_time, 1e-9) # Предотвращение деления на ноль
return elapsed_time, iops, total_files
def test_iops(path: Path):
os.makedirs(path / ".tests", exist_ok=True)
base_bir = (path / ".tests" / "iops").as_posix()
_, create_iops, _ = create_nested_structure(
base_bir, DEPTH, FILES_PER_LAYER, FILE_SIZE
)
_, scan_iops, _ = scan_files(base_bir)
shutil.rmtree(base_bir)
return create_iops, scan_iops