Difficult-Rocket/Difficult_Rocket/fps/fps_log.py

70 lines
1.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
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
2021-12-15 23:28:08 +08:00
from libs.pyglet import clock
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):
2021-12-15 23:28:08 +08:00
now_fps = clock.get_fps()
if now_fps != 0:
self.fps_list.append(now_fps)
else:
self.fps_list.append(1)
if len(self.fps_list) > self.count:
self.fps_list = self.fps_list[-self.count + 1:]
if len(self.get_fps_list) > self.count:
self.get_fps_list = self.get_fps_list[-self.count + 1:]
try:
2021-12-26 23:06:03 +08:00
self._fps = statistics.geometric_mean(self.fps_list[-100:])
2021-12-15 23:28:08 +08:00
self.middle_fps = statistics.median(self.fps_list)
except Exception:
print(self.fps_list)
raise
self._max_fps = max(self.fps_list)
self._min_fps = min(self.fps_list)
# 获取新fps
2021-10-30 20:19:12 +08:00
del now_fps
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