-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmarkanalysis.py
executable file
·187 lines (135 loc) · 5.75 KB
/
benchmarkanalysis.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/python -tt
"""
MAME support program to analyze MAME emulation benchmark files of a single game or a list of games:
- Average emulation speed and total emulation time are calculated by taking into account the benchmark from the last game and the previous benchmark.
- The previous benchmark is updated and saved in a dedicated MAME benchmarks directory.
- The average emulation speed is converted into a 1-5 star rating.
- Benchmark data are saved in a formatted ASCII file (AMbenchmarks.ini). This file can be used to display benchmark data in Attract-Mode.
Usage:
1) Change configsetup.py according to your system setup.
2) Optionally adjust star rating levels to your liking:
"""
starlevels = [75, 90, 95, 97.5]
"""
3) To process a single game, type:
./benchmarkanalysis.py {game}
where {game} is the romname of the game (e.g. pacman)
OR to process all games in your MAME benchmarks directory, type:
./benchmarkanalysis.py all
Author: Gordon Lim
Last Edit: 1 Feb 2018
"""
import configsetup
import os
import re
import subprocess
import sys
regexsearchpattern = 'Average speed: (\d+)\.\d*% \((\d+) seconds\)'
def getbenchmarkdata(MAMEbenchmarkfilename):
# Use regex search to extract benchmark data from file:
MAMEbenchmarkfile = open(MAMEbenchmarkfilename, "r")
line = MAMEbenchmarkfile.readline()
MAMEbenchmarkfile.close()
matchobject = re.search(regexsearchpattern, line)
if not matchobject:
print("------ regex search of MAME benchmark file failed - EXIT")
return 0, 0
speed = float(matchobject.group(1))
time = float(matchobject.group(2))
return speed, time
def calculatestarrating(speed):
speed = float(speed)
stars = 0
if (0 < speed < starlevels[0]):
stars = 1
elif (starlevels[0] < speed < starlevels[1]):
stars = 2
elif (starlevels[1] < speed < starlevels[2]):
stars = 3
elif (starlevels[2] < speed < starlevels[3]):
stars = 4
else:
stars = 5
return stars
def createbenchmarkfile(game):
print("--- Creating AM benchmark file for {}:".format(game))
# Get benchmark data from last MAME benchmark file:
speed_new = 0
time_new = 0
MAMEbenchmarkfilename_lastgame = MAMEbenchmarkdir + game + '_lastgame.log'
if os.path.isfile(MAMEbenchmarkfilename_lastgame):
speed_new, time_new = getbenchmarkdata(MAMEbenchmarkfilename_lastgame)
subprocess.call(["rm", MAMEbenchmarkfilename_lastgame])
# Get benchmark data from previous MAME benchmark file:
speed_old = 0
time_old = 0
MAMEbenchmarkfilename_allgames = MAMEbenchmarkdir + game + '_allgames.log'
if os.path.isfile(MAMEbenchmarkfilename_allgames):
speed_old, time_old = getbenchmarkdata(MAMEbenchmarkfilename_allgames)
if ((time_old == 0) and (time_new == 0)):
print("------ MAME benchmark files do not exist - EXIT")
return
# Calculate total time and average speed weigthed by time:
time_total = time_old + time_new
speed_ave = speed_old*(time_old/time_total) + speed_new*(time_new/time_total)
# Update previous MAME benchmark file:
MAMEbenchmarkfile = open(MAMEbenchmarkfilename_allgames, "w")
benchmarkline = "Average speed: {:.2f}% ({} seconds)".format(float(speed_ave), int(time_total))
MAMEbenchmarkfile.write(benchmarkline)
MAMEbenchmarkfile.close()
# Calculate "star" rating:
stars = calculatestarrating(speed_ave)
# Save benchmark data in AMbenchmarks.ini:
AMbenchmarkfilename = configsetup.AMsupportdir + "data/AMbenchmarks.ini"
AMbenchmarkfilename_exists = False
if os.path.isfile(AMbenchmarkfilename):
AMbenchmarkfilename_exists = True
AMbenchmarkfile = open(AMbenchmarkfilename, 'r')
lines = []
while True:
line = AMbenchmarkfile.readline()
if not line:
break
if (line == "[{}]\n".format(game)):
dummy = AMbenchmarkfile.readline() # skip speed
dummy = AMbenchmarkfile.readline() # skip stars
dummy = AMbenchmarkfile.readline() # skip time
else:
lines.append(line)
AMbenchmarkfile.close()
AMbenchmarkfile = open(AMbenchmarkfilename, 'w')
if (AMbenchmarkfilename_exists):
for line in lines:
AMbenchmarkfile.write(line)
AMbenchmarkfile.write("[{}]\n".format(game))
AMbenchmarkfile.write("speed={:.2f}\n".format(round(speed_ave)))
AMbenchmarkfile.write("stars={}\n".format(stars))
AMbenchmarkfile.write("time={}\n".format(int(time_total)))
AMbenchmarkfile.close()
print("------ AM benchmark file for {} created => SUCCESS".format(game))
return 0
def main():
# Setup configuration:
configsetup.init()
global MAMEbenchmarkdir
MAMEbenchmarkdir = configsetup.MAMEconfigdir + "benchmarks/"
if not os.path.isdir(MAMEbenchmarkdir):
subprocess.call(["mkdir", "benchmarks"], cwd = configsetup.MAMEconfigdir)
if not os.path.isdir(MAMEbenchmarkdir):
print("ERROR: MAME benchmark directory does not exist - EXIT")
return 1
# Check input and process game(s):
if (len(sys.argv) != 2):
print("Please provide a romname or 'all' as input argument")
return 1
inputargument = sys.argv[1]
if (inputargument == 'all'):
for filename in os.listdir(MAMEbenchmarkdir): # For all files in MAME benchmark directory:
if (filename[-13:] == "_allgames.log"):
romname = filename[:-13]
createbenchmarkfile(romname)
else:
createbenchmarkfile(inputargument)
return 0
if __name__ == '__main__':
main()