all repos — mgba @ dbd939022cf461336879da1b479ca12623be1cd0

mGBA Game Boy Advance Emulator

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        proc = subprocess.Popen(args, stdout=subprocess.PIPE, cwd=cwd, universal_newlines=True)
 33        try:
 34            self.wait(proc)
 35            proc.wait()
 36        except:
 37            proc.kill()
 38            raise
 39        if proc.returncode < 0:
 40            return
 41        reader = csv.DictReader(proc.stdout)
 42        self.results = next(reader)
 43
 44class WallClockTest(PerfTest):
 45    def __init__(self, rom, duration, renderer='software'):
 46        super(WallClockTest, self).__init__(rom, renderer)
 47        self.duration = duration
 48        self.name = 'Wall-Clock Test ({} seconds, {} renderer): {}'.format(duration, renderer, rom)
 49
 50    def wait(self, proc):
 51        time.sleep(self.duration)
 52        proc.send_signal(signal.SIGINT)
 53
 54class GameClockTest(PerfTest):
 55    def __init__(self, rom, frames, renderer='software'):
 56        super(GameClockTest, self).__init__(rom, renderer)
 57        self.frames = frames
 58        self.name = 'Game-Clock Test ({} frames, {} renderer): {}'.format(frames, renderer, rom)
 59
 60    def get_args(self):
 61        return ['-F', str(self.frames)]
 62
 63class Suite(object):
 64    def __init__(self, cwd, wall=None, game=None):
 65        self.cwd = cwd
 66        self.tests = []
 67        self.wall = wall
 68        self.game = game
 69
 70    def collect_tests(self):
 71        roms = []
 72        for f in os.listdir(self.cwd):
 73            if f.endswith('.gba'):
 74                roms.append(f)
 75        roms.sort()
 76        for rom in roms:
 77            self.add_tests(rom)
 78
 79    def add_tests(self, rom):
 80        if self.wall:
 81            self.tests.append(WallClockTest(rom, self.wall))
 82            self.tests.append(WallClockTest(rom, self.wall, renderer=None))
 83        if self.game:
 84            self.tests.append(GameClockTest(rom, self.game))
 85            self.tests.append(GameClockTest(rom, self.game, renderer=None))
 86
 87    def run(self):
 88        results = []
 89        for test in self.tests:
 90            print('Running test {}'.format(test.name), file=sys.stderr)
 91            try:
 92                test.run(self.cwd)
 93            except KeyboardInterrupt:
 94                print('Interrupted, returning early...', file=sys.stderr)
 95                return results
 96            if test.results:
 97                results.append(test.results)
 98        return results
 99
100if __name__ == '__main__':
101    parser = argparse.ArgumentParser()
102    parser.add_argument('-w', '--wall-time', type=float, default=120, metavar='TIME', help='wall-clock time')
103    parser.add_argument('-g', '--game-frames', type=int, default=120*60, metavar='FRAMES', help='game-clock frames')
104    parser.add_argument('-o', '--out', metavar='FILE', help='output file path')
105    parser.add_argument('directory', help='directory containing ROM files')
106    args = parser.parse_args()
107
108    s = Suite(args.directory, wall=args.wall_time, game=args.game_frames)
109    s.collect_tests()
110    results = s.run()
111    fout = sys.stdout
112    if args.out:
113        fout = open(args.out, 'w')
114    writer = csv.DictWriter(fout, results[0].keys())
115    writer.writeheader()
116    writer.writerows(results)
117    if fout is not sys.stdout:
118        fout.close()