-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
69 lines (55 loc) · 2.03 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""benchmark.py: Run the project multiple times and calculate statistics."""
import argparse
import re
import statistics
import subprocess
parser = argparse.ArgumentParser(
description="stats for your tileworld runs",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("-r", "--runs", type=int, default=10)
args = parser.parse_args()
p1_scores: list[int] = []
p1_win_count = 0
p2_scores: list[int] = []
p2_win_count = 0
for run_num in range(args.runs):
print(f"Run {run_num + 1} of {args.runs}:")
run_cap = subprocess.run(
["python", "main.py"], capture_output=True, text=True, check=True
)
score1_re = re.compile(r"Score of Player 1: (-?\d*)")
if p1_search_tmp := score1_re.search(run_cap.stdout):
if p1_score_tmp := p1_search_tmp.group(1):
p1_scores.append(int(p1_score_tmp))
else:
raise Exception("Could not find Player 1 score")
score2_re = re.compile(r"Score of Player 2: (-?\d*)")
if p2_search_tmp := score2_re.search(run_cap.stdout):
if p2_score_tmp := p2_search_tmp.group(1):
p2_scores.append(int(p2_score_tmp))
else:
raise Exception("Could not find Player 2 score")
if p1_scores[-1] > p2_scores[-1]:
p1_win_count += 1
else:
p2_win_count += 1
print(f" Player 1: {p1_scores[-1]}")
print(f" Player 2: {p2_scores[-1]}")
print("\nSummary:")
print("\nPlayer 1:\n")
print(f" Wins: {p1_win_count}")
print(f" Total Points: {sum(p1_scores)}\n")
print(f" Scores: {p1_scores}")
print(f" Sorted: {sorted(p1_scores)}")
print(f" Mean: {statistics.mean(p1_scores)}")
print(f" Median: {statistics.median(p1_scores)}")
print(f" Std Dev: {statistics.stdev(p1_scores):.2f}")
print("\nPlayer 2:\n")
print(f" Wins: {p2_win_count}")
print(f" Total Points: {sum(p2_scores)}\n")
print(f" Scores: {p2_scores}")
print(f" Sorted: {sorted(p2_scores)}")
print(f" Mean: {statistics.mean(p2_scores)}")
print(f" Median: {statistics.median(p2_scores)}")
print(f" Std Dev: {statistics.stdev(p2_scores):.2f}")