changeset 442:171dde4f61dc

performance: Add profiling to the asset pipeline workers.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 28 Jun 2015 14:52:58 -0700
parents dc8518c51cbe
children 6b9f59b19db7
files piecrust/processing/pipeline.py piecrust/processing/worker.py
diffstat 2 files changed, 23 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/processing/pipeline.py	Sun Jun 28 14:52:37 2015 -0700
+++ b/piecrust/processing/pipeline.py	Sun Jun 28 14:52:58 2015 -0700
@@ -255,12 +255,19 @@
         ctx.pool.queue.put_nowait(job)
 
     def _createWorkerPool(self):
+        import sys
+
+        main_module = sys.modules['__main__']
+        is_profiling = os.path.basename(main_module.__file__) in [
+                'profile.py', 'cProfile.py']
+
         pool = _WorkerPool()
         for i in range(self.num_workers):
             ctx = ProcessingWorkerContext(
                     self.app.root_dir, self.out_dir, self.tmp_dir,
                     pool.queue, pool.results, pool.abort_event,
                     self.force, self.app.debug)
+            ctx.is_profiling = is_profiling
             ctx.enabled_processors = self.enabled_processors
             ctx.additional_processors = self.additional_processors
             w = multiprocessing.Process(
--- a/piecrust/processing/worker.py	Sun Jun 28 14:52:37 2015 -0700
+++ b/piecrust/processing/worker.py	Sun Jun 28 14:52:58 2015 -0700
@@ -23,6 +23,21 @@
 
 
 def worker_func(wid, ctx):
+    if ctx.is_profiling:
+        try:
+            import cProfile as profile
+        except ImportError:
+            import profile
+
+        ctx.is_profiling = False
+        profile.runctx('_real_worker_func(wid, ctx)',
+                       globals(), locals(),
+                       filename='PipelineWorker-%d.prof' % wid)
+    else:
+        _real_worker_func(wid, ctx)
+
+
+def _real_worker_func(wid, ctx):
     logger.debug("Worker %d booting up..." % wid)
     w = ProcessingWorker(wid, ctx)
     w.run()
@@ -40,6 +55,7 @@
         self.abort_event = abort_event
         self.force = force
         self.debug = debug
+        self.is_profiling = False
         self.enabled_processors = None
         self.additional_processors = None