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

67 lines
1.9 KiB
Python
Raw Normal View History

2021-10-25 22:08:00 +08:00
# -------------------------------
# Difficult Rocket
2021-12-26 23:06:03 +08:00
# Copyright © 2021-2022 by shenjackyuanjie
2021-10-25 22:08:00 +08:00
# All rights reserved
# -------------------------------
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
import time
2021-12-15 23:28:08 +08:00
import statistics
2021-10-25 22:08:00 +08:00
2021-12-15 23:28:08 +08:00
from typing import Union
2021-10-25 22:08:00 +08:00
from decimal import Decimal
2022-06-04 11:08:30 +08:00
from libs.pyglet.clock import get_frequency
2021-10-25 22:08:00 +08:00
class FpsLogger:
def __init__(self,
stable_fps: int = 60,
2021-12-26 23:06:03 +08:00
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
self.fps_list = [stable_fps] # type: list[Union[int, float]]
self.get_fps_list = [stable_fps] # type: list[Union[int, float]]
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
2021-12-15 23:28:08 +08:00
2021-10-25 22:08:00 +08:00
def update_tick(self,
tick: Decimal):
2022-06-04 11:08:30 +08:00
now_fps = get_frequency()
2021-12-15 23:28:08 +08:00
if now_fps != 0:
self.fps_list.append(now_fps)
else:
self.fps_list.append(1)
if len(self.fps_list) > self.count:
2022-03-22 21:18:04 +08:00
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:
2022-03-22 21:18:04 +08:00
self.get_fps_list = self.get_fps_list[-self.count + 1:] # 整个列表往前挪一位
2021-12-15 23:28:08 +08:00
try:
2022-03-22 21:18:04 +08:00
self._fps = statistics.geometric_mean(self.fps_list[-100:]) # 取最后100个值的平均值
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