|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import time |
| 6 | + |
| 7 | +import tdl |
| 8 | + |
| 9 | +WIDTH = 80 # must be divisible by 16 |
| 10 | +HEIGHT = 48 |
| 11 | + |
| 12 | +log = None |
| 13 | + |
| 14 | +def print_result(string): |
| 15 | + print(string) |
| 16 | + print(string, file=log) |
| 17 | + |
| 18 | +class Benchmark: |
| 19 | + default_frames = 50 |
| 20 | + |
| 21 | + def run(self, console, frames=None, times=3): |
| 22 | + if times > 1: |
| 23 | + print_result('Running %s' % self.__class__.__name__) |
| 24 | + while times > 0: |
| 25 | + self.run(console, frames, times=1) |
| 26 | + times -= 1 |
| 27 | + print_result('') |
| 28 | + return |
| 29 | + if frames is None: |
| 30 | + frames = self.default_frames |
| 31 | + self.total_frames = 0 |
| 32 | + self.tiles = 0 |
| 33 | + console.clear() |
| 34 | + self.start_time = time.time() |
| 35 | + while self.total_frames < frames: |
| 36 | + self.total_frames += 1 |
| 37 | + self.test(console) |
| 38 | + for event in tdl.event.get(): |
| 39 | + if event.type == 'QUIT': |
| 40 | + raise SystemExit('Benchmark Canceled') |
| 41 | + self.total_time = time.time() - self.start_time |
| 42 | + self.tiles_per_second = self.tiles / self.total_time |
| 43 | + print_result( |
| 44 | + '%i tiles drawn in %.2f seconds, %.2f characters/ms, %.2f FPS' % |
| 45 | + (self.tiles, self.total_time,self.tiles_per_second / 1000, |
| 46 | + self.total_frames / self.total_time)) |
| 47 | + |
| 48 | + def test(self, console): |
| 49 | + for x,y in console: |
| 50 | + console.draw_char(x, y, '.') |
| 51 | + tiles += 1 |
| 52 | + tdl.flush() |
| 53 | + |
| 54 | + |
| 55 | +class Benchmark_DrawChar_DefaultColor(Benchmark): |
| 56 | + |
| 57 | + def test(self, console): |
| 58 | + for x,y in console: |
| 59 | + console.draw_char(x, y, 'A') |
| 60 | + self.tiles += 1 |
| 61 | + tdl.flush() |
| 62 | + |
| 63 | + |
| 64 | +class Benchmark_DrawChar_NoColor(Benchmark): |
| 65 | + |
| 66 | + def test(self, console): |
| 67 | + for x,y in console: |
| 68 | + console.draw_char(x, y, 'B', None, None) |
| 69 | + self.tiles += 1 |
| 70 | + tdl.flush() |
| 71 | + |
| 72 | + |
| 73 | +class Benchmark_DrawStr16_DefaultColor(Benchmark): |
| 74 | + default_frames = 100 |
| 75 | + |
| 76 | + def test(self, console): |
| 77 | + for y in range(HEIGHT): |
| 78 | + for x in range(0, WIDTH, 16): |
| 79 | + console.draw_str(x, y, '0123456789ABCDEF') |
| 80 | + self.tiles += 16 |
| 81 | + tdl.flush() |
| 82 | + |
| 83 | + |
| 84 | +class Benchmark_DrawStr16_NoColor(Benchmark): |
| 85 | + default_frames = 100 |
| 86 | + |
| 87 | + def test(self, console): |
| 88 | + for y in range(HEIGHT): |
| 89 | + for x in range(0, WIDTH, 16): |
| 90 | + console.draw_str(x, y, '0123456789ABCDEF', None, None) |
| 91 | + self.tiles += 16 |
| 92 | + tdl.flush() |
| 93 | + |
| 94 | +def run_benchmark(): |
| 95 | + global log |
| 96 | + log = open('results.log', 'a') |
| 97 | + print('', file=log) |
| 98 | + print_result('Benchmark run on %s' % time.ctime()) |
| 99 | + renderer='OpenGL' |
| 100 | + |
| 101 | + console = tdl.init(WIDTH, HEIGHT, renderer=renderer) |
| 102 | + print_result('Opened console in %s mode' % renderer) |
| 103 | + Benchmark_DrawChar_DefaultColor().run(console) |
| 104 | + Benchmark_DrawChar_NoColor().run(console) |
| 105 | + Benchmark_DrawStr16_DefaultColor().run(console) |
| 106 | + Benchmark_DrawStr16_NoColor().run(console) |
| 107 | + log.close() |
| 108 | + print('results written to results.log') |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + run_benchmark() |
0 commit comments