QGIS API Documentation 3.33.0-Master (bf22a165b3)
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,
50 };
51
55 QgsRuntimeProfilerNode( const QString &group, const QString &name );
56
61
63
69 QgsRuntimeProfilerNode *parent() { return mParent; }
70
74 QStringList fullParentPath() const;
75
79 QVariant data( int role = Qt::DisplayRole ) const;
80
84 int childCount() const { return mChildren.size(); }
85
89 void addChild( std::unique_ptr< QgsRuntimeProfilerNode > child );
90
96 int indexOf( QgsRuntimeProfilerNode *child ) const;
97
102 QgsRuntimeProfilerNode *child( const QString &group, const QString &name );
103
107 QgsRuntimeProfilerNode *childAt( int index );
108
112 void clear();
113
117 void removeChildAt( int index );
118
123 void start();
124
128 void stop();
129
133 void setElapsed( double time );
134
140 double elapsed() const;
141
146 double totalElapsedTimeForChildren( const QString &group ) const;
147
148 private:
149 std::deque< std::unique_ptr< QgsRuntimeProfilerNode > > mChildren;
150 QgsRuntimeProfilerNode *mParent = nullptr;
151 QElapsedTimer mProfileTime;
152 double mElapsed = 0;
153
154 QString mName;
155 QString mGroup;
156
157};
158#endif
159
173class CORE_EXPORT QgsRuntimeProfiler : public QAbstractItemModel
174{
175 Q_OBJECT
176
177 public:
178
187
195 Q_DECL_DEPRECATED void beginGroup( const QString &name ) SIP_DEPRECATED;
196
202 Q_DECL_DEPRECATED void endGroup() SIP_DEPRECATED;
203
208 QStringList childGroups( const QString &parent = QString(), const QString &group = "startup" ) const;
209
215 void start( const QString &name, const QString &group = "startup" );
216
220 void end( const QString &group = "startup" );
221
227 void record( const QString &name, double time, const QString &group = "startup" );
228
233 double profileTime( const QString &name, const QString &group = "startup" ) const;
234
238 void clear( const QString &group = "startup" );
239
244 double totalTime( const QString &group = "startup" );
245
249 QSet< QString > groups() const { return mGroups; }
250
257 bool groupIsActive( const QString &group ) const;
258
262 static QString translateGroupName( const QString &group );
263
264 // Implementation of virtual functions from QAbstractItemModel
265
266 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
267 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
268 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
269 QModelIndex parent( const QModelIndex &child ) const override;
270 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
271 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
272
278 QString asText( const QString &group = QString() );
279
280#ifndef SIP_RUN
282 signals:
283
284 void started( const QString &group, const QStringList &path, const QString &name );
285 void ended( const QString &group, const QStringList &path, const QString &name, double elapsed );
287#endif
288
292 void groupAdded( const QString &group );
293
294 private slots:
295
296 void otherProfilerStarted( const QString &group, const QStringList &path, const QString &name );
297 void otherProfilerEnded( const QString &group, const QStringList &path, const QString &name, double elapsed );
298
299 private:
300
301 static QgsRuntimeProfiler *threadLocalInstance();
302 static QgsRuntimeProfiler *sMainProfiler;
303 bool mInitialized = false;
304 void setupConnections();
305
306 QgsRuntimeProfilerNode *pathToNode( const QString &group, const QString &path ) const;
307 QgsRuntimeProfilerNode *pathToNode( const QString &group, const QStringList &path ) const;
308 QModelIndex node2index( QgsRuntimeProfilerNode *node ) const;
309 QModelIndex indexOfParentNode( QgsRuntimeProfilerNode *parentNode ) const;
310 void extractModelAsText( QStringList &lines, const QString &group, const QModelIndex &parent = QModelIndex(), int level = 0 );
311
315 QgsRuntimeProfilerNode *index2node( const QModelIndex &index ) const;
316
317 QMap< QString, QStack< QgsRuntimeProfilerNode * > > mCurrentStack;
318 std::unique_ptr< QgsRuntimeProfilerNode > mRootNode;
319
320 QSet< QString > mGroups;
321
322 friend class QgsApplication;
323};
324
325
342class CORE_EXPORT QgsScopedRuntimeProfile
343{
344 public:
345
352 QgsScopedRuntimeProfile( const QString &name, const QString &group = "startup" );
353
358
367 void switchTask( const QString &name );
368
369 private:
370
371 QString mGroup;
372
373};
374
375
376#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.
@ 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