-
Notifications
You must be signed in to change notification settings - Fork 158
Added queue.getFps() function and tests for this function #1229
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
base: v3_develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| // std | ||
| #include <chrono> | ||
| #include <deque> | ||
| #include <iostream> | ||
|
|
||
| // project | ||
|
|
@@ -70,6 +71,37 @@ unsigned int MessageQueue::isFull() const { | |
| return queue.isFull(); | ||
| } | ||
|
|
||
| float MessageQueue::getFps() { | ||
| std::unique_lock<std::mutex> lock(callbacksMtx); | ||
|
|
||
| // Get current time | ||
| auto now = std::chrono::steady_clock::now(); | ||
| auto threshold = now - std::chrono::seconds(2); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason behind the treshold? I would set a max queue size instead so it's more consistent for low and high FPS
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @moratom well if you have script node and queue suddenly stops receiving messages, it would still report last FPS. Instead this logic removes messages older than 2sec so you get real FPS. |
||
|
|
||
| // Remove timestamps older than 2 seconds | ||
| while (!fpsQueue.empty() && fpsQueue.front() < threshold) { | ||
| fpsQueue.pop_front(); | ||
| } | ||
|
|
||
| // If fewer than 2 timestamps are in queue, not enough data to compute FPS | ||
| if(fpsQueue.size() < 2) { | ||
| return 0.0; | ||
| } | ||
|
|
||
| auto oldest = fpsQueue.front(); | ||
| auto newest = fpsQueue.back(); | ||
| auto diff = std::chrono::duration<float>(newest - oldest).count(); // seconds | ||
|
|
||
| // If diff is extremely small, avoid dividing by zero | ||
| if(diff <= 0.0) { | ||
| return 0.0; | ||
| } | ||
| // Using (N - 1) frames over 'diff' seconds | ||
| // or (N) messages over 'diff' seconds—both approaches are common. | ||
| // This calculates how many frames we got over that time window. | ||
| return (fpsQueue.size() - 1) / diff; | ||
| } | ||
|
|
||
| int MessageQueue::addCallback(std::function<void(std::string, std::shared_ptr<ADatatype>)> callback) { | ||
| // Lock first | ||
| std::unique_lock<std::mutex> lock(callbacksMtx); | ||
|
|
@@ -111,6 +143,15 @@ void MessageQueue::send(const std::shared_ptr<ADatatype>& msg) { | |
| callCallbacks(msg); | ||
| auto queueNotClosed = queue.push(msg); | ||
| if(!queueNotClosed) throw QueueException(CLOSED_QUEUE_MESSAGE); | ||
|
|
||
| // Record the timestamp for FPS calculation | ||
| { | ||
| auto now = std::chrono::steady_clock::now(); | ||
| fpsQueue.push_back(now); | ||
| if(fpsQueue.size() > FPS_QUEUE_MAX_SIZE) { | ||
| fpsQueue.pop_front(); | ||
| } | ||
| } | ||
| } | ||
Erol444 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| bool MessageQueue::send(const std::shared_ptr<ADatatype>& msg, std::chrono::milliseconds timeout) { | ||
|
|
@@ -119,6 +160,13 @@ bool MessageQueue::send(const std::shared_ptr<ADatatype>& msg, std::chrono::mill | |
| if(queue.isDestroyed()) { | ||
| throw QueueException(CLOSED_QUEUE_MESSAGE); | ||
| } | ||
| { | ||
| auto now = std::chrono::steady_clock::now(); | ||
| fpsQueue.push_back(now); | ||
| if(fpsQueue.size() > FPS_QUEUE_MAX_SIZE) { | ||
| fpsQueue.pop_front(); | ||
| } | ||
| } | ||
| return queue.tryWaitAndPush(msg, timeout); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.