Skip to content

Commit a913a6a

Browse files
committedMay 18, 2020
Create RAII QgsScopedRuntimeProfile class and Python context manager
to ease logging of runtime profiles. Now it's possible to do: with QgsRuntimeProfiler.profile('My operation'): # do something to automatically handle everything required to log the operation runtime
1 parent 1b4dd48 commit a913a6a

File tree

6 files changed

+95
-5
lines changed

6 files changed

+95
-5
lines changed
 

‎python/core/__init__.py.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ from .additions.qgsgeometry import _geometryNonZero, mapping_geometry
3636
from .additions.qgssettings import _qgssettings_enum_value, _qgssettings_set_enum_value, _qgssettings_flag_value
3737
from .additions.qgstaskwrapper import QgsTaskWrapper
3838
from .additions.readwritecontextentercategory import ReadWriteContextEnterCategory
39+
from .additions.runtimeprofiler import ScopedRuntimeProfileContextManager
3940
from .additions.validitycheck import check
4041

4142
# Injections into classes
@@ -47,6 +48,7 @@ QgsProcessingFeatureSourceDefinition.__repr__ = processing_source_repr
4748
QgsProcessingOutputLayerDefinition.__repr__ = processing_output_layer_repr
4849
QgsProject.blockDirtying = ProjectDirtyBlocker
4950
QgsReadWriteContext.enterCategory = ReadWriteContextEnterCategory
51+
QgsRuntimeProfiler.profile = ScopedRuntimeProfileContextManager
5052
QgsSettings.enumValue = _qgssettings_enum_value
5153
QgsSettings.setEnumValue = _qgssettings_set_enum_value
5254
QgsSettings.flagValue = _qgssettings_flag_value

‎python/core/auto_generated/qgsruntimeprofiler.sip.in

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,43 @@ The current total time collected in the profiler.
8181

8282
};
8383

84+
85+
class QgsScopedRuntimeProfile
86+
{
87+
%Docstring
88+
89+
Scoped object for logging of the runtime for a single operation or group of operations.
90+
91+
This class automatically takes care of registering an operation in the :py:func:`QgsApplication.profiler()`
92+
registry upon construction, and recording of the elapsed runtime upon destruction.
93+
94+
Python scripts should not use QgsScopedRuntimeProfile directly. Instead, use :py:func:`QgsRuntimeProfiler.profile()`
95+
.. code-block:: python
96+
97+
with QgsRuntimeProfiler.profile('My operation'):
98+
# do something
99+
100+
.. versionadded:: 3.14
101+
%End
102+
103+
%TypeHeaderCode
104+
#include "qgsruntimeprofiler.h"
105+
%End
106+
public:
107+
108+
QgsScopedRuntimeProfile( const QString &name );
109+
%Docstring
110+
Constructor for QgsScopedRuntimeProfile.
111+
112+
Automatically registers the operation in the QgsApplication.profiler() instance
113+
and starts recording the run time of the operation.
114+
%End
115+
116+
~QgsScopedRuntimeProfile();
117+
118+
};
119+
120+
84121
/************************************************************************
85122
* This file has been generated automatically from *
86123
* *

‎src/app/qgisapp.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14979,9 +14979,8 @@ void QgisApp::endProfile()
1497914979

1498014980
void QgisApp::functionProfile( void ( QgisApp::*fnc )(), QgisApp *instance, const QString &name )
1498114981
{
14982-
startProfile( name );
14982+
QgsScopedRuntimeProfile profile( name );
1498314983
( instance->*fnc )();
14984-
endProfile();
1498514984
}
1498614985

1498714986
void QgisApp::mapCanvas_keyPressed( QKeyEvent *e )

‎src/app/qgspluginregistry.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "qgslogger.h"
3333
#include "qgsmessagelog.h"
3434
#include "qgsmessagebar.h"
35+
#include "qgsruntimeprofiler.h"
3536

3637
#ifdef WITH_BINDINGS
3738
#include "qgspythonutils.h"

‎src/core/qgsruntimeprofiler.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,24 @@ void QgsRuntimeProfiler::clear()
9696
double QgsRuntimeProfiler::totalTime()
9797
{
9898
double total = 0;
99-
QList<QPair<QString, double> >::const_iterator it = mProfileTimes.constBegin();
100-
for ( ; it != mProfileTimes.constEnd(); ++it )
99+
for ( auto it = mProfileTimes.constBegin(); it != mProfileTimes.constEnd(); ++it )
101100
{
102-
total += ( *it ).second;
101+
total += it->second;
103102
}
104103
return total;
105104
}
105+
106+
107+
//
108+
// QgsScopedRuntimeProfile
109+
//
110+
111+
QgsScopedRuntimeProfile::QgsScopedRuntimeProfile( const QString &name )
112+
{
113+
QgsApplication::profiler()->start( name );
114+
}
115+
116+
QgsScopedRuntimeProfile::~QgsScopedRuntimeProfile()
117+
{
118+
QgsApplication::profiler()->end();
119+
}

‎src/core/qgsruntimeprofiler.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,41 @@ class CORE_EXPORT QgsRuntimeProfiler
9595
QList< QPair< QString, double > > mProfileTimes;
9696
};
9797

98+
99+
/**
100+
* \ingroup core
101+
*
102+
* Scoped object for logging of the runtime for a single operation or group of operations.
103+
*
104+
* This class automatically takes care of registering an operation in the QgsApplication::profiler()
105+
* registry upon construction, and recording of the elapsed runtime upon destruction.
106+
*
107+
* Python scripts should not use QgsScopedRuntimeProfile directly. Instead, use QgsRuntimeProfiler.profile()
108+
* \code{.py}
109+
* with QgsRuntimeProfiler.profile('My operation'):
110+
* # do something
111+
* \endcode
112+
*
113+
* \since QGIS 3.14
114+
*/
115+
class CORE_EXPORT QgsScopedRuntimeProfile
116+
{
117+
public:
118+
119+
/**
120+
* Constructor for QgsScopedRuntimeProfile.
121+
*
122+
* Automatically registers the operation in the QgsApplication::profiler() instance
123+
* and starts recording the run time of the operation.
124+
*/
125+
QgsScopedRuntimeProfile( const QString &name );
126+
127+
/**
128+
* Records the final runtime of the operation in the profiler instance.
129+
*/
130+
~QgsScopedRuntimeProfile();
131+
132+
};
133+
134+
98135
#endif // QGSRUNTIMEPROFILER_H

0 commit comments

Comments
 (0)
Please sign in to comment.