QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
44 enum Roles
45 {
46 Name = Qt::UserRole + 1,
51 };
52
58 QgsRuntimeProfilerNode( const QString &group, const QString &name, const QString &id = QString() );
59
64
66
72 QgsRuntimeProfilerNode *parent() { return mParent; }
73
79 QStringList fullParentPath() const;
80
84 QVariant data( int role = Qt::DisplayRole ) const;
85
89 int childCount() const { return mChildren.size(); }
90
94 void addChild( std::unique_ptr< QgsRuntimeProfilerNode > child );
95
101 int indexOf( QgsRuntimeProfilerNode *child ) const;
102
107 QgsRuntimeProfilerNode *child( const QString &group, const QString &name, const QString &id = QString() );
108
112 QgsRuntimeProfilerNode *childAt( int index );
113
117 void clear();
118
122 void removeChildAt( int index );
123
128 void start();
129
133 void stop();
134
138 void setElapsed( double time );
139
145 double elapsed() const;
146
151 double totalElapsedTimeForChildren( const QString &group ) const;
152
153 private:
154 std::deque< std::unique_ptr< QgsRuntimeProfilerNode > > mChildren;
155 QgsRuntimeProfilerNode *mParent = nullptr;
156 QElapsedTimer mProfileTime;
157 double mElapsed = 0;
158
159 QString mId;
160 QString mName;
161 QString mGroup;
162
163};
164#endif
165
179class CORE_EXPORT QgsRuntimeProfiler : public QAbstractItemModel
180{
181 Q_OBJECT
182
183 public:
184
193
201 Q_DECL_DEPRECATED void beginGroup( const QString &name ) SIP_DEPRECATED;
202
208 Q_DECL_DEPRECATED void endGroup() SIP_DEPRECATED;
209
214 QStringList childGroups( const QString &parent = QString(), const QString &group = "startup" ) const;
215
224 void start( const QString &name, const QString &group = "startup", const QString &id = QString() );
225
229 void end( const QString &group = "startup" );
230
239 void record( const QString &name, double time, const QString &group = "startup", const QString &id = QString() );
240
245 double profileTime( const QString &name, const QString &group = "startup" ) const;
246
250 void clear( const QString &group = "startup" );
251
256 double totalTime( const QString &group = "startup" );
257
261 QSet< QString > groups() const { return mGroups; }
262
269 bool groupIsActive( const QString &group ) const;
270
274 static QString translateGroupName( const QString &group );
275
276 // Implementation of virtual functions from QAbstractItemModel
277
278 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
279 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
280 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
281 QModelIndex parent( const QModelIndex &child ) const override;
282 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
283 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
284
290 QString asText( const QString &group = QString() );
291
292#ifndef SIP_RUN
294 signals:
295
296 void started( const QString &group, const QStringList &path, const QString &name, const QString &id );
297 void ended( const QString &group, const QStringList &path, const QString &name, const QString &id, double elapsed );
299#endif
300
304 void groupAdded( const QString &group );
305
306 private slots:
307
308 void otherProfilerStarted( const QString &group, const QStringList &path, const QString &name, const QString &id );
309 void otherProfilerEnded( const QString &group, const QStringList &path, const QString &name, const QString &id, double elapsed );
310
311 private:
312
313 static QgsRuntimeProfiler *threadLocalInstance();
314 static QgsRuntimeProfiler *sMainProfiler;
315 bool mInitialized = false;
316 void setupConnections();
317
318 QgsRuntimeProfilerNode *pathToNode( const QString &group, const QString &path ) const;
319 QgsRuntimeProfilerNode *pathToNode( const QString &group, const QStringList &path ) const;
320 QModelIndex node2index( QgsRuntimeProfilerNode *node ) const;
321 QModelIndex indexOfParentNode( QgsRuntimeProfilerNode *parentNode ) const;
322 void extractModelAsText( QStringList &lines, const QString &group, const QModelIndex &parent = QModelIndex(), int level = 0 );
323
327 QgsRuntimeProfilerNode *index2node( const QModelIndex &index ) const;
328
329 QMap< QString, QStack< QgsRuntimeProfilerNode * > > mCurrentStack;
330 std::unique_ptr< QgsRuntimeProfilerNode > mRootNode;
331
332 QSet< QString > mGroups;
333
334 friend class QgsApplication;
335};
336
337
354class CORE_EXPORT QgsScopedRuntimeProfile
355{
356 public:
357
367 QgsScopedRuntimeProfile( const QString &name, const QString &group = "startup", const QString &id = QString() );
368
373
382 void switchTask( const QString &name );
383
384 private:
385
386 QString mGroup;
387
388};
389
390
391#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 cannot be copied.
QgsRuntimeProfilerNode & operator=(const QgsRuntimeProfilerNode &other)=delete
QgsRuntimeProfilerNode cannot be copied.
Roles
Custom node data roles.
@ Id
Internal node ID (since QGIS 3.34)
@ Elapsed
Node elapsed time.
@ ParentElapsed
Total elapsed time for node's parent.
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