QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsruntimeprofiler.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsruntimeprofiler.cpp
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 #include "qgsruntimeprofiler.h"
16 #include "qgslogger.h"
17 #include "qgis.h"
18 #include "qgsapplication.h"
19 #include <QSet>
20 
21 void QgsRuntimeProfiler::beginGroup( const QString &name )
22 {
23  start( name );
24 }
25 
27 {
28  end();
29 }
30 
31 QStringList QgsRuntimeProfiler::childGroups( const QString &parent ) const
32 {
33  QStringList res;
34  const int parentDepth = parent.split( '/' ).length();
35  for ( auto it = mProfileTimes.constBegin(); it != mProfileTimes.constEnd(); ++it )
36  {
37  if ( !parent.isEmpty() && !it->first.startsWith( parent + '/' ) )
38  continue;
39 
40  if ( it->first.isEmpty() )
41  continue;
42 
43  const QStringList groups = it->first.split( '/' );
44  if ( parent.isEmpty() )
45  {
46  if ( !res.contains( groups.at( 0 ) ) )
47  res << groups.at( 0 );
48  }
49  else
50  {
51  if ( !res.contains( groups.at( parentDepth ) ) )
52  res << groups.at( parentDepth );
53  }
54  }
55  return res;
56 }
57 
58 void QgsRuntimeProfiler::start( const QString &name )
59 {
60  mProfileTime.push( QElapsedTimer() );
61  mProfileTime.top().restart();
62  QString cleanedName = name;
63  cleanedName.replace( '/', '_' );
64  mCurrentName.push( cleanedName );
65 }
66 
68 {
69  QString name;
70  for ( const QString &group : qgis::as_const( mCurrentName ) )
71  {
72  name += name.isEmpty() || name.right( 1 ) == '/' ? group : '/' + group;
73  }
74  mCurrentName.pop();
75 
76  double timing = mProfileTime.top().elapsed() / 1000.0;
77  mProfileTime.pop();
78 
79  mProfileTimes << qMakePair( name, timing );
80  QgsDebugMsgLevel( QStringLiteral( "PROFILE: %1 - %2" ).arg( name ).arg( timing ), 2 );
81 }
82 
83 double QgsRuntimeProfiler::profileTime( const QString &name ) const
84 {
85  if ( !name.isEmpty() )
86  {
87  for ( auto it = mProfileTimes.constBegin(); it != mProfileTimes.constEnd(); ++it )
88  {
89  if ( it->first == name )
90  return it->second;
91  }
92  return -1;
93  }
94  else
95  {
96  // collect total time for top level items
97  double totalTime = 0;
98  for ( auto it = mProfileTimes.constBegin(); it != mProfileTimes.constEnd(); ++it )
99  {
100  if ( it->first.count( '/' ) == 0 )
101  totalTime += it->second;
102  }
103  return totalTime;
104  }
105 }
106 
108 {
109  mProfileTimes.clear();
110 }
111 
113 {
114  double total = 0;
115  for ( auto it = mProfileTimes.constBegin(); it != mProfileTimes.constEnd(); ++it )
116  {
117  total += it->second;
118  }
119  return total;
120 }
121 
122 
123 //
124 // QgsScopedRuntimeProfile
125 //
126 
128 {
129  QgsApplication::profiler()->start( name );
130 }
131 
133 {
135 }
qgsruntimeprofiler.h
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsRuntimeProfiler::beginGroup
Q_DECL_DEPRECATED void beginGroup(const QString &name)
Begin the group for the profiler.
Definition: qgsruntimeprofiler.cpp:21
qgis.h
QgsRuntimeProfiler::start
void start(const QString &name)
Start a profile event with the given name.
Definition: qgsruntimeprofiler.cpp:58
QgsRuntimeProfiler::clear
void clear()
clear Clear all profile data.
Definition: qgsruntimeprofiler.cpp:107
QgsScopedRuntimeProfile::~QgsScopedRuntimeProfile
~QgsScopedRuntimeProfile()
Records the final runtime of the operation in the profiler instance.
Definition: qgsruntimeprofiler.cpp:132
qgsapplication.h
QgsRuntimeProfiler::totalTime
double totalTime()
The current total time collected in the profiler.
Definition: qgsruntimeprofiler.cpp:112
QgsRuntimeProfiler::profileTime
double profileTime(const QString &name) const
Returns the profile time for the specified name.
Definition: qgsruntimeprofiler.cpp:83
QgsApplication::profiler
static QgsRuntimeProfiler * profiler()
Returns the application runtime profiler.
Definition: qgsapplication.cpp:461
QgsScopedRuntimeProfile::QgsScopedRuntimeProfile
QgsScopedRuntimeProfile(const QString &name)
Constructor for QgsScopedRuntimeProfile.
Definition: qgsruntimeprofiler.cpp:127
QgsRuntimeProfiler::endGroup
Q_DECL_DEPRECATED void endGroup()
End the current active group.
Definition: qgsruntimeprofiler.cpp:26
QgsRuntimeProfiler::end
void end()
End the current profile event.
Definition: qgsruntimeprofiler.cpp:67
QgsRuntimeProfiler::childGroups
QStringList childGroups(const QString &parent=QString()) const
Returns a list of all child groups with the specified parent.
Definition: qgsruntimeprofiler.cpp:31
qgslogger.h