Files
OS-LABS/2/benchmark.py
T
2026-04-28 15:42:35 +07:00

69 lines
2.0 KiB
Python
Raw 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 subprocess
import matplotlib.pyplot as plt
def run_test(n, threads, threshold):
cmd = f"./lab2 {n} {threads} {threshold}"
process = subprocess.Popen(
cmd.split(), stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True
)
_, err = process.communicate()
for line in err.split("\n"):
if "STAT:" in line:
return float(line.split("time=")[1])
return 0
def build_benchmark():
n_size = 500000
threshold = 10000
thread_counts = [0, 1, 2, 4, 8, 12, 16]
times = []
print(f"Запуск бенчмарка (N={n_size}, Порог={threshold})...")
for t in thread_counts:
t_exec = run_test(n_size, t, threshold)
times.append(t_exec)
print(f"Потоков: {t} | Время: {t_exec:.4f}с")
plt.figure(figsize=(12, 5))
# График времени
plt.subplot(1, 2, 1)
plt.plot(thread_counts, times, "o-", color="blue", label="Фактическое время")
plt.xlabel("Кол-во потоков (0 = посл.)")
plt.ylabel("Время выполнения (сек)")
plt.title("Зависимость времени от потоков")
plt.grid(True)
plt.legend()
# График ускорения
plt.subplot(1, 2, 2)
t_seq = times[0]
speedup = [t_seq / x if x > 0 else 1 for x in times]
plt.plot(thread_counts, speedup, "s-", color="green", label="Ускорение (S)")
plt.plot(
thread_counts,
[x if x > 0 else 1 for x in thread_counts],
"--",
color="red",
alpha=0.5,
label="Идеал",
)
plt.xlabel("Кол-во потоков")
plt.ylabel("S = T(послед) / T(паралл)")
plt.title("График масштабируемости (Speedup)")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig("out/performance_results.png")
print("Графики сохранены в out/performance_results.png")
if __name__ == "__main__":
os.makedirs("out", exist_ok=True)
build_benchmark()