tools/perf.py (view raw)
1#!/usr/bin/env python
2from __future__ import print_function
3import argparse
4import csv
5import os
6import shlex
7import signal
8import socket
9import subprocess
10import sys
11import time
12
13class PerfTest(object):
14 EXECUTABLE = 'mgba-perf'
15
16 def __init__(self, rom, renderer='software'):
17 self.rom = rom
18 self.renderer = renderer
19 self.results = None
20 self.name = 'Perf Test: {}'.format(rom)
21
22 def get_args(self):
23 return []
24
25 def wait(self, proc):
26 pass
27
28 def run(self, cwd):
29 args = [os.path.join(os.getcwd(), self.EXECUTABLE), '-P']
30 args.extend(self.get_args())
31 if not self.renderer:
32 args.append('-N')
33 elif self.renderer == 'threaded-software':
34 args.append('-T')
35 args.append(self.rom)
36 env = {}
37 if 'LD_LIBRARY_PATH' in os.environ:
38 env['LD_LIBRARY_PATH'] = os.path.abspath(os.environ['LD_LIBRARY_PATH'])
39 env['DYLD_LIBRARY_PATH'] = env['LD_LIBRARY_PATH'] # Fake it on OS X
40 proc = subprocess.Popen(args, stdout=subprocess.PIPE, cwd=cwd, universal_newlines=True, env=env)
41 try:
42 self.wait(proc)
43 proc.wait()
44 except:
45 proc.kill()
46 raise
47 if proc.returncode:
48 print('Game crashed!', file=sys.stderr)
49 return
50 reader = csv.DictReader(proc.stdout)
51 self.results = next(reader)
52
53class WallClockTest(PerfTest):
54 def __init__(self, rom, duration, renderer='software'):
55 super(WallClockTest, self).__init__(rom, renderer)
56 self.duration = duration
57 self.name = 'Wall-Clock Test ({} seconds, {} renderer): {}'.format(duration, renderer, rom)
58
59 def wait(self, proc):
60 time.sleep(self.duration)
61 proc.send_signal(signal.SIGINT)
62
63class GameClockTest(PerfTest):
64 def __init__(self, rom, frames, renderer='software'):
65 super(GameClockTest, self).__init__(rom, renderer)
66 self.frames = frames
67 self.name = 'Game-Clock Test ({} frames, {} renderer): {}'.format(frames, renderer, rom)
68
69 def get_args(self):
70 return ['-F', str(self.frames)]
71
72class PerfServer(object):
73 ITERATIONS_PER_INSTANCE = 50
74
75 def __init__(self, address, command=None):
76 s = address.rsplit(':', 1)
77 if len(s) == 1:
78 self.address = (s[0], 7216)
79 else:
80 self.address = (s[0], s[1])
81 if command:
82 self.command = shlex.split(command)
83 self.iterations = self.ITERATIONS_PER_INSTANCE
84 self.socket = None
85 self.results = []
86 self.reader = None
87
88 def _start(self, test):
89 if self.command:
90 server_command = list(self.command)
91 else:
92 server_command = [os.path.join(os.getcwd(), PerfTest.EXECUTABLE)]
93 server_command.extend(['--', '-PD'])
94 if hasattr(test, "frames"):
95 server_command.extend(['-F', str(test.frames)])
96 if not test.renderer:
97 server_command.append('-N')
98 elif test.renderer == 'threaded-software':
99 server_command.append('-T')
100 subprocess.check_call(server_command)
101 time.sleep(4)
102 self.socket = socket.create_connection(self.address, timeout=1000)
103 kwargs = {}
104 if sys.version_info[0] >= 3:
105 kwargs["encoding"] = "utf-8"
106 self.reader = csv.DictReader(self.socket.makefile(**kwargs))
107
108 def run(self, test):
109 if not self.socket:
110 self._start(test)
111 self.socket.send(os.path.join("/perfroms", test.rom).encode("utf-8"))
112 self.results.append(next(self.reader))
113 self.iterations -= 1
114 if self.iterations == 0:
115 self.finish()
116 self.iterations = self.ITERATIONS_PER_INSTANCE
117
118 def finish(self):
119 self.socket.send(b"\n");
120 self.reader = None
121 self.socket.close()
122 time.sleep(5)
123 self.socket = None
124
125class Suite(object):
126 def __init__(self, cwd, wall=None, game=None, renderer='software'):
127 self.cwd = cwd
128 self.tests = []
129 self.wall = wall
130 self.game = game
131 self.renderer = renderer
132 self.server = None
133
134 def set_server(self, server):
135 self.server = server
136
137 def collect_tests(self):
138 roms = []
139 for f in os.listdir(self.cwd):
140 if f.endswith('.gba') or f.endswith('.zip') or f.endswith('.gbc') or f.endswith('.gb'):
141 roms.append(f)
142 roms.sort()
143 for rom in roms:
144 self.add_tests(rom)
145
146 def add_tests(self, rom):
147 if self.wall:
148 self.tests.append(WallClockTest(rom, self.wall, renderer=self.renderer))
149 if self.game:
150 self.tests.append(GameClockTest(rom, self.game, renderer=self.renderer))
151
152 def run(self):
153 results = []
154 sock = None
155 for test in self.tests:
156 print('Running test {}'.format(test.name), file=sys.stderr)
157 if self.server:
158 self.server.run(test)
159 else:
160 try:
161 test.run(self.cwd)
162 except KeyboardInterrupt:
163 print('Interrupted, returning early...', file=sys.stderr)
164 return results
165 if test.results:
166 results.append(test.results)
167 if self.server:
168 self.server.finish()
169 results.extend(self.server.results)
170 return results
171
172if __name__ == '__main__':
173 parser = argparse.ArgumentParser()
174 parser.add_argument('-w', '--wall-time', type=float, default=0, metavar='TIME', help='wall-clock time')
175 parser.add_argument('-g', '--game-frames', type=int, default=0, metavar='FRAMES', help='game-clock frames')
176 parser.add_argument('-N', '--disable-renderer', action='store_const', const=True, help='disable video rendering')
177 parser.add_argument('-T', '--threaded-renderer', action='store_const', const=True, help='threaded video rendering')
178 parser.add_argument('-s', '--server', metavar='ADDRESS', help='run on server')
179 parser.add_argument('-S', '--server-command', metavar='COMMAND', help='command to launch server')
180 parser.add_argument('-o', '--out', metavar='FILE', help='output file path')
181 parser.add_argument('directory', help='directory containing ROM files')
182 args = parser.parse_args()
183
184 renderer = 'software'
185 if args.disable_renderer:
186 renderer = None
187 elif args.threaded_renderer:
188 renderer = 'threaded-software'
189 s = Suite(args.directory, wall=args.wall_time, game=args.game_frames, renderer=renderer)
190 if args.server:
191 if args.server_command:
192 server = PerfServer(args.server, args.server_command)
193 else:
194 server = PerfServer(args.server)
195 s.set_server(server)
196 s.collect_tests()
197 results = s.run()
198 fout = sys.stdout
199 if args.out:
200 fout = open(args.out, 'w')
201 writer = csv.DictWriter(fout, results[0].keys())
202 writer.writeheader()
203 writer.writerows(results)
204 if fout is not sys.stdout:
205 fout.close()