Benchmark.test()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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