Skip to content

Commit 1cd04f3

Browse files
rickstaaecmulli
andcommitted
refactor: improve average fps stats format
This commit adds the metric timestamp in the `minute_fps_array` so it can be used by the client to plot the data or perform calculations. Co-authored-by: Evan Mullins <evancmullins@gmail.com>
1 parent 0fdff79 commit 1cd04f3

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

server/app.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def __init__(self, track: MediaStreamTrack, pipeline: Pipeline):
5959

6060
self._lock = asyncio.Lock()
6161
self._fps_interval_frame_count = 0
62-
self._last_fps_calculation_time = time.monotonic()
63-
self._fps_loop_start_time = self._last_fps_calculation_time
62+
self._last_fps_calculation_time = None
63+
self._fps_loop_start_time = time.monotonic()
6464
self._fps = 0.0
6565
self._fps_measurements = deque(maxlen=60)
6666
self._running_event = asyncio.Event()
@@ -84,19 +84,22 @@ async def _calculate_fps_loop(self):
8484
await self._running_event.wait()
8585
self._fps_loop_start_time = time.monotonic()
8686
while self.readyState != "ended":
87-
await asyncio.sleep(1) # Calculate FPS every second.
8887
async with self._lock:
89-
current_time_monotonic = time.monotonic()
90-
time_diff = current_time_monotonic - self._last_fps_calculation_time
91-
if time_diff > 0:
88+
current_time = time.monotonic()
89+
if self._last_fps_calculation_time is not None:
90+
time_diff = current_time - self._last_fps_calculation_time
9291
self._fps = self._fps_interval_frame_count / time_diff
9392
self._fps_measurements.append(
94-
self._fps
95-
) # Store the FPS measurement
93+
{
94+
"timestamp": current_time - self._fps_loop_start_time,
95+
"fps": self._fps,
96+
}
97+
) # Store the FPS measurement with timestamp
9698

97-
# Reset start_time and frame_count for the next interval.
98-
self._last_fps_calculation_time = current_time_monotonic
99-
self._fps_interval_frame_count = 0
99+
# Reset start_time and frame_count for the next interval.
100+
self._last_fps_calculation_time = current_time
101+
self._fps_interval_frame_count = 0
102+
await asyncio.sleep(1) # Calculate FPS every second.
100103

101104
@property
102105
async def fps(self) -> float:
@@ -128,7 +131,9 @@ async def average_fps(self) -> float:
128131
async with self._lock:
129132
if not self._fps_measurements:
130133
return 0.0
131-
return sum(self._fps_measurements) / len(self._fps_measurements)
134+
return sum(
135+
measurement["fps"] for measurement in self._fps_measurements
136+
) / len(self._fps_measurements)
132137

133138
@property
134139
async def last_fps_calculation_time(self) -> float:
@@ -256,7 +261,6 @@ def on_datachannel(channel):
256261
async def on_message(message):
257262
try:
258263
params = json.loads(message)
259-
260264
if params.get("type") == "get_nodes":
261265
nodes_info = await pipeline.get_nodes_info()
262266
response = {
@@ -405,7 +409,9 @@ async def on_shutdown(app: web.Application):
405409
# Add routes for getting stream statistics.
406410
stream_stats = StreamStats(app)
407411
app.router.add_get("/streams/stats", stream_stats.collect_all_stream_metrics)
408-
app.router.add_get("/stream/{stream_id}/stats", stream_stats.collect_stream_metrics_by_id)
412+
app.router.add_get(
413+
"/stream/{stream_id}/stats", stream_stats.collect_stream_metrics_by_id
414+
)
409415

410416
# Add hosted platform route prefix.
411417
# NOTE: This ensures that the local and hosted experiences have consistent routes.

0 commit comments

Comments
 (0)