Using line_profiler to profile Pyramid views

440 views Asked by At

I have a trouble profiling a Pyramid view with line_profiler. Since Pyramid's @view_config can't be used together with @profile in typical fashion, I modified the view decorator I use for other purpose:

def view_decorator_profile(view_callable):
    def inner(context, request):
        request._mso = MySpecialObject()
        log.debug('################## VIEW_CALLABLE %s', view_callable)
        profile.add_function(view_callable)
        return view_callable(context, request)
    return inner

I use it in standard Pyramid fashion:

@view_config(decorator=view_decorator_profile, route_name='myroute', renderer='....', permission='...')
def my_callable(...):

Now, profiling runs.. except it seems that Pyramid does some black magic and instead of my_callable being profiled, rendered_view is profiled!

See:

Function: rendered_view at line 353

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   353                                                   def rendered_view(context, request):
   354                                                       renderer = view_renderer
   355                                                       result = view(context, request)
   356                                                       if result.__class__ is Response: # potential common case
   357                                                           response = result
   358                                                       else:
   359                                                           registry = self.registry
   360                                                           # this must adapt, it can't do a simple interface check
   361                                                           # (avoid trying to render webob responses)
   362                                                           response = registry.queryAdapterOrSelf(result, IResponse)
   363                                                           if response is None:
   364                                                               attrs = getattr(request, '__dict__', {})
   365                                                               if 'override_renderer' in attrs:
   366                                                                   # renderer overridden by newrequest event or other
   367                                                                   renderer_name = attrs.pop('override_renderer')
   368                                                                   renderer = renderers.RendererHelper(
   369                                                                       name=renderer_name,
   370                                                                       package=self.kw.get('package'),
   371                                                                       registry = registry)
   372                                                               if '__view__' in attrs:
   373                                                                   view_inst = attrs.pop('__view__')
   374                                                               else:
   375                                                                   view_inst = getattr(view, '__original_view__', view)
   376                                                               response = renderer.render_view(request, result, view_inst,
   377                                                                                               context)
   378                                                       return response

2016-02-23 17:43:34,259 INFO    base.py:159  shutdown             | Scheduler has been shut down
2016-02-23 17:43:34,288 INFO __init__.py:195  atexit_handler       |

Now, I'm positive that the view_callable I get in the decorator is correct: log.debug logs correct name (my_callable).

How can I make Pyramid/line_profiler profile my_callable?

0

There are 0 answers