Guest User

noisy_stats.py

a guest
Oct 29th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.84 KB | None | 0 0
  1.  
  2. '''
  3. Bounces ten balls around a window
  4. '''
  5. from __future__ import division, print_function
  6. import os
  7. import random
  8. import sys
  9. import pyglet
  10. from pyglet.gl import *
  11.  
  12. window = pyglet.window.Window(640, 480, vsync=True)
  13.  
  14. BALL_IMAGE = 'ball.png'
  15.  
  16. class Ball(pyglet.sprite.Sprite):
  17.     ball_image = pyglet.resource.image(BALL_IMAGE)
  18.     width = ball_image.width
  19.     height = ball_image.height
  20.  
  21.     def __init__(self):
  22.         x = random.random() * (window.width - self.width)
  23.         y = random.random() * (window.height - self.height)
  24.  
  25.         super(Ball, self).__init__(self.ball_image, x, y, batch=balls_batch)
  26.  
  27.         self.dx = (random.random() - 0.5) * 300
  28.         self.dy = (random.random() - 0.5) * 300
  29.  
  30.     def update(self, dt):
  31.         if self.x <= 0 or self.x + self.width >= window.width:
  32.             self.dx *= -1
  33.         if self.y <= 0 or self.y + self.height >= window.height:
  34.             self.dy *= -1
  35.         self.x += self.dx * dt
  36.         self.y += self.dy * dt
  37.  
  38.         self.x = min(max(self.x, 0), window.width - self.width)
  39.         self.y = min(max(self.y, 0), window.height - self.height)
  40.  
  41. pyglet_time = 0
  42. dt_estimated = None
  43. dt_samples = [0.0] * 100
  44. num_dt_samples = 0
  45. deviation_percent = [0] * 250
  46. fn_stat = None
  47. num_draw_calls = 0
  48.  
  49. def wait_fps_stabilization(dt):
  50.     global fn_stat
  51.     if pyglet_time > 2.0:
  52.         fn_stat = sample_for_estimation
  53.         # print(fn_stat)
  54.  
  55. def sample_for_estimation(dt):
  56.     global num_dt_samples, fn_stat, dt_estimated, num_draw_calls
  57.     dt_samples[num_dt_samples] = dt
  58.     num_dt_samples += 1
  59.     if num_dt_samples >= len(dt_samples):
  60.         dt_estimated = sum(dt_samples) / len(dt_samples)
  61.         num_dt_samples = 0
  62.         num_draw_calls = 0
  63.         fn_stat = collect_deviation
  64.         # print(fn_stat)
  65.  
  66. def collect_deviation(dt):
  67.     global num_dt_samples, deviation_percent
  68.     num_dt_samples += 1
  69.     bucket = int(abs((dt - dt_estimated) / dt_estimated) * 100)
  70.     if bucket >= len(deviation_percent):
  71.         bucket = len(deviation_percent)-1
  72.     deviation_percent[bucket] += 1
  73.  
  74. fn_stat = wait_fps_stabilization
  75.  
  76. @window.event
  77. def on_draw():
  78.     global num_draw_calls
  79.     window.clear()
  80.     num_draw_calls +=1
  81.     balls_batch.draw()
  82.  
  83. def update(dt):
  84.     global pyglet_time
  85.     pyglet_time += dt
  86.     #fn_stat(dt)
  87.     for ball in balls:
  88.         ball.update(dt)
  89.  
  90. ##@window.event
  91. ##def on_close(*args):
  92. ##    global deviation_percent, dt_estimated, num_draw_calls
  93. ##    deviation_percent = [v / num_dt_samples * 100 for v in deviation_percent]
  94. ##    print('dt_estimated:', dt_estimated)
  95. ##    print('num_draw_calls:', num_draw_calls)
  96. ##    print('num_dt_samples:', num_dt_samples)
  97. ##    print("dt diferences with estimated real dt, in %")
  98. ##    print(deviation_percent)
  99.    
  100. balls_batch = pyglet.graphics.Batch()
  101. balls = [ Ball() for i in range(10)]
  102.  
  103. # the schedule style used in pyglet's noisy.py -> smooth movement, max 2% cpu
  104. #pyglet.clock.schedule_interval(update, 1/60.)
  105.  
  106. # the schedule style update used in cocos actions -> smooth movement, 50% cpu in dual core
  107. pyglet.clock.schedule(update)
  108.  
  109. pyglet.app.run()
  110.  
  111. ## stats with deviation clamped to 100 %
  112. ##stats with schedule interval:
  113. ##[39.47001394700139, 30.264993026499305, 7.252440725244072, 4.602510460251046, 2.
  114. ##510460251046025, 1.394700139470014, 0.41841004184100417, 0.8368200836820083, 0.6
  115. ##97350069735007, 1.2552301255230125, 0.2789400278940028, 0.2789400278940028, 0.13
  116. ##94700139470014, 0.0, 0.0, 0.41841004184100417, 0.5578800557880056, 0.27894002789
  117. ##40028, 0.1394700139470014, 0.0, 0.5578800557880056, 0.0, 0.1394700139470014, 0.1
  118. ##394700139470014, 0.0, 0.0, 0.2789400278940028, 0.0, 0.2789400278940028, 0.0, 0.0
  119. ##, 0.0, 0.0, 0.1394700139470014, 0.0, 0.1394700139470014, 0.0, 0.0, 0.0, 0.139470
  120. ##0139470014, 0.8368200836820083, 2.3709902370990235, 0.9762900976290098, 0.836820
  121. ##0836820083, 0.8368200836820083, 0.9762900976290098, 0.2789400278940028, 0.0, 0.0
  122. ##, 0.1394700139470014, 0.0, 0.0, 0.0, 0.1394700139470014, 0.0, 0.0, 0.0, 0.0, 0.0
  123. ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
  124. ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
  125. ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  126. ##    
  127. ##
  128. ##stats with schedule:
  129. ##    [51.02669404517454, 10.882956878850102, 29.774127310061605, 3.1827515400410675,
  130. ##1.8480492813141685, 0.5133470225872689, 0.3080082135523614, 0.10266940451745381,
  131. ## 0.5133470225872689, 0.7186858316221766, 0.0, 0.20533880903490762, 0.0, 0.0, 0.0
  132. ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10266940451745381, 0.0, 0.0, 0.10266940451745381, 0
  133. ##.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.20533880903490762, 0.0, 0.0,
  134. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  135. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  136. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  137. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10266940
  138. ##451745381, 0.41067761806981523]
  139. ##
  140.    
  141. ## stats with deviation clamped to 250 %
  142. ##dt_estimated: 0.0302887169153 -> seria fps =33,015594645241027094409941328863
  143. ##num_draw_calls: 2074
  144. ##num_dt_samples: 2073
  145. ##dt diferences with estimated real dt, in %
  146. ##[3.2320308731307286, 8.007718282682104, 27.399903521466474, 28.268210323203085,
  147. ##8.05595754944525, 2.2672455378678245, 0.8200675349734683, 0.3376748673420164, 0.
  148. ##3376748673420164, 0.6271104679208876, 1.4954172696575012, 1.6401350699469366, 0.
  149. ##6271104679208876, 0.482392667631452, 0.2894356005788712, 0.3376748673420164, 0.2
  150. ##41196333815726, 0.1929570670525808, 0.6271104679208876, 1.0130246020260492, 0.33
  151. ##76748673420164, 0.5306319343945972, 0.0964785335262904, 0.3859141341051616, 0.14
  152. ##47178002894356, 0.2894356005788712, 0.0482392667631452, 0.1447178002894356, 0.04
  153. ##82392667631452, 0.241196333815726, 0.2894356005788712, 0.3859141341051616, 0.530
  154. ##6319343945972, 0.1929570670525808, 0.0, 0.0482392667631452, 0.0482392667631452,
  155. ##0.0964785335262904, 0.723589001447178, 1.8330921369995177, 2.894356005788712, 1.
  156. ##3989387361312107, 0.5306319343945972, 0.8200675349734683, 1.3506994693680656, 0.
  157. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0482392667631452, 0.0, 0.0482392667631452, 0.04823
  158. ##92667631452, 0.0, 0.1447178002894356, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  159. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  160. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  161. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  162. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  163. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  164. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  165. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  166. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  167. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  168. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  169. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  170. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  171.  
  172.  
  173.  
  174. ##stats with schedule:
  175. ##los fps estimados serian 59,978476672184653552338310649317
  176. ##dt_estimated: 0.0166726475143
  177. ##num_dt_samples: 2359
  178. ##dt diferences with estimated real dt, in %
  179. ##[53.624417125900806, 13.310724883425179, 23.61169987282747, 4.196693514200932, 1
  180. ##.0173802458668928, 0.5510809665112336, 0.5934718100890208, 0.3391267486222976, 0
  181. ##.3391267486222976, 0.38151759220008474, 0.1695633743111488, 0.1271725307333616,
  182. ##0.1695633743111488, 0.0847816871555744, 0.0847816871555744, 0.0423908435777872,
  183. ##0.0847816871555744, 0.0423908435777872, 0.0423908435777872, 0.0, 0.1271725307333
  184. ##616, 0.0423908435777872, 0.0423908435777872, 0.0423908435777872, 0.0, 0.04239084
  185. ##35777872, 0.0847816871555744, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.08478168
  186. ##71555744, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0423908435777872,
  187. ##0.0423908435777872, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.04239084
  188. ##35777872, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0423908435777872,
  189. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  190. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0423908435777
  191. ##872, 0.0423908435777872, 0.0, 0.0, 0.0, 0.0423908435777872, 0.0, 0.0, 0.0, 0.0,
  192. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  193. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  194. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  195. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  196. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0847816871555744, 0.0, 0.0, 0.0423908435777
  197. ##872, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0423908435777872, 0.0, 0.0, 0.0, 0.0, 0.0,
  198. ##0.0, 0.0, 0.0, 0.0423908435777872, 0.1271725307333616, 0.0, 0.0423908435777872,
  199. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  200. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  201. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.04239084
  202. ##35777872]
  203.  
  204. ## usando schedule
  205. ##C:\sync2\_working_now\pyglet schedule>py -2.7 noisy_schedule_interval.py
  206. ##dt_estimated: 0.0166772762424 corresponderia a fps = 59,961829825521413106889246354743
  207. ##num_draw_calls: 3646
  208. ##num_dt_samples: 3645
  209. ##dt diferences with estimated real dt, in %
  210. ##[50.75445816186557, 11.193415637860083, 28.31275720164609, 3.7037037037037033, 0
  211. ##.823045267489712, 0.7681755829903978, 0.43895747599451307, 0.6035665294924555, 0
  212. ##.35665294924554186, 0.5212620027434842, 0.24691358024691357, 0.19204389574759945
  213. ##, 0.1646090534979424, 0.0823045267489712, 0.19204389574759945, 0.109739368998628
  214. ##27, 0.054869684499314134, 0.0, 0.0, 0.0, 0.027434842249657067, 0.027434842249657
  215. ##067, 0.0823045267489712, 0.027434842249657067, 0.0, 0.027434842249657067, 0.0274
  216. ##34842249657067, 0.027434842249657067, 0.0823045267489712, 0.027434842249657067,
  217. ##0.054869684499314134, 0.027434842249657067, 0.0, 0.027434842249657067, 0.0274348
  218. ##42249657067, 0.0, 0.10973936899862827, 0.0, 0.054869684499314134, 0.0, 0.0548696
  219. ##84499314134, 0.054869684499314134, 0.0, 0.0, 0.0, 0.0, 0.0, 0.027434842249657067
  220. ##, 0.0, 0.027434842249657067, 0.054869684499314134, 0.027434842249657067, 0.0, 0.
  221. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.027434842249657067, 0.0, 0.054869684499314134
  222. ##, 0.027434842249657067, 0.0, 0.027434842249657067, 0.0, 0.0, 0.0, 0.0, 0.0274348
  223. ##42249657067, 0.0, 0.0, 0.027434842249657067, 0.0, 0.027434842249657067, 0.027434
  224. ##842249657067, 0.054869684499314134, 0.054869684499314134, 0.0, 0.0, 0.0, 0.0, 0.
  225. ##0, 0.027434842249657067, 0.0, 0.0, 0.0, 0.027434842249657067, 0.0, 0.0, 0.0, 0.0
  226. ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.027434842249657067, 0.0, 0.027434842
  227. ##249657067, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0274348
  228. ##42249657067, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  229. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
  230. ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0274348422
  231. ##49657067, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  232. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  233. ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0274348422496
  234. ##57067, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
  235. ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.027434842249657067, 0.0, 0.0, 0.0, 0.0, 0
  236. ##.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0
  237. ##.0, 0.0]
Advertisement
Add Comment
Please, Sign In to add comment