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):
69 self.cwd = cwd
70 self.tests = []
71 self.wall = wall
72 self.game = game
73
74 def collect_tests(self):
75 roms = []
76 for f in os.listdir(self.cwd):
77 if f.endswith('.gba'):
78 roms.append(f)
79 roms.sort()
80 for rom in roms:
81 self.add_tests(rom)
82
83 def add_tests(self, rom):
84 if self.wall:
85 self.tests.append(WallClockTest(rom, self.wall))
86 if self.game:
87 self.tests.append(GameClockTest(rom, self.game, renderer=None))
88
89 def run(self):
90 results = []
91 for test in self.tests:
92 print('Running test {}'.format(test.name), file=sys.stderr)
93 try:
94 test.run(self.cwd)
95 except KeyboardInterrupt:
96 print('Interrupted, returning early...', file=sys.stderr)
97 return results
98 if test.results:
99 results.append(test.results)
100 return results
101
102if __name__ == '__main__':
103 parser = argparse.ArgumentParser()
104 parser.add_argument('-w', '--wall-time', type=float, default=60, metavar='TIME', help='wall-clock time')
105 parser.add_argument('-g', '--game-frames', type=int, default=120*60, metavar='FRAMES', help='game-clock frames')
106 parser.add_argument('-o', '--out', metavar='FILE', help='output file path')
107 parser.add_argument('directory', help='directory containing ROM files')
108 args = parser.parse_args()
109
110 s = Suite(args.directory, wall=args.wall_time, game=args.game_frames)
111 s.collect_tests()
112 results = s.run()
113 fout = sys.stdout
114 if args.out:
115 fout = open(args.out, 'w')
116 writer = csv.DictWriter(fout, results[0].keys())
117 writer.writeheader()
118 writer.writerows(results)
119 if fout is not sys.stdout:
120 fout.close()