Record FPS samples
This commit is contained in:
		@@ -3,6 +3,7 @@
 | 
			
		||||
from .RAIILock import RAIILock
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
from collections import deque
 | 
			
		||||
import math
 | 
			
		||||
import os
 | 
			
		||||
import threading
 | 
			
		||||
@@ -20,6 +21,8 @@ class TextCanvas:
 | 
			
		||||
	__DRAW_RESOLUTION = 3
 | 
			
		||||
	__DEFAULT_TEXT_HEIGHT_TO_WIDTH_RATIO = 2.0
 | 
			
		||||
	
 | 
			
		||||
	__MAX_FPS_SAMPLES = 100
 | 
			
		||||
	
 | 
			
		||||
	def __init__(
 | 
			
		||||
		self,
 | 
			
		||||
		output_width=None, output_height=None, text_to_height_width=None,
 | 
			
		||||
@@ -62,6 +65,49 @@ class TextCanvas:
 | 
			
		||||
		self.__render_string = ""
 | 
			
		||||
		self.__is_dirty = True
 | 
			
		||||
		self.__last_render_time = time.time()
 | 
			
		||||
		self.__last_get_render_time = time.time()
 | 
			
		||||
		self.__fps_samples = deque(maxlen=self.__MAX_FPS_SAMPLES)
 | 
			
		||||
		
 | 
			
		||||
		self.__conveyor_enabled = False
 | 
			
		||||
		self.__conveyor_thread = None
 | 
			
		||||
		self._start_conveyor()
 | 
			
		||||
	
 | 
			
		||||
	def __exit__(self, exc_type, exc_val, exc_tb):
 | 
			
		||||
		
 | 
			
		||||
		self.shutdown()
 | 
			
		||||
	
 | 
			
		||||
	def shutdown(self):
 | 
			
		||||
		
 | 
			
		||||
		self._stop_conveyor()
 | 
			
		||||
	
 | 
			
		||||
	def _start_conveyor(self, acquire_locks=True):
 | 
			
		||||
		
 | 
			
		||||
		with RAIILock(self.__lock, defer=not acquire_locks):
 | 
			
		||||
			
 | 
			
		||||
			self._stop_conveyor(acquire_locks=False)
 | 
			
		||||
			
 | 
			
		||||
			self.__conveyor_enabled = True
 | 
			
		||||
			self.__conveyor_thread = threading.Thread(target=self._conveyor)
 | 
			
		||||
			self.__conveyor_thread.start()
 | 
			
		||||
		
 | 
			
		||||
	def _stop_conveyor(self, acquire_locks=True):
 | 
			
		||||
		
 | 
			
		||||
		with RAIILock(self.__lock, defer=not acquire_locks):
 | 
			
		||||
			
 | 
			
		||||
			if self.__conveyor_thread:
 | 
			
		||||
				
 | 
			
		||||
				self.__conveyor_enabled = False
 | 
			
		||||
				self.__conveyor_thread.join()
 | 
			
		||||
				self.__conveyor_thread = None
 | 
			
		||||
	
 | 
			
		||||
	def _conveyor(self):
 | 
			
		||||
		
 | 
			
		||||
		# So far simply does maintenance once per second
 | 
			
		||||
		while self.__conveyor_enabled is True:
 | 
			
		||||
			
 | 
			
		||||
			self.__fps_samples.append(0)
 | 
			
		||||
			
 | 
			
		||||
			time.sleep(1)
 | 
			
		||||
	
 | 
			
		||||
	def _update_virtual_ratios(self, acquire_locks=True):
 | 
			
		||||
		
 | 
			
		||||
@@ -105,10 +151,35 @@ class TextCanvas:
 | 
			
		||||
		
 | 
			
		||||
		with RAIILock(self.__lock, defer=not acquire_locks):
 | 
			
		||||
			
 | 
			
		||||
			self._record_frame_get(acquire_locks=False)
 | 
			
		||||
			self._update_render_if_dirty(acquire_locks=False)
 | 
			
		||||
			
 | 
			
		||||
			return self.__render_string
 | 
			
		||||
	
 | 
			
		||||
	def _record_frame_get(self, acquire_locks=True):
 | 
			
		||||
		
 | 
			
		||||
		with RAIILock(self.__lock, defer=not acquire_locks):
 | 
			
		||||
			
 | 
			
		||||
			if len(self.__fps_samples) == 0:
 | 
			
		||||
				return
 | 
			
		||||
			
 | 
			
		||||
			self.__fps_samples[0] += 1
 | 
			
		||||
	
 | 
			
		||||
	def get_fps(self, acquire_locks=True):
 | 
			
		||||
		
 | 
			
		||||
		with RAIILock(self.__lock, defer=not acquire_locks):
 | 
			
		||||
			
 | 
			
		||||
			if len(self.__fps_samples) == 0:
 | 
			
		||||
				return 0
 | 
			
		||||
			
 | 
			
		||||
			fps = 0
 | 
			
		||||
			for f in self.__fps_samples:
 | 
			
		||||
				fps += f
 | 
			
		||||
			
 | 
			
		||||
			fps /= len(self.__fps_samples)
 | 
			
		||||
			
 | 
			
		||||
			return fps
 | 
			
		||||
	
 | 
			
		||||
	def print(self, acquire_locks=True):
 | 
			
		||||
		
 | 
			
		||||
		with RAIILock(self.__lock, defer=not acquire_locks):
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user