QGIS API Documentation 3.39.0-Master (3aed037ce22)
Loading...
Searching...
No Matches
qgsruntimeprofiler.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsruntimeprofiler.h
3 ---------------------
4 begin : June 2016
5 copyright : (C) 2016 by Nathan Woodrow
6 email : woodrow dot nathan at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#ifndef QGSRUNTIMEPROFILER_H
16#define QGSRUNTIMEPROFILER_H
17
18#include <QTime>
19#include <QElapsedTimer>
20#include "qgis_sip.h"
21#include <QPair>
22#include <QStack>
23#include <QList>
24#include <QAbstractItemModel>
25#include <memory>
26#include <deque>
27#include <QSet>
28#include "qgis_core.h"
29
30#ifndef SIP_RUN
31
39class CORE_EXPORT QgsRuntimeProfilerNode
40{
41 public:
42
43 // *INDENT-OFF*
44
52 {
53 Name = Qt::UserRole + 1,
54 Group,
55 Elapsed,
56 ParentElapsed,
57 Id,
58 };
59 // *INDENT-ON*
60
66 QgsRuntimeProfilerNode( const QString &group, const QString &name, const QString &id = QString() );
67
70
72
78 QgsRuntimeProfilerNode *parent() { return mParent; }
79
85 QStringList fullParentPath() const;
86
90 QVariant data( int role = Qt::DisplayRole ) const;
91
95 int childCount() const { return mChildren.size(); }
96
100 void addChild( std::unique_ptr< QgsRuntimeProfilerNode > child );
101
107 int indexOf( QgsRuntimeProfilerNode *child ) const;
108
113 QgsRuntimeProfilerNode *child( const QString &group, const QString &name, const QString &id = QString() );
114
118 QgsRuntimeProfilerNode *childAt( int index );
119
123 void clear();
124
128 void removeChildAt( int index );
129
134 void start();
135
139 void stop();
140
144 void setElapsed( double time );
145
151 double elapsed() const;
152
157 double totalElapsedTimeForChildren( const QString &group ) const;
158
159 private:
160 std::deque< std::unique_ptr< QgsRuntimeProfilerNode > > mChildren;
161 QgsRuntimeProfilerNode *mParent = nullptr;
162 QElapsedTimer mProfileTime;
163 double mElapsed = 0;
164
165 QString mId;
166 QString mName;
167 QString mGroup;
168
169};
170#endif
171
185class CORE_EXPORT QgsRuntimeProfiler : public QAbstractItemModel
186{
187 Q_OBJECT
188
189 public:
190
199
207 Q_DECL_DEPRECATED void beginGroup( const QString &name ) SIP_DEPRECATED;
208
214 Q_DECL_DEPRECATED void endGroup() SIP_DEPRECATED;
215
220 QStringList childGroups( const QString &parent = QString(), const QString &group = "startup" ) const;
221
230 void start( const QString &name, const QString &group = "startup", const QString &id = QString() );
231
235 void end( const QString &group = "startup" );
236
245 void record( const QString &name, double time, const QString &group = "startup", const QString &id = QString() );
246
251 double profileTime( const QString &name, const QString &group = "startup" ) const;
252
256 void clear( const QString &group = "startup" );
257
262 double totalTime( const QString &group = "startup" );
263
267 QSet< QString > groups() const { return mGroups; }
268
275 bool groupIsActive( const QString &group ) const;
276
280 static QString translateGroupName( const QString &group );
281
282 // Implementation of virtual functions from QAbstractItemModel
283
284 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
285 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
286 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
287 QModelIndex parent( const QModelIndex &child ) const override;
288 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
289 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
290
296 QString asText( const QString &group = QString() );
297
298#ifndef SIP_RUN
300 signals:
301
302 void started( const QString &group, const QStringList &path, const QString &name, const QString &id );
303 void ended( const QString &group, const QStringList &path, const QString &name, const QString &id, double elapsed );
305#endif
306
310 void groupAdded( const QString &group );
311
312 private slots:
313
314 void otherProfilerStarted( const QString &group, const QStringList &path, const QString &name, const QString &id );
315 void otherProfilerEnded( const QString &group, const QStringList &path, const QString &name, const QString &id, double elapsed );
316
317 private:
318
319 static QgsRuntimeProfiler *threadLocalInstance();
320 static QgsRuntimeProfiler *sMainProfiler;
321 bool mInitialized = false;
322 void setupConnections();
323
324 QgsRuntimeProfilerNode *pathToNode( const QString &group, const QString &path ) const;
325 QgsRuntimeProfilerNode *pathToNode( const QString &group, const QStringList &path ) const;
326 QModelIndex node2index( QgsRuntimeProfilerNode *node ) const;
327 QModelIndex indexOfParentNode( QgsRuntimeProfilerNode *parentNode ) const;
328 void extractModelAsText( QStringList &lines, const QString &group, const QModelIndex &parent = QModelIndex(), int level = 0 );
329
333 QgsRuntimeProfilerNode *index2node( const QModelIndex &index ) const;
334
335 QMap< QString, QStack< QgsRuntimeProfilerNode * > > mCurrentStack;
336 std::unique_ptr< QgsRuntimeProfilerNode > mRootNode;
337
338 QSet< QString > mGroups;
339
340 friend class QgsApplication;
341};
342
343
360class CORE_EXPORT QgsScopedRuntimeProfile
361{
362 public:
363
373 QgsScopedRuntimeProfile( const QString &name, const QString &group = "startup", const QString &id = QString() );
374
379
388 void switchTask( const QString &name );
389
390 private:
391
392 QString mGroup;
393
394};
395
396
397#endif // QGSRUNTIMEPROFILER_H
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
A node representing an entry in a QgsRuntimeProfiler.
QgsRuntimeProfilerNode(const QgsRuntimeProfilerNode &other)=delete
QgsRuntimeProfilerNode & operator=(const QgsRuntimeProfilerNode &other)=delete
CustomRole
Custom node data roles.
QgsRuntimeProfilerNode * parent()
Returns the node's parent node.
int childCount() const
Returns the number of child nodes owned by this node.
Provides a method of recording run time profiles of operations, allowing easy recording of their over...
void groupAdded(const QString &group)
Emitted when a new group has started being profiled.
~QgsRuntimeProfiler() override
Scoped object for logging of the runtime for a single operation or group of operations.
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:271