[diffoscope] 01/03: profiling: Report on the real total time taken to execute, including cleanup.

Chris Lamb chris at chris-lamb.co.uk
Mon Jan 2 19:25:56 CET 2017


This is an automated email from the git hooks/post-receive script.

lamby pushed a commit to branch master
in repository diffoscope.

commit 9bbb4b94ff5fd8fea053c447c7066103662abcfc
Author: Chris Lamb <lamby at debian.org>
Date:   Mon Jan 2 18:13:39 2017 +0000

    profiling: Report on the real total time taken to execute, including cleanup.
---
 diffoscope/main.py             | 21 +++++++++++++++------
 diffoscope/presenters/utils.py | 10 +---------
 diffoscope/profiling.py        |  9 +++++++++
 3 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/diffoscope/main.py b/diffoscope/main.py
index b8b2882..099f0f4 100644
--- a/diffoscope/main.py
+++ b/diffoscope/main.py
@@ -33,6 +33,7 @@ from .config import Config
 from .locale import set_locale
 from .logging import logger
 from .progress import ProgressManager, Progress
+from .profiling import ProfileManager, profile
 from .tempfiles import clean_all_temp_files
 from .difference import Difference
 from .presenters.html import JQUERY_SYSTEM_LOCATIONS
@@ -227,14 +228,16 @@ def run_diffoscope(parsed_args):
     logger.debug('Starting comparison')
     ProgressManager().setup(parsed_args)
     with Progress(1, parsed_args.path1):
-        difference = compare_root_paths(
-            parsed_args.path1, parsed_args.path2)
+        with profile('main', 'outputs'):
+            difference = compare_root_paths(
+                parsed_args.path1, parsed_args.path2)
     ProgressManager().finish()
     # Generate an empty, dummy diff to write, saving for exit code first.
     has_differences = bool(difference is not None)
     if difference is None and parsed_args.output_empty:
         difference = Difference(None, parsed_args.path1, parsed_args.path2)
-    output_all(difference, parsed_args, has_differences)
+    with profile('main', 'outputs'):
+        output_all(difference, parsed_args, has_differences)
     return 1 if has_differences else 0
 
 
@@ -248,8 +251,9 @@ def main(args=None):
     signal.signal(signal.SIGTERM, sigterm_handler)
     parsed_args = None
     try:
-        parser = create_parser()
-        parsed_args = parser.parse_args(args)
+        with profile('main', 'parse_args'):
+            parser = create_parser()
+            parsed_args = parser.parse_args(args)
         sys.exit(run_diffoscope(parsed_args))
     except KeyboardInterrupt:
         logger.info('Keyboard Interrupt')
@@ -263,7 +267,12 @@ def main(args=None):
             pdb.post_mortem()
         sys.exit(2)
     finally:
-        clean_all_temp_files()
+        with profile('main', 'cleanup'):
+            clean_all_temp_files()
+
+        # Print profiling output at the very end
+        if parsed_args is not None:
+            ProfileManager().finish(parsed_args)
 
 if __name__ == '__main__':
     main()
diff --git a/diffoscope/presenters/utils.py b/diffoscope/presenters/utils.py
index bbe858c..041d29f 100644
--- a/diffoscope/presenters/utils.py
+++ b/diffoscope/presenters/utils.py
@@ -22,7 +22,7 @@ import codecs
 import contextlib
 
 from ..logging import logger
-from ..profiling import ProfileManager, profile
+from ..profiling import profile
 
 from .text import output_text
 from .html import output_html, output_html_directory
@@ -45,7 +45,6 @@ def output_all(difference, parsed_args, has_differences):
         ('text', text, parsed_args.text_output),
         ('html', html, parsed_args.html_output),
         ('html_directory', html_directory, parsed_args.html_output_directory),
-        ('profile', profiling, parsed_args.profile_output),
     ):
         if target is None:
             continue
@@ -56,9 +55,6 @@ def output_all(difference, parsed_args, has_differences):
             fn(difference, parsed_args, has_differences)
 
 def text(difference, parsed_args, has_differences):
-    if difference is None:
-        return
-
     # As a sppecial case, write an empty file instead of an empty diff.
     if not has_differences:
         open(parsed_args.text_output, 'w').close()
@@ -95,10 +91,6 @@ def html_directory(difference, parsed_args, has_differences):
         jquery_url=parsed_args.jquery_url,
     )
 
-def profiling(difference, parsed_args, has_differences):
-    with make_printer(parsed_args.profile_output or '-') as fn:
-        ProfileManager().output(fn)
-
 @contextlib.contextmanager
 def make_printer(path):
     output = sys.stdout
diff --git a/diffoscope/profiling.py b/diffoscope/profiling.py
index 31b0737..9539267 100644
--- a/diffoscope/profiling.py
+++ b/diffoscope/profiling.py
@@ -52,6 +52,15 @@ class ProfileManager(object):
         self.data[namespace][key]['time'] += time.time() - start
         self.data[namespace][key]['count'] += 1
 
+    def finish(self, parsed_args):
+        from .presenters.utils import make_printer
+
+        if parsed_args.profile_output is None:
+            return
+
+        with make_printer(parsed_args.profile_output) as fn:
+            self.output(fn)
+
     def output(self, print):
         title = "Profiling output for: {}".format(' '.join(sys.argv))
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list