QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 <deque>
19#include <memory>
20
21#include "qgis_core.h"
22#include "qgis_sip.h"
23
24#include <QAbstractItemModel>
25#include <QElapsedTimer>
26#include <QList>
27#include <QPair>
28#include <QSet>
29#include <QStack>
30#include <QTime>
31
32#ifndef SIP_RUN
33
41class CORE_EXPORT QgsRuntimeProfilerNode
42{
43 public:
44
45 // *INDENT-OFF*
46
54 {
55 Name = Qt::UserRole + 1,
56 Group,
57 Elapsed,
58 ParentElapsed,
59 Id,
60 };
61 // *INDENT-ON*
62
68 QgsRuntimeProfilerNode( const QString &group, const QString &name, const QString &id = QString() );
69
72
74
80 QgsRuntimeProfilerNode *parent() { return mParent; }
81
87 QStringList fullParentPath() const;
88
92 QVariant data( int role = Qt::DisplayRole ) const;
93
97 int childCount() const { return mChildren.size(); }
98
102 void addChild( std::unique_ptr< QgsRuntimeProfilerNode > child );
103
109 int indexOf( QgsRuntimeProfilerNode *child ) const;
110
115 QgsRuntimeProfilerNode *child( const QString &group, const QString &name, const QString &id = QString() );
116
120 QgsRuntimeProfilerNode *childAt( int index );
121
125 void clear();
126
130 void removeChildAt( int index );
131
136 void start();
137
141 void stop();
142
146 void setElapsed( double time );
147
153 double elapsed() const;
154
159 double totalElapsedTimeForChildren( const QString &group ) const;
160
161 private:
162 std::deque< std::unique_ptr< QgsRuntimeProfilerNode > > mChildren;
163 QgsRuntimeProfilerNode *mParent = nullptr;
164 QElapsedTimer mProfileTime;
165 double mElapsed = 0;
166
167 QString mId;
168 QString mName;
169 QString mGroup;
170
171};
172#endif
173
187class CORE_EXPORT QgsRuntimeProfiler : public QAbstractItemModel
188{
189 Q_OBJECT
190
191 public:
192
201
209 Q_DECL_DEPRECATED void beginGroup( const QString &name ) SIP_DEPRECATED;
210
216 Q_DECL_DEPRECATED void endGroup() SIP_DEPRECATED;
217
222 QStringList childGroups( const QString &parent = QString(), const QString &group = "startup" ) const;
223
232 void start( const QString &name, const QString &group = "startup", const QString &id = QString() );
233
237 void end( const QString &group = "startup" );
238
247 void record( const QString &name, double time, const QString &group = "startup", const QString &id = QString() );
248
253 double profileTime( const QString &name, const QString &group = "startup" ) const;
254
258 void clear( const QString &group = "startup" );
259
264 double totalTime( const QString &group = "startup" );
265
269 QSet< QString > groups() const { return mGroups; }
270
277 bool groupIsActive( const QString &group ) const;
278
282 static QString translateGroupName( const QString &group );
283
284 // Implementation of virtual functions from QAbstractItemModel
285
286 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
287 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
288 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
289 QModelIndex parent( const QModelIndex &child ) const override;
290 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
291 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
292
298 QString asText( const QString &group = QString() );
299
300#ifndef SIP_RUN
302 signals:
303
304 void started( const QString &group, const QStringList &path, const QString &name, const QString &id );
305 void ended( const QString &group, const QStringList &path, const QString &name, const QString &id, double elapsed );
307#endif
308
312 void groupAdded( const QString &group );
313
314 private slots:
315
316 void otherProfilerStarted( const QString &group, const QStringList &path, const QString &name, const QString &id );
317 void otherProfilerEnded( const QString &group, const QStringList &path, const QString &name, const QString &id, double elapsed );
318
319 private:
320
321 static QgsRuntimeProfiler *threadLocalInstance();
322 static QgsRuntimeProfiler *sMainProfiler;
323 bool mInitialized = false;
324 void setupConnections();
325
326 QgsRuntimeProfilerNode *pathToNode( const QString &group, const QString &path ) const;
327 QgsRuntimeProfilerNode *pathToNode( const QString &group, const QStringList &path ) const;
328 QModelIndex node2index( QgsRuntimeProfilerNode *node ) const;
329 QModelIndex indexOfParentNode( QgsRuntimeProfilerNode *parentNode ) const;
330 void extractModelAsText( QStringList &lines, const QString &group, const QModelIndex &parent = QModelIndex(), int level = 0 );
331
335 QgsRuntimeProfilerNode *index2node( const QModelIndex &index ) const;
336
337 QMap< QString, QStack< QgsRuntimeProfilerNode * > > mCurrentStack;
338 std::unique_ptr< QgsRuntimeProfilerNode > mRootNode;
339
340 QSet< QString > mGroups;
341
342 friend class QgsApplication;
343};
344
345
362class CORE_EXPORT QgsScopedRuntimeProfile
363{
364 public:
365
375 QgsScopedRuntimeProfile( const QString &name, const QString &group = "startup", const QString &id = QString() );
376
381
390 void switchTask( const QString &name ); // cppcheck-suppress functionConst
391
392 private:
393
394 QString mGroup;
395
396};
397
398
399#endif // QGSRUNTIMEPROFILER_H
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(const QString &group, const QString &name, const QString &id=QString())
Constructor for QgsRuntimeProfilerNode, with the specified group and name.
QgsRuntimeProfilerNode * parent()
Returns the node's parent node.
int childCount() const
Returns the number of child nodes owned by this node.
void groupAdded(const QString &group)
Emitted when a new group has started being profiled.
QModelIndex parent(const QModelIndex &child) const override
double profileTime(const QString &name, const QString &group="startup") const
Returns the profile time for the specified name.
QgsRuntimeProfiler()
Constructor to create a new runtime profiler.
void start(const QString &name, const QString &group="startup", const QString &id=QString())
Start a profile event with the given name.
QStringList childGroups(const QString &parent=QString(), const QString &group="startup") const
Returns a list of all child groups with the specified parent.
QSet< QString > groups() const
Returns the set of known groups.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void end(const QString &group="startup")
End the current profile event.
double totalTime(const QString &group="startup")
The current total time collected in the profiler.
void clear(const QString &group="startup")
clear Clear all profile data.
Q_DECL_DEPRECATED void beginGroup(const QString &name)
Begin the group for the profiler.
~QgsRuntimeProfiler() override
Q_DECL_DEPRECATED void endGroup()
End the current active group.
void record(const QString &name, double time, const QString &group="startup", const QString &id=QString())
Manually adds a profile event with the given name and total time (in seconds).
void switchTask(const QString &name)
Switches the current task managed by the scoped profile to a new task with the given name.
QgsScopedRuntimeProfile(const QString &name, const QString &group="startup", const QString &id=QString())
Constructor for QgsScopedRuntimeProfile.
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:268