Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_latency_tracker.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Bernhard Liebl <poke1024@gmx.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#ifndef KRITA_KIS_SCALAR_TRACKER_H
8#define KRITA_KIS_SCALAR_TRACKER_H
9
10#include "kis_shared.h"
11#include <kritaglobal_export.h>
12
13#include <QQueue>
14#include <QElapsedTimer>
15#include <QDebug>
16
17#include <boost/heap/fibonacci_heap.hpp>
18
19#include <boost/accumulators/accumulators.hpp>
20#include <boost/accumulators/statistics/stats.hpp>
21#include <boost/accumulators/statistics/rolling_mean.hpp>
22#include <boost/accumulators/statistics/rolling_variance.hpp>
23
24template<typename T>
26public:
27 KisRollingMax(int windowSize) : m_windowSize(windowSize) {
28 }
29
30 void push(T value) {
31 while (m_samples.size() > m_windowSize) {
32 m_values.erase(m_samples.dequeue());
33 }
34
35 m_samples.enqueue(m_values.push(value));
36 }
37
38 T max() const {
39 if (m_values.empty()) {
40 throw std::runtime_error("no values to get max of");
41 } else {
42 return m_values.top();
43 }
44 }
45
46private:
47 const int m_windowSize;
48
49 typedef boost::heap::fibonacci_heap<T> heap_type;
50
51 QQueue<typename heap_type::handle_type> m_samples;
53};
54
55template<typename T>
57public:
63 KisScalarTracker(const QString &name, int windowSize = 500) :
64 m_name(name),
65 m_windowSize(windowSize),
66 m_addCount(0),
67 m_max(windowSize),
68 m_acc(boost::accumulators::tag::rolling_window::window_size = windowSize)
69 {
70 m_printTimer.start();
71 }
72
74 }
75
80 virtual void push(T value) {
81 m_max.push(value);
82 m_acc(value);
83 m_addCount++;
84
85 if (m_addCount >= m_windowSize || m_printTimer.elapsed() >= 1000) {
86 m_printTimer.restart();
87 QString s = format(boost::accumulators::rolling_mean(m_acc),
88 boost::accumulators::rolling_variance(m_acc),
89 m_max.max());
90 print(s);
91 m_addCount = 0;
92 }
93
94 }
95
96protected:
101 virtual void print(const QString &message) {
102 qInfo() << qUtf8Printable(message);
103 }
104
111 virtual QString format(qint64 mean, qint64 variance, qint64 max) {
112 return QString("%1: mean %2 ms, var %3, max %4 ms").arg(m_name).arg(mean).arg(variance).arg(max);
113 }
114
115private:
116 const QString m_name;
117 const int m_windowSize;
119
120 QElapsedTimer m_printTimer;
121
123
124 // see https://svn.boost.org/trac10/ticket/11437
125 typedef boost::accumulators::stats<
126 boost::accumulators::tag::lazy_rolling_mean,
127 boost::accumulators::tag::rolling_variance> stats;
128
129 boost::accumulators::accumulator_set<T, stats> m_acc;
130};
131
136class KRITAGLOBAL_EXPORT KisLatencyTracker : public KisScalarTracker<qint64> {
137public:
143 KisLatencyTracker(int windowSize = 500);
144
150 virtual void push(qint64 timestamp) override;
151
152protected:
157 virtual qint64 currentTimestamp() const = 0;
158};
159
160#endif //KRITA_KIS_SCALAR_TRACKER_H
float value(const T *src, size_t ch)
virtual qint64 currentTimestamp() const =0
QQueue< typename heap_type::handle_type > m_samples
boost::heap::fibonacci_heap< T > heap_type
KisRollingMax(int windowSize)
void push(T value)
virtual void print(const QString &message)
KisRollingMax< T > m_max
KisScalarTracker(const QString &name, int windowSize=500)
boost::accumulators::accumulator_set< T, stats > m_acc
boost::accumulators::stats< boost::accumulators::tag::lazy_rolling_mean, boost::accumulators::tag::rolling_variance > stats
QElapsedTimer m_printTimer
virtual void push(T value)
virtual QString format(qint64 mean, qint64 variance, qint64 max)