Difficult-Rocket/Difficult_Rocket/client/fps/fps_log.py

64 lines
1.9 KiB
Python
Raw Normal View History

2021-10-25 22:08:00 +08:00
# -------------------------------
# Difficult Rocket
2023-01-20 14:08:12 +08:00
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
2021-10-25 22:08:00 +08:00
# All rights reserved
# -------------------------------
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
2021-12-15 23:28:08 +08:00
import statistics
2021-10-25 22:08:00 +08:00
2023-02-25 09:18:41 +08:00
from typing import Union, List
2021-10-25 22:08:00 +08:00
from decimal import Decimal
2021-10-25 22:08:00 +08:00
class FpsLogger:
2023-12-03 16:54:07 +08:00
def __init__(self, stable_fps: int = 60, count: int = 700):
2021-10-25 22:08:00 +08:00
self.stable_fps = stable_fps
2021-12-15 23:28:08 +08:00
self.count = count
self._fps = stable_fps
self.middle_fps = stable_fps
2023-02-25 09:18:41 +08:00
self.fps_list: List[Union[int, float]] = [stable_fps]
self.get_fps_list: List[Union[int, float]] = [stable_fps]
2021-10-29 00:15:31 +08:00
self._max_fps = stable_fps
self._min_fps = stable_fps
2021-10-25 22:08:00 +08:00
2023-12-03 16:54:07 +08:00
def update_tick(self, pyglet_fps: float, tick: Decimal):
if pyglet_fps != 0:
self.fps_list.append(pyglet_fps)
2023-01-27 21:09:37 +08:00
elif tick == 0:
self.fps_list.append(1)
2021-12-15 23:28:08 +08:00
else:
2023-01-27 21:09:37 +08:00
self.fps_list.append(float(1 / tick))
2021-12-15 23:28:08 +08:00
if len(self.fps_list) > self.count:
self.fps_list = self.fps_list[-self.count + 1:] # 整个列表往前挪一位
2021-12-15 23:28:08 +08:00
if len(self.get_fps_list) > self.count:
self.get_fps_list = self.get_fps_list[-self.count + 1:] # 整个列表往前挪一位
2021-12-15 23:28:08 +08:00
try:
2023-12-03 16:54:07 +08:00
self._fps = statistics.geometric_mean(
self.fps_list[-100:]
) # 取最后100个值的平均值
2022-03-22 21:18:04 +08:00
self.middle_fps = statistics.median(self.fps_list) # 取中间值
2021-12-15 23:28:08 +08:00
except Exception:
print(self.fps_list)
raise
self._max_fps = max(self.fps_list)
self._min_fps = min(self.fps_list)
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
@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
@property
2021-12-15 23:28:08 +08:00
def fps(self):
return self._fps