Skip to content

Commit

Permalink
log_fps
Browse files Browse the repository at this point in the history
  • Loading branch information
abrichr committed Apr 19, 2023
1 parent b4b51da commit 0e058f6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
14 changes: 2 additions & 12 deletions puterbot/replay.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
from pprint import pformat
import importlib
import time

from loguru import logger
from pynput import keyboard, mouse
import fire

from puterbot.crud import (
get_latest_recording,
)
from puterbot.utils import (
configure_logging,
get_strategy_class_by_name,
)
from puterbot.crud import get_latest_recording
from puterbot.utils import configure_logging, get_strategy_class_by_name


LOG_LEVEL = "INFO"
Expand Down
24 changes: 21 additions & 3 deletions puterbot/strategies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,33 @@
"""

from abc import ABC, abstractmethod
import time

from loguru import logger
from pynput import keyboard, mouse
import mss.base
import numpy as np

from puterbot.models import Recording, InputEvent
from puterbot.playback import play_input_event
from puterbot.utils import get_screenshot


MAX_FRAME_TIMES = 1000


class BaseReplayStrategy(ABC):

def __init__(
self,
recording: Recording,
max_frame_times: int = MAX_FRAME_TIMES,
):
self.recording = recording
self.max_frame_times = max_frame_times
self.input_events = []
self.screenshots = []
self.frame_times = []

@abstractmethod
def get_next_input_event(
Expand All @@ -29,9 +38,7 @@ def get_next_input_event(
) -> InputEvent:
pass

def run(
self,
):
def run(self):
keyboard_controller = keyboard.Controller()
mouse_controller = mouse.Controller()
while True:
Expand All @@ -41,10 +48,21 @@ def run(
input_event = self.get_next_input_event(screenshot)
except StopIteration:
break
self.log_fps()
self.input_events.append(input_event)
if input_event:
play_input_event(
input_event,
mouse_controller,
keyboard_controller,
)

def log_fps(self):
t = time.time()
self.frame_times.append(t)
dts = np.diff(self.frame_times)
mean_dt = np.mean(dts)
fps = len(dts) / mean_dt
logger.info(f"{fps=:.2f}")
if len(self.frame_times) > self.max_frame_times:
self.frame_times.pop(0)

0 comments on commit 0e058f6

Please sign in to comment.