forked from libtcod/python-tcod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstress_test.py
More file actions
executable file
·167 lines (131 loc) · 5.6 KB
/
stress_test.py
File metadata and controls
executable file
·167 lines (131 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
"""
Stress test designed to test tdl under harsh conditions
Note that one of the slower parts is converting colors over ctypes so you
can get a bit of speed avoiding the fgcolor and bgcolor parameters.
Another thing slowing things down are mass calls to ctypes functions.
Eventually these will be optimized to work better.
"""
import sys
import itertools
import random
import time
sys.path.insert(0, '../')
import tdl
class StopWatch:
"Simple tool used to count time within a block using the with statement"
MAXSNAPSHOTS = 10
def __init__(self):
self.enterTime = None
self.snapshots = []
def __enter__(self):
self.enterTime = time.clock()
def __exit__(self, *exc):
self.snapshots.append(time.clock() - self.enterTime)
if len(self.snapshots) > self.MAXSNAPSHOTS:
self.snapshots.pop(0)
def getMeanTime(self):
if not self.snapshots:
return 0
return sum(self.snapshots) / len(self.snapshots)
class TestApp(tdl.event.App):
def __init__(self, console):
self.console = console
self.writer = self.console
self.console.setMode('scroll')
self.width, self.height = self.console.getSize()
self.total = self.width * self.height
self.cells = list(itertools.product(range(self.width), range(self.height)))
self.tick = 0
self.init()
def init(self):
pass
def ev_MOUSEDOWN(self, event):
self.suspend()
def update(self, deltaTime):
self.updateTest(deltaTime)
self.tick += 1
#if self.tick % 50 == 0:
tdl.setTitle('%s: %i FPS' % (self.__class__.__name__, tdl.getFPS()))
tdl.flush()
class FullDrawCharTest(TestApp):
def updateTest(self, deltaTime):
# getrandbits is around 5x faster than using randint
bgcolors = [(random.getrandbits(7), random.getrandbits(7), random.getrandbits(7)) for _ in range(self.total)]
char = [random.getrandbits(8) for _ in range(self.total)]
fgcolor = (255, 255, 255)
for (x,y), bgcolor, char in zip(self.cells, bgcolors, char):
self.console.draw_char(x, y, char, fgcolor, bgcolor)
class PreCompiledColorTest(TestApp):
"""
This test was to see if the cast to the ctypes Color object was a big
impact on performance, turns out there's no hit with this class.
"""
def init(self):
self.bgcolors = [tdl._Color(random.getrandbits(7),
random.getrandbits(7),
random.getrandbits(7))
for _ in range(256)]
self.fgcolor = tdl._Color(255, 255, 255)
def updateTest(self, deltaTime):
# getrandbits is around 5x faster than using randint
char = [random.getrandbits(8) for _ in range(self.total)]
for (x,y), char in zip(self.cells, char):
self.console.draw_char(x, y, char,
self.fgcolor, random.choice(self.bgcolors))
class CharOnlyTest(TestApp):
def updateTest(self, deltaTime):
self.console.clear((255, 255, 255), (0, 0, 0))
char = [random.getrandbits(8) for _ in range(self.total)]
for (x,y), char in zip(self.cells, char):
self.console.draw_char(x, y, char)
class TypewriterCharOnlyTest(TestApp):
def updateTest(self, deltaTime):
self.writer.move(0, 0)
char = [random.getrandbits(8) for _ in range(self.total)]
for (x,y), char in zip(self.cells, char):
self.writer.move(x, y)
self.writer.print_str(chr(char))
class ColorOnlyTest(TestApp):
def updateTest(self, deltaTime):
# getrandbits is around 5x faster than using randint
bgcolors = [(random.getrandbits(6), random.getrandbits(6), random.getrandbits(6)) for _ in range(self.total)]
for (x,y), bgcolor in zip(self.cells, bgcolors):
self.console.draw_char(x, y, None, None, bgcolor)
class GetCharTest(TestApp):
def init(self):
for (x,y) in self.cells:
bgcolor = (random.getrandbits(6), random.getrandbits(6), random.getrandbits(6))
ch = random.getrandbits(8)
self.console.get_char(x, y, ch, bgcolor=bgcolor)
def updateTest(self, deltaTime):
for (x,y) in self.cells:
self.console.draw_char(x, y, *self.console.get_char(x, y))
class SingleRectTest(TestApp):
def updateTest(self, deltaTime):
bgcolor = (random.getrandbits(6), random.getrandbits(6), random.getrandbits(6))
self.console.draw_rect(0, 0, None, None, ' ', (255, 255, 255), bgcolor)
class DrawStrTest(TestApp):
def updateTest(self, deltaTime):
for y in range(self.height):
bgcolor = (random.getrandbits(6), random.getrandbits(6), random.getrandbits(6))
string = [random.getrandbits(8) for x in range(self.width)]
self.console.draw_str(0, y, string, (255, 255, 255), bgcolor)
class BlitScrollTest(TestApp):
def updateTest(self, deltaTime):
self.console.scroll(0, 1)
for x in range(self.width):
bgcolor = (random.getrandbits(6), random.getrandbits(6), random.getrandbits(6))
ch = random.getrandbits(8)
self.console.draw_char(x, 0, ch, bg=bgcolor)
# match libtcod sample screen
WIDTH = 46
HEIGHT = 20
def main():
console = tdl.init(46, 20, renderer='OPENGL')
for Test in [FullDrawCharTest, CharOnlyTest, TypewriterCharOnlyTest, ColorOnlyTest, GetCharTest,
SingleRectTest, DrawStrTest, BlitScrollTest]:
Test(console).run()
console.clear()
if __name__ == '__main__':
main()