forked from irmen/rocketsimulator
/
performancetest_nodisplay.py
64 lines (55 loc) · 2.23 KB
/
performancetest_nodisplay.py
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
"""
A rocket animation performance test (no display output).
Copyright by Irmen de Jong (irmen@razorvine.net).
Open source software license: MIT.
"""
from __future__ import print_function, division
import random
import time
import sys
from vectors import Vector2D
from rocketsimulator import Rocket
if sys.version_info < (3,0):
input = raw_input
class PerformanceTest(object):
def run(self):
self.cwidth = 1000
self.cheight = 1000
self.rockets = []
rockets_added_per_iteration = 200
num_frames_per_iteration = 200
while True:
for _ in range(rockets_added_per_iteration):
self.add_rocket()
print("simulating {:d} frames with {:d} rockets...".format(num_frames_per_iteration, len(self.rockets)))
start_time = time.time()
self.simulate(num_frames_per_iteration)
duration = time.time() - start_time
print(" ... that took {:.2f} seconds; {:.2f} frames/sec".format(duration, num_frames_per_iteration/duration))
input("\nenter to run again with {:d} extra rockets...:".format(rockets_added_per_iteration))
def simulate(self, num_frames=10000):
class DummyCanvas(object):
def create_polygon(self, points, outline="orange", fill="yellow"):
pass
dummycanvas = DummyCanvas()
for _ in range(num_frames):
self.update()
for rocket in self.rockets:
rocket.draw(dummycanvas)
def add_rocket(self):
rocket = Rocket(self.cwidth, self.cheight)
rocket.rotation_speed = random.uniform(-.3,.3)
rocket.engine_throttle = 1
rocket.position = Vector2D((random.randint(-self.cwidth/2,self.cwidth/2), random.randint(0,self.cheight)))
rocket.velocity = Vector2D((random.uniform(-10,10), random.uniform(-4,4)))
self.rockets.append(rocket)
def update(self):
for rocket in self.rockets:
rocket.update()
if not(-self.cwidth/2 < rocket.position.x < self.cwidth/2):
rocket.velocity.flipx()
if not(0<rocket.position.y<self.cheight):
rocket.velocity.flipy()
if __name__ == "__main__":
test = PerformanceTest()
test.run()