-
Notifications
You must be signed in to change notification settings - Fork 0
PerformanceMetrics Class Documentation
The PerformanceMetrics
class is designed to measure and report performance metrics, such as latency and frames per second (FPS), for processes that involve frame processing. It is particularly useful for real-time applications, such as video processing or inference engines, where monitoring performance over time is critical.
- Latency Measurement: Calculates the average time taken to process each frame.
- FPS Calculation: Computes the frames per second (FPS) over a moving time window.
- Customizable Display: Allows rendering of performance metrics directly on video frames with customizable text properties.
- Metric Logging: Provides functions to log metrics for later analysis.
-
Clock
: Alias forstd::chrono::steady_clock
, used for high-resolution timing. -
TimePoint
: Alias forstd::chrono::time_point<Clock>
, representing a specific point in time. -
Duration
: Alias forClock::duration
, representing a span of time. -
Ms
: Alias forstd::chrono::duration<double, std::ratio<1, 1000>>
, representing milliseconds. -
Sec
: Alias forstd::chrono::duration<double, std::ratio<1, 1>>
, representing seconds. -
Metrics
: Struct containing two double values:latency
andfps
. -
MetricTypes
: Enum class with the following values:-
ALL
: Measures and displays both latency and FPS. -
FPS
: Measures and displays FPS only. -
LATENCY
: Measures and displays latency only.
-
-
PerformanceMetrics(Duration timeWindow = std::chrono::seconds(1))
-
Parameters:
-
timeWindow
: The duration over which the FPS is calculated, defaulting to 1 second.
-
-
Description: Initializes the
PerformanceMetrics
object with a specified time window for FPS calculation.
-
-
void update(TimePoint lastRequestStartTime, const cv::Mat& frame, cv::Point position = {15, 30}, int fontFace = cv::FONT_HERSHEY_COMPLEX, double fontScale = 0.75, cv::Scalar color = {200, 10, 10}, int thickness = 2, MetricTypes metricType = ALL)
-
Parameters:
-
lastRequestStartTime
: The timestamp when the last frame processing started. -
frame
: The OpenCV matrix (image) on which the metrics will be drawn. -
position
: The position where the metrics text will be displayed on the frame. -
fontFace
: The font type used for displaying text. -
fontScale
: The scale factor applied to the font size. -
color
: The color of the text. -
thickness
: The thickness of the text. -
metricType
: Specifies whether to display both latency and FPS, only FPS, or only latency.
-
-
Description: Updates the performance metrics based on the provided start time and optionally paints the metrics on the given frame.
-
-
void update(TimePoint lastRequestStartTime)
-
Parameters:
-
lastRequestStartTime
: The timestamp when the last frame processing started.
-
-
Description: Updates the performance metrics without rendering them on a frame. This method is used when only metrics calculation is needed.
-
-
void paintMetrics(const cv::Mat& frame, cv::Point position = {15, 30}, int fontFace = cv::FONT_HERSHEY_COMPLEX, double fontScale = 0.75, cv::Scalar color = {200, 10, 10}, int thickness = 2, MetricTypes metricType = ALL) const
-
Parameters:
-
frame
: The OpenCV matrix (image) on which the metrics will be drawn. -
position
: The position where the metrics text will be displayed on the frame. -
fontFace
: The font type used for displaying text. -
fontScale
: The scale factor applied to the font size. -
color
: The color of the text. -
thickness
: The thickness of the text. -
metricType
: Specifies whether to display both latency and FPS, only FPS, or only latency.
-
-
Description: Renders the calculated performance metrics (latency and/or FPS) on the provided video frame.
-
-
Metrics getLast() const
-
Returns: A
Metrics
struct containing the most recent latency and FPS values. -
Description: Retrieves the latest calculated performance metrics (latency and FPS) based on the most recent time window.
-
-
Metrics getTotal() const
-
Returns: A
Metrics
struct containing the total average latency and FPS over the entire period since thePerformanceMetrics
object was created. -
Description: Provides the overall performance metrics (latency and FPS) calculated from the start of monitoring.
-
-
void logTotal() const
- Description: Logs the total performance metrics (latency and FPS) to the standard output or a log file. This is useful for performance analysis and debugging.
-
Duration timeWindowSize
: The duration over which the FPS is calculated. -
Statistic lastMovingStatistic
: Stores the metrics for the last completed time window. -
Statistic currentMovingStatistic
: Stores the metrics for the current ongoing time window. -
Statistic totalStatistic
: Accumulates the metrics over the entire monitoring period. -
TimePoint lastUpdateTime
: The timestamp of the last update to the metrics. -
bool firstFrameProcessed
: A flag indicating whether the first frame has been processed.
#include "performance_metrics.hpp"
#include <opencv2/opencv.hpp>
int main() {
// Initialize the performance metrics with a 1-second time window
PerformanceMetrics metrics(std::chrono::seconds(1));
// Simulate processing loop
while (true) {
auto startTime = PerformanceMetrics::Clock::now();
// Simulate frame processing here...
// Update metrics
metrics.update(startTime);
// Optionally, display metrics on a frame
cv::Mat frame;
metrics.paintMetrics(frame);
// Log total metrics at the end of processing
metrics.logTotal();
}
return 0;
}
-
void logLatencyPerStage(double readLat, double preprocLat, double inferLat, double postprocLat, double renderLat)
-
Parameters:
-
readLat
: Latency of the decoding stage. -
preprocLat
: Latency of the preprocessing stage. -
inferLat
: Latency of the inference stage. -
postprocLat
: Latency of the postprocessing stage. -
renderLat
: Latency of the rendering stage.
-
-
Description: Logs the latency for each processing stage individually. This is useful for detailed performance analysis across different stages of the processing pipeline.
-
-
OpenCV: The
PerformanceMetrics
class uses OpenCV for rendering the metrics on video frames. - slog: Utility for logging metrics and other information to standard output or log files.
- The
PerformanceMetrics
class is platform-independent and can be used on any system where OpenCV is supported.