From c5a1e6b6db13646c6bdf739123c4d8886c2d2e55 Mon Sep 17 00:00:00 2001 From: pajjilykk Date: Tue, 28 Apr 2026 15:57:05 +0700 Subject: [PATCH] update test cases 2 --- 2/benchmark.py | 78 +++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/2/benchmark.py b/2/benchmark.py index 798dd69..ecc4cd3 100644 --- a/2/benchmark.py +++ b/2/benchmark.py @@ -12,52 +12,64 @@ def run_test(n, threads, threshold): _, err = process.communicate() for line in err.split("\n"): if "STAT:" in line: - return float(line.split("time=")[1]) - return 0 + try: + # 1. Берем часть после "time=" -> "0.010061 sec" + # 2. .split() разбивает по пробелу на ["0.010061", "sec"] + # 3. [0] берет первый элемент -> "0.010061" + time_value = line.split("time=")[1].split()[0] + return float(time_value) + except (IndexError, ValueError): + continue + return 0.0 def build_benchmark(): - n_size = 500000 + data_sizes = { + "Small (10^5)": 100000, + "Medium (2*10^6)": 2000000, + "Large (5*10^6)": 5000000, + } + threshold = 10000 - thread_counts = [0, 1, 2, 4, 8, 12, 16] - times = [] + thread_counts = [1, 2, 4, 8, 12, 16] - 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=(14, 6)) + ax1 = plt.subplot(1, 2, 1) + ax2 = plt.subplot(1, 2, 2) - plt.figure(figsize=(12, 5)) + for label, n_size in data_sizes.items(): + times = [] + print(f"\nТестирование: {label}...") + for t in thread_counts: + t_exec = run_test(n_size, t, threshold) + times.append(t_exec) + print(f" Потоков: {t} | Время: {t_exec:.4f}с") - plt.subplot(1, 2, 1) - plt.plot(thread_counts, times, "o-", color="blue", label="Фактическое время") - plt.xlabel("Потоки") - plt.ylabel("Время (сек)") - plt.title("Время выполнения") - plt.grid(True) + ax1.plot(thread_counts, times, "o-", label=label) + t_seq = times[0] + speedup = [t_seq / x if x > 0 else 1 for x in times] + ax2.plot(thread_counts, speedup, "s-", label=label) - 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="Ускорение") - plt.plot( - thread_counts, - [x if x > 0 else 1 for x in thread_counts], - "--", - color="red", - alpha=0.5, + ax1.set_xlabel("Потоки") + ax1.set_ylabel("Время (сек)") + ax1.set_title("Время выполнения") + ax1.legend() + ax1.grid(True) + + ax2.plot( + thread_counts, thread_counts, "--", color="black", alpha=0.3, label="Идеал" ) - plt.xlabel("Потоки") - plt.ylabel("Ускорение S") - plt.title("Масштабируемость") - plt.grid(True) + ax2.set_xlabel("Потоки") + ax2.set_ylabel("Ускорение") + ax2.set_title("Масштабируемость") + ax2.legend() + ax2.grid(True) plt.tight_layout() + os.makedirs("out", exist_ok=True) plt.savefig("out/performance_results.png") + print("\nГрафик производительности сохранен в out/performance_results.png") if __name__ == "__main__": - if not os.path.exists("out"): - os.makedirs("out") build_benchmark()