-
Notifications
You must be signed in to change notification settings - Fork 38
Introducing Framecounter for measuring FPS #799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e933af7
added framecounter
jfboeve 1e2779b
tweaking property names, change framecount payload
jfboeve 9ac1f0f
make boundaries and fps interval setable during renderer creation / r…
jfboeve 07692f5
sort boundaries in ascending order
jfboeve 0d6514b
fixed co-pilot comments, cleaned up calculateFPS function
jfboeve 5b19bf1
test: add fps unit coverage requested in review
Copilot 0b7fe63
use explicit !== undefined
jfboeve 6f20901
use boundaries in docs, instead of buckets
jfboeve 2ba3331
clone newBounderies before sorting
jfboeve 37d379a
set this.options.fpsBoundaries on updateFpsBounderies
jfboeve 0caf5fc
Set this.options.fpsUpdateInterval on updateFpsUpdateInterval
jfboeve deea92f
Fix FPS calculation to use actual elapsed time with Math.round
Copilot 6989e22
updated FpsUpdatePayload type
jfboeve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * If not stated otherwise in this file or this component's LICENSE file the | ||
| * following copyright and licenses apply: | ||
| * | ||
| * Copyright 2026 Comcast Cable Communications Management, LLC. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the License); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { beforeEach, describe, expect, it } from 'vitest'; | ||
| import { createFrameCounter, setFpsBoundaries, setFpsInterval } from './fps.js'; | ||
|
|
||
| describe('fps', () => { | ||
| beforeEach(() => { | ||
| setFpsBoundaries([20, 40, 60, 80, 100]); | ||
| setFpsInterval(1000); | ||
| }); | ||
|
|
||
| it('assigns frames to matching buckets and overflow', () => { | ||
| setFpsBoundaries([20, 40]); | ||
| const counter = createFrameCounter(0); | ||
|
|
||
| counter.increment(10); | ||
| counter.increment(20); | ||
| counter.increment(21); | ||
| counter.increment(40); | ||
| counter.increment(41); | ||
|
|
||
| expect(counter.count[20]).toBe(2); | ||
| expect(counter.count[40]).toBe(2); | ||
| expect(counter.count.overflow).toBe(1); | ||
| expect(counter.total).toBe(5); | ||
| }); | ||
|
|
||
| it('calculates averageFps from total frames and elapsed interval', () => { | ||
| setFpsInterval(2000); | ||
| const counter = createFrameCounter(100); | ||
|
|
||
| for (let i = 0; i < 10; i++) { | ||
| counter.increment(16); | ||
| } | ||
|
|
||
| expect(counter.averageFps).toBe(5); | ||
| }); | ||
|
|
||
| it('keeps counter boundaries and interval when global settings change', () => { | ||
| setFpsBoundaries([10, 20]); | ||
| setFpsInterval(1000); | ||
| const firstCounter = createFrameCounter(0); | ||
|
|
||
| setFpsBoundaries([30, 60]); | ||
| setFpsInterval(2000); | ||
| const secondCounter = createFrameCounter(100); | ||
|
|
||
| firstCounter.increment(15); | ||
| secondCounter.increment(15); | ||
|
|
||
| expect(firstCounter.count[20]).toBe(1); | ||
| expect(firstCounter.count.overflow).toBe(0); | ||
| expect(firstCounter.end).toBe(1000); | ||
| expect(secondCounter.count[30]).toBe(1); | ||
| expect(secondCounter.count.overflow).toBe(0); | ||
| expect(secondCounter.end).toBe(2100); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * If not stated otherwise in this file or this component's LICENSE file the | ||
| * following copyright and licenses apply: | ||
| * | ||
| * Copyright 2023 Comcast Cable Communications Management, LLC. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the License); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| export interface FrameCount { | ||
| /** | ||
| * The boundaries for the frame count buckets. | ||
| */ | ||
| boundaries: number[]; | ||
| /** | ||
| * The count of frames in each bucket. | ||
| */ | ||
| count: Record<number | string, number>; | ||
| /** | ||
| * The total number of frames counted. | ||
| */ | ||
| total: number; | ||
| } | ||
|
jfboeve marked this conversation as resolved.
|
||
|
|
||
| export type FrameCounter = FrameCount & { | ||
| /** | ||
| * The start time of the frame counting interval. | ||
| */ | ||
| start: number; | ||
| /** | ||
| * The end time of the frame counting interval. | ||
| */ | ||
| end: number; | ||
| /** | ||
| * Increments the frame count based on the provided frame delta time. | ||
| * @param frameDelta - The time in milliseconds it took to render the last frame. | ||
| */ | ||
| increment: (frameDelta: number) => void; | ||
| /** | ||
| * Calculates and returns the average frames per second (FPS) based on the total frames counted and the elapsed time. | ||
| * @returns The average FPS. | ||
| */ | ||
| get averageFps(): number; | ||
| }; | ||
|
|
||
| let fpsBoundaries = [20, 40, 60, 80, 100]; | ||
| let fpsInterval = 1000; // 1 second | ||
|
|
||
|
jfboeve marked this conversation as resolved.
|
||
| const frameCounter: FrameCounter = { | ||
| start: 0, | ||
| end: 0, | ||
| total: 0, | ||
| boundaries: [], | ||
| count: {}, | ||
| increment(frameDelta: number) { | ||
| this.total++; | ||
| const boundaries = this.boundaries; | ||
| for (let i = 0; i < boundaries.length; i++) { | ||
| const bucket = boundaries[i] as number; | ||
| if (frameDelta <= bucket) { | ||
| this.count[bucket]!++; | ||
| return; | ||
| } | ||
| } | ||
| this.count['overflow']!++; | ||
| }, | ||
| get averageFps() { | ||
| //calculate fps based on frame count and elapsed time | ||
| return (this.total / (this.end - this.start)) * 1000; | ||
| }, | ||
| }; | ||
|
|
||
| export function setFpsBoundaries(newBoundaries: number[]) { | ||
| // sort buckets in ascending order just in case | ||
| fpsBoundaries = newBoundaries.slice().sort((a, b) => a - b); | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| export function setFpsInterval(newInterval: number) { | ||
| fpsInterval = newInterval; | ||
| } | ||
|
|
||
| export function createFrameCounter(frameTime: number): FrameCounter { | ||
| const counter = Object.create(frameCounter) as FrameCounter; | ||
| counter.boundaries = fpsBoundaries; | ||
| counter.start = frameTime; | ||
| counter.end = frameTime + fpsInterval; | ||
| counter.total = 0; | ||
| counter.count = Object.create(null) as Record<number | string, number>; | ||
| //fill frames with 0 for each bucket | ||
| for (let i = 0; i < fpsBoundaries.length; i++) { | ||
| const bucket = fpsBoundaries[i] as number; | ||
| counter.count[bucket] = 0; | ||
| } | ||
| counter.count['overflow'] = 0; | ||
| return counter; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.