summaryrefslogtreecommitdiff
path: root/python/openvino/runtime/common/demo_utils/src/performance_metrics.cpp
blob: d1e494ebb15e441c6cd038661235d1ce9dbfbda5 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (C) 2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <limits>
#include "utils/performance_metrics.hpp"
#include "utils/slog.hpp"

// timeWindow defines the length of the timespan over which the 'current fps' value is calculated
PerformanceMetrics::PerformanceMetrics(Duration timeWindow)
    : timeWindowSize(timeWindow)
    , firstFrameProcessed(false)
{}

void PerformanceMetrics::update(TimePoint lastRequestStartTime,
    const cv::Mat& frame,
    cv::Point position,
    int fontFace,
    double fontScale,
    cv::Scalar color,
    int thickness,
    MetricTypes metricType) {
    update(lastRequestStartTime);
    paintMetrics(frame, position, fontFace, fontScale, color, thickness, metricType);
}

void PerformanceMetrics::update(TimePoint lastRequestStartTime) {
    TimePoint currentTime = Clock::now();

    if (!firstFrameProcessed) {
        lastUpdateTime = lastRequestStartTime;
        firstFrameProcessed = true;
    }

    currentMovingStatistic.latency += currentTime - lastRequestStartTime;
    currentMovingStatistic.period = currentTime - lastUpdateTime;
    currentMovingStatistic.frameCount++;

    if (currentTime - lastUpdateTime > timeWindowSize) {
        lastMovingStatistic = currentMovingStatistic;
        totalStatistic.combine(lastMovingStatistic);
        currentMovingStatistic = Statistic();

        lastUpdateTime = currentTime;
    }
}

void PerformanceMetrics::paintMetrics(const cv::Mat& frame, cv::Point position, int fontFace,
    double fontScale, cv::Scalar color, int thickness, MetricTypes metricType) const {
    // Draw performance stats over frame
    Metrics metrics = getLast();

    std::ostringstream out;
    if (!std::isnan(metrics.latency) &&
        (metricType == PerformanceMetrics::MetricTypes::LATENCY || metricType == PerformanceMetrics::MetricTypes::ALL)) {
        out << "Latency: " << std::fixed << std::setprecision(1) << metrics.latency << " ms";
        putHighlightedText(frame, out.str(), position, fontFace, fontScale, color, thickness);
    }
    if (!std::isnan(metrics.fps) &&
        (metricType == PerformanceMetrics::MetricTypes::FPS || metricType == PerformanceMetrics::MetricTypes::ALL)) {
        out.str("");
        out << "FPS: " << std::fixed << std::setprecision(1) << metrics.fps;
        int offset = metricType == PerformanceMetrics::MetricTypes::ALL ? 30 : 0;
        putHighlightedText(frame, out.str(), {position.x, position.y + offset}, fontFace, fontScale, color, thickness);
    }
}

PerformanceMetrics::Metrics PerformanceMetrics::getLast() const {
    Metrics metrics;

    metrics.latency = lastMovingStatistic.frameCount != 0
                      ? std::chrono::duration_cast<Ms>(lastMovingStatistic.latency).count()
                        / lastMovingStatistic.frameCount
                      : std::numeric_limits<double>::signaling_NaN();
    metrics.fps = lastMovingStatistic.period != Duration::zero()
                  ? lastMovingStatistic.frameCount
                    / std::chrono::duration_cast<Sec>(lastMovingStatistic.period).count()
                  : std::numeric_limits<double>::signaling_NaN();

    return metrics;
}

PerformanceMetrics::Metrics PerformanceMetrics::getTotal() const {
    Metrics metrics;

    int frameCount = totalStatistic.frameCount + currentMovingStatistic.frameCount;
    if (frameCount != 0) {
        metrics.latency = std::chrono::duration_cast<Ms>(
            totalStatistic.latency + currentMovingStatistic.latency).count() / frameCount;
        metrics.fps = frameCount / std::chrono::duration_cast<Sec>(
                                       totalStatistic.period + currentMovingStatistic.period).count();
    } else {
        metrics.latency = std::numeric_limits<double>::signaling_NaN();
        metrics.fps = std::numeric_limits<double>::signaling_NaN();
    }

    return metrics;
}

void PerformanceMetrics::logTotal() const {
    Metrics metrics = getTotal();

    slog::info << "\tLatency: " << std::fixed << std::setprecision(1) << metrics.latency << " ms" << slog::endl;
    slog::info << "\tFPS: " << metrics.fps << slog::endl;
}

void logLatencyPerStage(double readLat, double preprocLat, double inferLat, double postprocLat, double renderLat) {
    slog::info << "\tDecoding:\t" << std::fixed << std::setprecision(1) <<
        readLat << " ms" << slog::endl;
    slog::info << "\tPreprocessing:\t" << preprocLat << " ms" << slog::endl;
    slog::info << "\tInference:\t" << inferLat << " ms" << slog::endl;
    slog::info << "\tPostprocessing:\t" << postprocLat << " ms" << slog::endl;
    slog::info << "\tRendering:\t" << renderLat << " ms" << slog::endl;
}