def test_combine_stats(self): """ Combining multiple profiles """ self.client.get('/a/') self.client.get('/a/') backend = get_backend() stats, output = backend.get_stats(Session.on_site.get(path='/a/',active=True)) self.assertIsInstance(stats, Stats) stats.print_stats() self.assertIn('HelloWorld', output.getvalue())
def stats_view(self, request, object_id, extra_context=None): """ Render a view for selecting details before printing stats """ opts = self.model._meta app_label = opts.app_label backend = get_backend() if not callable(getattr(backend, 'get_stats', None)): raise Exception('Current backend does not support getting ' 'aggregate stats') obj = self.get_object(request, unquote(object_id)) if obj is None: raise Http404('Stats object does not exist') if request.method == 'POST': form = StatsForm(request.POST) if form.is_valid(): data = form.cleaned_data stats, output = backend.get_stats(obj) if not stats: raise Http404('No stats collected for this object') if data['strip_dirs']: stats.strip_dirs() if data['sort']: stats.sort_stats(*data['sort']) if data['reverse_sort']: stats.reverse_order() method = getattr(stats, 'print_%s' % (data['method'],)) method(*data['restrictions']) return HttpResponse(output.getvalue(), content_type='text/plain') else: form = StatsForm() adminform = helpers.AdminForm(form, [ (None, {'fields':[ 'sort','reverse_sort','strip_dirs', 'restrictions','method'] }), ], {}) context = { 'title': 'Stats for %s' % obj.name, 'adminform': adminform, 'object_id': object_id, 'original': obj, 'is_popup': "_popup" in request.REQUEST, 'errors': helpers.AdminErrorList(form, None), 'app_label': opts.app_label, 'opts': opts, 'add': False, } context.update(extra_context or {}) return TemplateResponse(request, ['admin/dprofiling/session/stats_form.html'], context, current_app=self.admin_site.name)
def __init__(self, backend=get_backend(), *args, **kwags): self.backend = backend