forked from mtusi2026/mtusi2026lab5
298 lines
7.1 KiB
Python
298 lines
7.1 KiB
Python
import csv
|
|
import re
|
|
import subprocess
|
|
import time
|
|
from pathlib import Path
|
|
|
|
CONFIGS = [(1, 1), (2, 2), (4, 4)]
|
|
COUNT_PER_SIZE = 20
|
|
RESULTS_DIR = Path("benchmark_results")
|
|
RESULTS_DIR.mkdir(exist_ok=True)
|
|
|
|
RSS_CODE = """
|
|
import os
|
|
|
|
total_kb = 0
|
|
current_pid = os.getpid()
|
|
|
|
for name in os.listdir("/proc"):
|
|
if not name.isdigit():
|
|
continue
|
|
|
|
if int(name) == current_pid:
|
|
continue
|
|
|
|
try:
|
|
with open(f"/proc/{name}/status") as file:
|
|
for line in file:
|
|
if line.startswith("VmRSS:"):
|
|
total_kb += int(line.split()[1])
|
|
break
|
|
except (OSError, ValueError):
|
|
pass
|
|
|
|
print(total_kb / 1024)
|
|
"""
|
|
|
|
|
|
def command(args, check=True):
|
|
return subprocess.run(
|
|
args,
|
|
text=True,
|
|
capture_output=True,
|
|
check=check,
|
|
)
|
|
|
|
|
|
def remove_test_worker():
|
|
command(
|
|
["docker", "rm", "-f", "benchmark-worker"],
|
|
check=False,
|
|
)
|
|
|
|
|
|
def start_worker(concurrency, prefetch):
|
|
remove_test_worker()
|
|
|
|
result = command([
|
|
"docker", "compose", "run",
|
|
"-d",
|
|
"--name", "benchmark-worker",
|
|
"--no-deps",
|
|
"-e", f"WORKER_CONCURRENCY={concurrency}",
|
|
"-e", f"WORKER_PREFETCH={prefetch}",
|
|
"worker",
|
|
])
|
|
|
|
expected = (
|
|
f"[Worker] CONCURRENCY={concurrency}, "
|
|
f"PREFETCH={prefetch}"
|
|
)
|
|
|
|
for _ in range(30):
|
|
logs = command(
|
|
["docker", "logs", "benchmark-worker"],
|
|
check=False,
|
|
)
|
|
|
|
if expected in logs.stdout + logs.stderr:
|
|
print(expected)
|
|
return
|
|
|
|
time.sleep(1)
|
|
|
|
raise RuntimeError(
|
|
f"Worker {concurrency}/{prefetch} не запустился.\n"
|
|
+ command(
|
|
["docker", "logs", "benchmark-worker"],
|
|
check=False,
|
|
).stderr
|
|
)
|
|
|
|
|
|
def measure_rss():
|
|
result = command(
|
|
[
|
|
"docker", "exec",
|
|
"benchmark-worker",
|
|
"python", "-c", RSS_CODE,
|
|
],
|
|
check=False,
|
|
)
|
|
|
|
if result.returncode != 0:
|
|
return None
|
|
|
|
try:
|
|
return float(result.stdout.strip())
|
|
except ValueError:
|
|
return None
|
|
|
|
|
|
def extract_number(text, label):
|
|
match = re.search(
|
|
rf"^{re.escape(label)}:\s*([0-9.]+)",
|
|
text,
|
|
re.MULTILINE,
|
|
)
|
|
return float(match.group(1)) if match else None
|
|
|
|
|
|
def run_benchmark(concurrency, prefetch):
|
|
config = f"{concurrency}_{prefetch}"
|
|
|
|
print(f"\n{'=' * 55}")
|
|
print(f"ЭКСПЕРИМЕНТ: {concurrency}/{prefetch}")
|
|
print(f"{'=' * 55}")
|
|
|
|
start_worker(concurrency, prefetch)
|
|
|
|
log_path = RESULTS_DIR / f"load_{config}.log"
|
|
rss_samples = []
|
|
|
|
load_command = [
|
|
"docker", "compose", "exec",
|
|
"-T",
|
|
"-w", "/tmp",
|
|
"-e", "PYTHONPATH=/app",
|
|
"app1",
|
|
"python", "-m", "app.load_test",
|
|
"--count", str(COUNT_PER_SIZE),
|
|
"--timeout", "300",
|
|
]
|
|
|
|
print("Запускаем 60 фотографий...")
|
|
start_time = time.monotonic()
|
|
|
|
with log_path.open("w") as log_file:
|
|
process = subprocess.Popen(
|
|
load_command,
|
|
stdout=log_file,
|
|
stderr=subprocess.STDOUT,
|
|
)
|
|
|
|
while process.poll() is None:
|
|
elapsed = time.monotonic() - start_time
|
|
rss = measure_rss()
|
|
|
|
if rss is not None:
|
|
rss_samples.append((elapsed, rss))
|
|
|
|
time.sleep(0.5)
|
|
|
|
return_code = process.wait()
|
|
|
|
output = log_path.read_text()
|
|
|
|
if return_code != 0:
|
|
print(output)
|
|
raise RuntimeError(
|
|
f"Нагрузочный тест {concurrency}/{prefetch} завершился с ошибкой"
|
|
)
|
|
|
|
print(output)
|
|
|
|
if not rss_samples:
|
|
raise RuntimeError("Не удалось измерить RSS")
|
|
|
|
rss_values = [value for _, value in rss_samples]
|
|
peak_rss = max(rss_values)
|
|
mean_rss = sum(rss_values) / len(rss_values)
|
|
|
|
samples_path = RESULTS_DIR / f"rss_{config}.csv"
|
|
|
|
with samples_path.open("w", newline="") as file:
|
|
writer = csv.writer(file)
|
|
writer.writerow(["elapsed_s", "rss_mb"])
|
|
writer.writerows(rss_samples)
|
|
|
|
csv_match = re.search(
|
|
r"CSV:\s*(benchmark_[a-zA-Z0-9]+\.csv)",
|
|
output,
|
|
)
|
|
|
|
if csv_match:
|
|
filename = csv_match.group(1)
|
|
|
|
copy_result = command(
|
|
[
|
|
"docker", "compose", "cp",
|
|
f"app1:/tmp/{filename}",
|
|
str(RESULTS_DIR / filename),
|
|
],
|
|
check=False,
|
|
)
|
|
|
|
if copy_result.returncode != 0:
|
|
print("Не удалось скопировать CSV:", copy_result.stderr)
|
|
|
|
result = {
|
|
"config": f"{concurrency}/{prefetch}",
|
|
"total": int(extract_number(output, "Total jobs") or 0),
|
|
"ready": int(extract_number(output, "Ready") or 0),
|
|
"failed": int(extract_number(output, "Failed") or 0),
|
|
"duration_s": extract_number(output, "Duration"),
|
|
"throughput": extract_number(output, "Throughput"),
|
|
"peak_processing": extract_number(
|
|
output, "Peak observed processing"
|
|
),
|
|
"peak_rss_mb": round(peak_rss, 2),
|
|
"mean_rss_mb": round(mean_rss, 2),
|
|
}
|
|
|
|
print(f"\n[RSS] Пиковое значение: {peak_rss:.2f} MB")
|
|
print(f"[RSS] Среднее значение: {mean_rss:.2f} MB")
|
|
print(f"[RSS] Количество замеров: {len(rss_samples)}")
|
|
|
|
return result
|
|
|
|
|
|
def save_summary(results):
|
|
output = RESULTS_DIR / "summary.csv"
|
|
|
|
with output.open("w", newline="") as file:
|
|
writer = csv.DictWriter(
|
|
file,
|
|
fieldnames=[
|
|
"config",
|
|
"total",
|
|
"ready",
|
|
"failed",
|
|
"duration_s",
|
|
"throughput",
|
|
"peak_processing",
|
|
"peak_rss_mb",
|
|
"mean_rss_mb",
|
|
],
|
|
)
|
|
|
|
writer.writeheader()
|
|
writer.writerows(results)
|
|
|
|
print("\n" + "=" * 70)
|
|
print("ИТОГОВАЯ ТАБЛИЦА")
|
|
print("=" * 70)
|
|
|
|
for item in results:
|
|
print(
|
|
f"{item['config']:5} | "
|
|
f"Ready: {item['ready']:3} | "
|
|
f"Time: {item['duration_s']} s | "
|
|
f"Throughput: {item['throughput']} img/s | "
|
|
f"Peak RSS: {item['peak_rss_mb']} MB"
|
|
)
|
|
|
|
print(f"\nРезультаты сохранены в {RESULTS_DIR}/")
|
|
|
|
|
|
def main():
|
|
results = []
|
|
|
|
try:
|
|
print("Останавливаем обычный Worker...")
|
|
command(["docker", "compose", "stop", "worker"])
|
|
|
|
for concurrency, prefetch in CONFIGS:
|
|
result = run_benchmark(concurrency, prefetch)
|
|
results.append(result)
|
|
|
|
finally:
|
|
print("\nВосстанавливаем обычный Worker...")
|
|
remove_test_worker()
|
|
|
|
restore = command(
|
|
["docker", "compose", "up", "-d", "worker"],
|
|
check=False,
|
|
)
|
|
|
|
if restore.returncode != 0:
|
|
print("Ошибка восстановления Worker:")
|
|
print(restore.stderr)
|
|
|
|
if results:
|
|
save_summary(results)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|