mirror of https://github.com/JoshKarpel/spiel
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
953 B
Python
33 lines
953 B
Python
from collections import deque
|
|
from time import monotonic
|
|
from typing import Deque, Optional
|
|
|
|
from .constants import TARGET_RPS
|
|
|
|
|
|
class RPSCounter:
|
|
def __init__(self, render_history_length: Optional[int] = None) -> None:
|
|
if render_history_length is None:
|
|
render_history_length = 3 * TARGET_RPS
|
|
|
|
self.render_time_history: Deque[float] = deque(maxlen=render_history_length)
|
|
|
|
@property
|
|
def num_samples(self) -> int:
|
|
return len(self.render_time_history)
|
|
|
|
def mark(self) -> None:
|
|
self.render_time_history.append(monotonic())
|
|
|
|
def renders_per_second(self) -> float:
|
|
if self.num_samples < 2:
|
|
return 0
|
|
|
|
return self.num_samples / (self.render_time_history[-1] - self.render_time_history[0])
|
|
|
|
def last_elapsed_render_time(self) -> float:
|
|
if self.num_samples < 2:
|
|
return 0
|
|
|
|
return self.render_time_history[-1] - self.render_time_history[-2]
|