QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
39 class 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 
173 class 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 
226  double profileTime( const QString &name, const QString &group = "startup" ) const;
227 
231  void clear( const QString &group = "startup" );
232 
237  double totalTime( const QString &group = "startup" );
238 
242  QSet< QString > groups() const { return mGroups; }
243 
250  bool groupIsActive( const QString &group ) const;
251 
255  static QString translateGroupName( const QString &group );
256 
257  // Implementation of virtual functions from QAbstractItemModel
258 
259  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
260  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
261  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
262  QModelIndex parent( const QModelIndex &child ) const override;
263  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
264  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
265 
266 #ifndef SIP_RUN
268  signals:
269 
270  void started( const QString &group, const QStringList &path, const QString &name );
271  void ended( const QString &group, const QStringList &path, const QString &name, double elapsed );
273 #endif
274 
278  void groupAdded( const QString &group );
279 
280  private slots:
281 
282  void otherProfilerStarted( const QString &group, const QStringList &path, const QString &name );
283  void otherProfilerEnded( const QString &group, const QStringList &path, const QString &name, double elapsed );
284 
285  private:
286 
287  static QgsRuntimeProfiler *threadLocalInstance();
288  static QgsRuntimeProfiler *sMainProfiler;
289  bool mInitialized = false;
290  void setupConnections();
291 
292  QgsRuntimeProfilerNode *pathToNode( const QString &group, const QString &path ) const;
293  QgsRuntimeProfilerNode *pathToNode( const QString &group, const QStringList &path ) const;
294  QModelIndex node2index( QgsRuntimeProfilerNode *node ) const;
295  QModelIndex indexOfParentNode( QgsRuntimeProfilerNode *parentNode ) const;
296 
300  QgsRuntimeProfilerNode *index2node( const QModelIndex &index ) const;
301 
302  QMap< QString, QStack< QgsRuntimeProfilerNode * > > mCurrentStack;
303  std::unique_ptr< QgsRuntimeProfilerNode > mRootNode;
304 
305  QSet< QString > mGroups;
306 
307  friend class QgsApplication;
308 };
309 
310 
327 class CORE_EXPORT QgsScopedRuntimeProfile
328 {
329  public:
330 
337  QgsScopedRuntimeProfile( const QString &name, const QString &group = "startup" );
338 
343 
352  void switchTask( const QString &name );
353 
354  private:
355 
356  QString mGroup;
357 
358 };
359 
360 
361 #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 * parent()
Returns the node's parent node.
QgsRuntimeProfilerNode & operator=(const QgsRuntimeProfilerNode &other)=delete
QgsRuntimeProfilerNode cannot be copied.
QgsRuntimeProfilerNode(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.
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