Difficult-Rocket/Difficult_Rocket/fps/fps_log.py

104 lines
2.8 KiB
Python
Raw Normal View History

2021-10-25 22:08:00 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2021 by shenjackyuanjie
# All rights reserved
# -------------------------------
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
import time
import decimal
from decimal import Decimal
2021-10-29 00:15:31 +08:00
from ..api import new_thread
2021-10-28 06:43:35 +08:00
2021-10-25 22:08:00 +08:00
from libs import pyglet
"""
fps_list ->
[
[fps, time_ns, tick time_ns, pyglet tick]
]
"""
class FpsLogger:
def __init__(self,
stable_fps: int = 60,
wait_time: int = 5):
self.stable_fps = stable_fps
self.wait_time = wait_time
2021-10-30 20:19:12 +08:00
self.time = time.time_ns()
2021-10-25 22:08:00 +08:00
self.fps_list = [[stable_fps, time.time_ns()]]
2021-10-29 00:15:31 +08:00
self._max_fps = stable_fps
2021-10-30 20:19:12 +08:00
self._max_fps_tick = time.time_ns()
2021-10-29 00:15:31 +08:00
self._min_fps = stable_fps
2021-10-30 20:19:12 +08:00
self._min_fps_tick = time.time_ns()
2021-10-29 00:15:31 +08:00
self.check_list = True
self.update_list()
2021-10-25 22:08:00 +08:00
2021-10-28 06:43:35 +08:00
# @new_thread('fps_logger update', daemon=False, log_thread=False)
2021-10-25 22:08:00 +08:00
def update_tick(self,
tick: Decimal):
2021-10-30 20:19:12 +08:00
self.time = time.time_ns()
2021-10-25 22:08:00 +08:00
now_fps = pyglet.clock.get_fps()
2021-10-30 20:19:12 +08:00
# 获取当前信息
if now_fps > self.max_fps and not self.time - self._max_fps_tick >= self.wait_time:
self.max_fps = now_fps
2021-11-07 20:52:13 +08:00
# self.max_fps = max(self.max_fps, now_fps)
2021-10-30 20:19:12 +08:00
tick_time = self.time - self.fps_list[-1][1]
# 获取渲染用时
self.fps_list.append([now_fps, self.time, tick_time, tick])
if self.time - self.fps_list[0][1] >= self.wait_time * (10 ** 9):
2021-10-25 22:08:00 +08:00
self.fps_list.pop(0)
2021-10-30 20:19:12 +08:00
del now_fps
del tick_time
2021-10-25 22:08:00 +08:00
2021-10-29 00:15:31 +08:00
@new_thread('fps_logger check_list', daemon=True)
2021-10-28 06:43:35 +08:00
def update_list(self):
2021-10-29 00:15:31 +08:00
while self.check_list:
now_time = time.time_ns()
for fps_time in self.fps_list:
if now_time - fps_time[1] >= self.wait_time * (10 ** 9):
del self.fps_list[self.fps_list.index(fps_time)]
time.sleep(1)
2021-10-28 06:43:35 +08:00
2021-10-25 22:08:00 +08:00
@property
def max_fps(self):
2021-10-29 00:15:31 +08:00
return self._max_fps
2021-10-25 22:08:00 +08:00
2021-10-30 20:19:12 +08:00
@max_fps.setter
def max_fps(self, value):
2021-11-07 20:52:13 +08:00
# if self.time - self._max_fps_tick <= self.wait_time:
# self._max_fps = value
# self._max_fps_tick = time.time_ns()
# else:
# max_fps = self.list_max_fps
# self._max_fps = max_fps[0]
# self._max_fps_tick = max_fps[1]
self._max_fps = value
self._max_fps_tick = self.time
2021-10-30 20:19:12 +08:00
2021-10-25 22:08:00 +08:00
@property
def min_fps(self):
2021-10-29 00:15:31 +08:00
return self._min_fps
2021-10-25 22:08:00 +08:00
2021-10-30 20:19:12 +08:00
@min_fps.setter
def min_fps(self, value):
self._min_fps = value
self._min_fps_tick = time.time_ns()
@property
def list_max_fps(self):
return max(self.fps_list)
@property
def list_min_fps(self):
return min(self.fps_list)