Philipp Hörist pushed to branch master at gajim / gajim


Commits:
47c04155 by lovetox at 2022-06-11T11:59:53+02:00
feat: Add new profiling command line switch

Usage: gajim --cprofile > stats.txt

- - - - -


2 changed files:

- gajim/common/application.py
- gajim/gtk/application.py


Changes:

=====================================
gajim/common/application.py
=====================================
@@ -22,6 +22,9 @@
 import sys
 import json
 import logging
+import cProfile
+import pstats
+from pstats import SortKey
 from datetime import datetime
 from packaging.version import Version as V
 
@@ -46,6 +49,9 @@
 
 
 class CoreApplication:
+    def __init__(self) -> None:
+        self._profiling_session = None
+
     def _init_core(self) -> None:
         # Create and initialize Application Paths & Databases
         app.app = self
@@ -88,6 +94,21 @@ def _init_core(self) -> None:
     def _log(self) -> logging.Logger:
         return app.log('gajim.application')
 
+    def start_profiling(self) -> None:
+        self._log.info('Start profiling')
+        self._profiling_session = cProfile.Profile()
+        self._profiling_session.enable()
+
+    def end_profiling(self) -> None:
+        if self._profiling_session is None:
+            return
+
+        self._profiling_session.disable()
+        self._log.info('End profiling')
+        ps = pstats.Stats(self._profiling_session)
+        ps = ps.sort_stats(SortKey.CUMULATIVE)
+        ps.print_stats()
+
     def start_shutdown(self, *args: Any, **kwargs: Any) -> None:
         app.app.systray.shutdown()
 
@@ -119,6 +140,7 @@ def _shutdown_core(self) -> None:
         app.storage.archive.cleanup_chat_history()
         app.storage.cache.shutdown()
         app.storage.archive.shutdown()
+        self.end_profiling()
 
     def _quit_app(self) -> None:
         self._shutdown_core()


=====================================
gajim/gtk/application.py
=====================================
@@ -96,6 +96,7 @@ class GajimApplication(Gtk.Application, CoreApplication):
     '''Main class handling activation and command line.'''
 
     def __init__(self):
+        CoreApplication.__init__(self)
         flags = (Gio.ApplicationFlags.HANDLES_COMMAND_LINE |
                  Gio.ApplicationFlags.CAN_OVERRIDE_APP_ID)
         Gtk.Application.__init__(self,
@@ -180,6 +181,13 @@ def __init__(self):
             _('Sets an environment variable so '
               'GLib debug messages are printed'))
 
+        self.add_main_option(
+            'cprofile',
+            0,
+            GLib.OptionFlags.NONE,
+            GLib.OptionArg.NONE,
+            _('Profile application with cprofile'))
+
         self.add_main_option(
             'start-chat', 0,
             GLib.OptionFlags.NONE,
@@ -356,6 +364,9 @@ def _handle_local_options(self,
             print(gajim.__version__)
             return 0
 
+        if options.contains('cprofile'):
+            self.start_profiling()
+
         profile = options.lookup_value('profile')
         if profile is not None:
             # Incorporate profile name into application id



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/47c04155978951df5972fccd0952d7a6d21dabc8

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/47c04155978951df5972fccd0952d7a6d21dabc8
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list
Commits@gajim.org
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to