summaryrefslogtreecommitdiff
path: root/python/openvino/runtime/common/demo_utils/include/utils/performance_metrics.hpp
blob: 6c728b024db2bceb5e6f8d2adbea7f0224c1a6d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (C) 2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

/**
 * @brief a header file for performance metrics calculation class
 * @file performance_metrics.hpp
 */

#pragma once

#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>

#include "utils/ocv_common.hpp"

class PerformanceMetrics {
public:
    using Clock = std::chrono::steady_clock;
    using TimePoint = std::chrono::time_point<Clock>;
    using Duration = Clock::duration;
    using Ms = std::chrono::duration<double, std::ratio<1, 1000>>;
    using Sec = std::chrono::duration<double, std::ratio<1, 1>>;

    struct Metrics {
        double latency;
        double fps;
    };

    enum MetricTypes {
        ALL,
        FPS,
        LATENCY
    };

    PerformanceMetrics(Duration timeWindow = std::chrono::seconds(1));
    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);
    void update(TimePoint lastRequestStartTime);

    /// Paints metrics over provided mat
    /// @param frame frame to paint over
    /// @param position left top corner of text block
    /// @param fontScale font scale
    /// @param color font color
    /// @param thickness font thickness
    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;

    Metrics getLast() const;
    Metrics getTotal() const;
    void logTotal() const;

private:
    struct Statistic {
        Duration latency;
        Duration period;
        int frameCount;

        Statistic() {
            latency = Duration::zero();
            period = Duration::zero();
            frameCount = 0;
        }

        void combine(const Statistic& other) {
            latency += other.latency;
            period += other.period;
            frameCount += other.frameCount;
        }
    };

    Duration timeWindowSize;
    Statistic lastMovingStatistic;
    Statistic currentMovingStatistic;
    Statistic totalStatistic;
    TimePoint lastUpdateTime;
    bool firstFrameProcessed;
};

void logLatencyPerStage(double readLat, double preprocLat, double inferLat, double postprocLat, double renderLat);