tools/perf.py (view raw)
1#!/usr/bin/env python
2from __future__ import print_function
3import argparse
4import csv
5import os
6import signal
7import subprocess
8import sys
9import time
10
11class PerfTest(object):
12 EXECUTABLE = 'mgba-perf'
13
14 def __init__(self, rom, renderer='software'):
15 self.rom = rom
16 self.renderer = renderer
17 self.results = None
18 self.name = 'Perf Test: {}'.format(rom)
19
20 def get_args(self):
21 return []
22
23 def wait(self, proc):
24 pass
25
26 def run(self, cwd):
27 args = [os.path.join(os.getcwd(), self.EXECUTABLE), '-P']
28 args.extend(self.get_args())
29 if self.renderer != 'software':
30 args.append('-N')
31 args.append(self.rom)
32 env = {}
33 if 'LD_LIBRARY_PATH' in os.environ:
34 env['LD_LIBRARY_PATH'] = os.path.abspath(os.environ['LD_LIBRARY_PATH'])
35 env['DYLD_LIBRARY_PATH'] = env['LD_LIBRARY_PATH'] # Fake it on OS X
36 proc = subprocess.Popen(args, stdout=subprocess.PIPE, cwd=cwd, universal_newlines=True, env=env)
37 try:
38 self.wait(proc)
39 proc.wait()
40 except:
41 proc.kill()
42 raise
43 if proc.returncode < 0:
44 return
45 reader = csv.DictReader(proc.stdout)
46 self.results = next(reader)
47
48class WallClockTest(PerfTest):
49 def __init__(self, rom, duration, renderer='software'):
50 super(WallClockTest, self).__init__(rom, renderer)
51 self.duration = duration
52 self.name = 'Wall-Clock Test ({} seconds, {} renderer): {}'.format(duration, renderer, rom)
53
54 def wait(self, proc):
55 time.sleep(self.duration)
56 proc.send_signal(signal.SIGINT)
57
58class GameClockTest(PerfTest):
59 def __init__(self, rom, frames, renderer='software'):
60 super(GameClockTest, self).__init__(rom, renderer)
61 self.frames = frames
62 self.name = 'Game-Clock Test ({} frames, {} renderer): {}'.format(frames, renderer, rom)
63
64 def get_args(self):
65 return ['-F', str(self.frames)]
66
67class Suite(object):
68 def __init__(self, cwd, wall=None, game=None, renderer='software'):
69 self.cwd = cwd
70 self.tests = []
71 self.wall = wall
72 self.game = game
73 self.renderer = renderer
74
75 def collect_tests(self):
76 roms = []
77 for f in os.listdir(self.cwd):
78 if f.endswith('.gba'):
79 roms.append(f)
80 roms.sort()
81 for rom in roms:
82 self.add_tests(rom)
83
84 def add_tests(self, rom):
85 if self.wall:
86 self.tests.append(WallClockTest(rom, self.wall, renderer=self.renderer))
87 if self.game:
88 self.tests.append(GameClockTest(rom, self.game, renderer=self.renderer))
89
90 def run(self):
91 results = []
92 for test in self.tests:
93 print('Running test {}'.format(test.name), file=sys.stderr)
94 try:
95 test.run(self.cwd)
96 except KeyboardInterrupt:
97 print('Interrupted, returning early...', file=sys.stderr)
98 return results
99 if test.results:
100 results.append(test.results)
101 return results
102
103if __name__ == '__main__':
104 parser = argparse.ArgumentParser()
105 parser.add_argument('-w', '--wall-time', type=float, default=0, metavar='TIME', help='wall-clock time')
106 parser.add_argument('-g', '--game-frames', type=int, default=0, metavar='FRAMES', help='game-clock frames')
107 parser.add_argument('-N', '--disable-renderer', action='store_const', const=True, help='disable video rendering')
108 parser.add_argument('-o', '--out', metavar='FILE', help='output file path')
109 parser.add_argument('directory', help='directory containing ROM files')
110 args = parser.parse_args()
111
112 s = Suite(args.directory, wall=args.wall_time, game=args.game_frames, renderer=None if args.disable_renderer else 'software')
113 s.collect_tests()
114 results = s.run()
115 fout = sys.stdout
116 if args.out:
117 fout = open(args.out, 'w')
118 writer = csv.DictWriter(fout, results[0].keys())
119 writer.writeheader()
120 writer.writerows(results)
121 if fout is not sys.stdout:
122 fout.close()