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