def reset(request):
    next = request.GET.get('next') or request.POST.get('next') or request.META.get('HTTP_REFERER') or reverse('profiler_global_stats')
    if request.method == 'POST':
        get_client().clear()
        return HttpResponseRedirect(next)
    return render_to_response('profiler/reset.html',
                              {'next' : next},
                              context_instance=RequestContext(request))
Esempio n. 2
0
def reset(request):
    next = request.GET.get('next') or request.POST.get(
        'next') or request.META.get('HTTP_REFERER') or reverse(
            'profiler_global_stats')
    if request.method == 'POST':
        get_client().clear()
        return HttpResponseRedirect(next)
    return render(request, 'profiler/reset.html', {'next': next})
Esempio n. 3
0
def reset(request):
    next = (
        request.GET.get("next")
        or request.POST.get("next")
        or request.META.get("HTTP_REFERER")
        or reverse("profiler_global_stats")
    )
    if request.method == "POST":
        get_client().clear()
        return HttpResponseRedirect(next)
    return render_to_response("profiler/reset.html", {"next": next}, context_instance=RequestContext(request))
Esempio n. 4
0
def reset(request):
    next = request.GET.get('next') or request.POST.get(
        'next') or request.META.get('HTTP_REFERER') or reverse(
            'profiler_global_stats')
    if request.method == 'POST':
        try:
            get_client().clear()
        except:
            pass
        return HttpResponseRedirect(next)
    return render_to_response('profiler/reset.html', {'next': next},
                              context_instance=RequestContext(request))
Esempio n. 5
0
def stats_by_view(request):
    stats = get_client().select(group_by=['view','query'], where={'type':'sql'})
    grouped = {}
    for r in stats:
        if r['view'] not in grouped:
            grouped[r['view']] = {'queries' : [], 
                                  'count' : 0,
                                  'time' : 0,
                                  'average_time' : 0}
        grouped[r['view']]['queries'].append(r)
        grouped[r['view']]['count'] += r['count']
        grouped[r['view']]['time'] += r['time']
        r['average_time'] = r['time'] / r['count'] 
        grouped[r['view']]['average_time'] += r['average_time']
        
    maxtime = 0
    for r in stats:
        if r['average_time'] > maxtime:
            maxtime = r['average_time']
    for r in stats:
        r['normtime'] = (0.0+r['average_time'])/maxtime
           
    return render_to_response('profiler/by_view.html',
                              {'queries' : grouped,
                               'stats' :simplejson.dumps(stats),
                                'STATIC_URL' : settings.STATIC_URL,
                               },
                              context_instance=RequestContext(request))
Esempio n. 6
0
def python_stats(request):
    stats = get_client().select(group_by=['file','lineno'], where={'type':'python'})
    return render_to_response('profiler/code.html',
                              {'stats' : stats,
                                'STATIC_URL' : settings.STATIC_URL,
                               },
                              context_instance=RequestContext(request))
Esempio n. 7
0
 def process_response(self, request, response):
     if True:
         _tac('mid2')
         _tac('req')
         client = get_client()
         if client is not None:
             try:
                 client.insert({'code_part': 'Middlewares Down', 'type': 'code'},
                               {'time': _get_time('mid1'), 'count': 1})
             except StandardError:
                 pass
             try:
                 client.insert({'code_part': 'Middlewares Up', 'type': 'code'},
                               {'time': _get_time('mid2'), 'count': 1})
             except StandardError:
                 pass
             try:
                 client.insert({'request_path': request.path, 'type': 'url'},
                               {'time': _get_time('req'), 'count': 1})
             except StandardError:
                 pass
             try:
                 client.insert({'code_part': request.profiler_view, 'type': 'code'},
                               {'time': _get_time('view'), 'count': 1})
             except StandardError:
                 pass
     return response
Esempio n. 8
0
    def middleware(request):
        if request.path.startswith('/profiler'):
            return get_response(request)

        # print(f'[i] Starting sampling on {request.path}..')
        statprof.reset(
            getattr(settings, 'LIVEPROFILER_STATPROF_FREQUENCY', 100))
        statprof.start()

        response = get_response(request)

        statprof.stop()
        total_samples = statprof.state.sample_count
        if total_samples == 0:
            return response
        secs_per_sample = statprof.state.accumulated_time / total_samples

        # print('[i] Getting ZQM client...')
        client = get_client()
        client.insert_all([({
            'file': c.key.filename,
            'lineno': c.key.lineno,
            'function': c.key.name,
            'type': 'python',
        }, {
            'self_nsamples': c.self_sample_count,
            'cum_nsamples': c.cum_sample_count,
            'tot_nsamples': total_samples,
            'cum_time': c.cum_sample_count * secs_per_sample,
            'self_time': c.self_sample_count * secs_per_sample,
        }) for c in statprof.CallData.all_calls.values()])
        # print(f'[i] Saved {statprof.state.sample_count} samples for {request.path}.')

        return response
Esempio n. 9
0
def execute_sql(self, *args, **kwargs):
    client = get_client()
    if client is None:
        return self.__execute_sql(*args, **kwargs)
    try:
        q, params = self.as_sql()
        if not q:
            raise EmptyResultSet
    except EmptyResultSet:
        if kwargs.get('result_type', MULTI) == MULTI:
            return iter([])
        else:
            return
    start = datetime.now()
    try:
        return self.__execute_sql(*args, **kwargs)
    finally:
        d = (datetime.now() - start)
        client.insert({
            'query': q,
            'view': _get_current_view(),
            'type': 'sql'
        }, {
            'time': 0.0 + d.seconds * 1000 + d.microseconds / 1000,
            'count': 1
        })
Esempio n. 10
0
def global_stats(request):
    stats = get_client().select(group_by=['query'], where={'type':'sql'})
    for s in stats:
        s['average_time'] = s['time'] / s['count']
    return render_to_response('profiler/index.html',
                              {'queries' : stats},
                              context_instance=RequestContext(request))
Esempio n. 11
0
def urls_stats(request):
    stats = get_client().select(group_by=['request_path'],
                                where={'type': 'url'})
    for s in stats:
        s['average_time'] = s['time'] / s['count']
    return render_to_response('profiler/urls.html', {'urls': stats},
                              context_instance=RequestContext(request))
Esempio n. 12
0
def execute_sql(self, *args, **kwargs):
    client = get_client()
    if client is None:
        return self.__execute_sql(*args, **kwargs)
    try:
        q, params = self.as_sql()
        if not q:
            raise EmptyResultSet
    except EmptyResultSet:
        if kwargs.get('result_type', MULTI) == MULTI:
            return iter([])
        else:
            return
    start = datetime.now()
    try:
        return self.__execute_sql(*args, **kwargs)
    finally:
        d = (datetime.now() - start)
        # TODO: make this more generalized. good enough for what we need now.
        # tries to find where the sql call was made from
        our_stack = [
            '{module}.{func} #{num}'.format(module=format_path(f[1]), num=f[2], func=f[3])
            for f in inspect.stack() if 'eventsquare' in f[1]
        ]

        if our_stack:
            q = ' -> '.join(our_stack) + ' | ' + q

        client.insert({'query': q, 'view': _get_current_view(), 'type': 'sql'},
                      {'time': 0.0 + d.seconds * 1000 + d.microseconds/1000, 'count': 1})
Esempio n. 13
0
    def process_response(self, request, response):
        statprof.stop()
        client = get_client()
        total_samples = statprof.state.sample_count
        if total_samples == 0:
            return response

        secs_per_sample = statprof.state.accumulated_time / total_samples

        def get_struct(c):
            return ({
                'file': c.key.filename,
                'lineno': c.key.lineno,
                'function': c.key.name,
                'type': 'python'
            }, {
                'self_nsamples': c.self_sample_count,
                'cum_nsamples': c.cum_sample_count,
                'tot_nsamples': total_samples,
                'cum_time': c.cum_sample_count * secs_per_sample,
                'self_time': c.self_sample_count * secs_per_sample
            })

        client.insert_all([get_struct(c)
                           for c in statprof.CallData.all_calls.itervalues()])

        return response
    def process_response(self, request, response):
        statprof.stop()
        client = get_client()
        total_samples = statprof.state.sample_count

        if total_samples == 0:
            return response

        secs_per_sample = statprof.state.accumulated_time / total_samples

        client.insert_all(
            [
                (
                    {"file": c.key.filename, "lineno": c.key.lineno, "function": c.key.name, "type": "python"},
                    {
                        "self_nsamples": c.self_sample_count,
                        "cum_nsamples": c.cum_sample_count,
                        "tot_nsamples": total_samples,
                        "cum_time": c.cum_sample_count * secs_per_sample,
                        "self_time": c.self_sample_count * secs_per_sample,
                    },
                )
                for c in statprof.CallData.all_calls.itervalues()
            ]
        )

        return response
Esempio n. 15
0
def stats_by_view(request):
    stats = get_client().select(group_by=['view', 'query'],
                                where={'type': 'sql'})
    grouped = {}
    for r in stats:
        if r['view'] not in grouped:
            grouped[r['view']] = {
                'queries': [],
                'count': 0,
                'time': 0,
                'average_time': 0
            }
        grouped[r['view']]['queries'].append(r)
        grouped[r['view']]['count'] += r['count']
        grouped[r['view']]['time'] += r['time']
        r['average_time'] = r['time'] / r['count']
        grouped[r['view']]['average_time'] += r['average_time']

    maxtime = 0
    for r in stats:
        if r['average_time'] > maxtime:
            maxtime = r['average_time']
    for r in stats:
        r['normtime'] = (0.0 + r['average_time']) / maxtime

    return render_to_response('profiler/by_view.html', {
        'queries': grouped,
        'stats': simplejson.dumps(stats)
    },
                              context_instance=RequestContext(request))
Esempio n. 16
0
def python_stats(request):
    try:
        stats = get_client().select(group_by=['file','lineno'], where={'type':'python'})
    except:
        stats = {}
    return render_to_response('profiler/code.html',
                              {'stats' : stats},
                              context_instance=RequestContext(request))
Esempio n. 17
0
def python_stats(request):
    try:
        stats = get_client().select(group_by=['file', 'lineno'],
                                    where={'type': 'python'})
    except:
        stats = {}
    return render_to_response('profiler/code.html', {'stats': stats},
                              context_instance=RequestContext(request))
Esempio n. 18
0
def global_stats(request):
    try:
        stats = get_client().select(group_by=['query'], where={'type': 'sql'})
    except:
        stats = {}
    for s in stats:
        s['average_time'] = s['time'] / s['count']
    return render_to_response('profiler/index.html', {'queries': stats},
                              context_instance=RequestContext(request))
Esempio n. 19
0
def instrumented_execute(self, *args, **kwargs):
    start = datetime.now()
    try:
        return self._real_execute(*args, **kwargs)
    finally:
        client = get_client()
        d = (datetime.now() - start)
        client.insert({'query' : args[0], 'view' : _get_current_view(),
                       'type' : 'sql'},
                      {'time' : 0.0 + d.seconds * 1000 + \
                       float(d.microseconds)/1000, 'count' : 1})
Esempio n. 20
0
def instrumented_execute(self, *args, **kwargs):
    start = datetime.now()
    try:
        return self._real_execute(*args, **kwargs)
    finally:
        client = get_client()
        d = (datetime.now() - start)
        client.insert({'query' : args[0], 'view' : _get_current_view(),
                       'type' : 'sql'},
                      {'time' : 0.0 + d.seconds * 1000 + \
                       float(d.microseconds)/1000, 'count' : 1})
Esempio n. 21
0
 def process_response(self, request, response):
     if True:
         _tac('mid2')
         _tac('req')
         client = get_client()
         if client is not None:
             try:
                 client.insert(
                     {
                         'code_part': 'Middlewares Down',
                         'type': 'code'
                     }, {
                         'time': _get_time('mid1'),
                         'count': 1
                     })
             except StandardError:
                 pass
             try:
                 client.insert(
                     {
                         'code_part': 'Middlewares Up',
                         'type': 'code'
                     }, {
                         'time': _get_time('mid2'),
                         'count': 1
                     })
             except StandardError:
                 pass
             try:
                 client.insert({
                     'request_path': request.path,
                     'type': 'url'
                 }, {
                     'time': _get_time('req'),
                     'count': 1
                 })
             except StandardError:
                 pass
             try:
                 client.insert(
                     {
                         'code_part': request.profiler_view,
                         'type': 'code'
                     }, {
                         'time': _get_time('view'),
                         'count': 1
                     })
             except StandardError:
                 pass
     return response
Esempio n. 22
0
 def wrapper(self, *args, **kwargs):
     client = get_client()
     if client is None:
         return orig(self, *args, **kwargs)
     try:
         start = datetime.now()
         result = orig(self, *args, **kwargs)
         return result
     finally:
         d = datetime.now() - start
         # None return value is key to not log
         log_q = query(self, delay=d, *args, **kwargs) if callable(query) else query
         if log_q is not None:
             log_key = dict(query=log_q,
                         view=_get_current_view(),
                         type='mongo')
             log_val = dict(time=(0.0 + d.seconds * 1000 + d.microseconds/1000), count=1)
             client.insert(log_key, log_val)
def execute_sql(self, *args, **kwargs):
    client = get_client()
    if client is None:
        return self.__execute_sql(*args, **kwargs)
    try:
        q, params = self.as_sql()
        if not q:
            raise EmptyResultSet
    except EmptyResultSet:
        if kwargs.get('result_type', MULTI) == MULTI:
            return iter([])
        else:
            return
    start = datetime.now()
    try:
        return self.__execute_sql(*args, **kwargs)
    finally:
        d = (datetime.now() - start)
        client.insert({'query' : q, 'view' : _get_current_view(), 'type' : 'sql'},
                      {'time' : 0.0 + d.seconds * 1000 + d.microseconds/1000, 'count' : 1})
 def wrapper(self, *args, **kwargs):
     client = get_client()
     if client is None:
         return orig(self, *args, **kwargs)
     try:
         start = datetime.now()
         result = orig(self, *args, **kwargs)
         return result
     finally:
         d = datetime.now() - start
         # None return value is key to not log
         log_q = query(self, delay=d, *args, **
                       kwargs) if callable(query) else query
         if log_q is not None:
             log_key = dict(query=log_q,
                            view=_get_current_view(),
                            type='mongo')
             log_val = dict(time=(0.0 + d.seconds * 1000 +
                                  d.microseconds / 1000),
                            count=1)
             client.insert(log_key, log_val)
def execute_sql(self, *args, **kwargs):
    client = get_client()
    if client is None:
        return self.__execute_sql(*args, **kwargs)
    try:
        q, params = self.as_sql()
        if not q:
            raise EmptyResultSet
    except EmptyResultSet:
        if kwargs.get("result_type", MULTI) == MULTI:
            return iter([])
        else:
            return
    start = datetime.now()
    try:
        return self.__execute_sql(*args, **kwargs)
    finally:
        d = datetime.now() - start
        client.insert(
            {"query": q, "view": _get_current_view(), "type": "sql"},
            {"time": 0.0 + d.seconds * 1000 + d.microseconds / 1000, "count": 1},
        )
Esempio n. 26
0
    def process_response(self, request, response):
        statprof.stop()
        client = get_client()
        total_samples = statprof.state.sample_count
        if total_samples == 0:
            return response
        secs_per_sample = statprof.state.accumulated_time / total_samples

        client.insert_all([({
            'file': c.key.filename,
            'lineno': c.key.lineno,
            'function': c.key.name,
            'type': 'python'
        }, {
            'self_nsamples': c.self_sample_count,
            'cum_nsamples': c.cum_sample_count,
            'tot_nsamples': total_samples,
            'cum_time': c.cum_sample_count * secs_per_sample,
            'self_time': c.self_sample_count * secs_per_sample
        }) for c in statprof.CallData.all_calls.itervalues()])

        return response
Esempio n. 27
0
def stats_by_view(request):
    stats = get_client().select(group_by=["view", "query"], where={"type": "sql"})
    grouped = {}
    for r in stats:
        if r["view"] not in grouped:
            grouped[r["view"]] = {"queries": [], "count": 0, "time": 0, "average_time": 0}
        grouped[r["view"]]["queries"].append(r)
        grouped[r["view"]]["count"] += r["count"]
        grouped[r["view"]]["time"] += r["time"]
        r["average_time"] = r["time"] / r["count"]
        grouped[r["view"]]["average_time"] += r["average_time"]

    maxtime = 0
    for r in stats:
        if r["average_time"] > maxtime:
            maxtime = r["average_time"]
    for r in stats:
        r["normtime"] = (0.0 + r["average_time"]) / maxtime

    return render_to_response(
        "profiler/by_view.html",
        {"queries": grouped, "stats": simplejson.dumps(stats)},
        context_instance=RequestContext(request),
    )
Esempio n. 28
0
def global_stats(request):
    stats = get_client().select(group_by=["query"], where={"type": "sql"})
    for s in stats:
        s["average_time"] = s["time"] / s["count"]
    return render_to_response("profiler/index.html", {"queries": stats}, context_instance=RequestContext(request))
Esempio n. 29
0
def python_stats(request):
    stats = get_client().select(group_by=['file', 'lineno'],
                                where={'type': 'python'})
    return render(request, 'profiler/code.html', {'stats': stats})
Esempio n. 30
0
def urls_stats(request):
    stats = get_client().select(group_by=["request_path"], where={"type": "url"})
    for s in stats:
        s["average_time"] = s["time"] / s["count"]
    return render_to_response("profiler/urls.html", {"urls": stats}, context_instance=RequestContext(request))
Esempio n. 31
0
def global_stats_mongo(request):
    stats = get_client().select(group_by=['query'], where={'type': 'mongo'})
    for s in stats:
        s['average_time'] = s['time'] / s['count']
    return render(request, 'profiler/index.html', {'queries': stats})
Esempio n. 32
0
def views_stats(request):
    stats = get_client().select(group_by=["code_part"], where={"type": "code"})
    for s in stats:
        s["average_time"] = s["time"] / s["count"]
    return render_to_response("profiler/views.html", {"parts": stats}, context_instance=RequestContext(request))
Esempio n. 33
0
def python_stats(request):
    stats = get_client().select(group_by=["file", "lineno"], where={"type": "python"})
    return render_to_response("profiler/code.html", {"stats": stats}, context_instance=RequestContext(request))
Esempio n. 34
0
def stats_by_view(request):
    stats = get_client().select(group_by=['view', 'query'],
                                where={'type': 'sql'})
    return _render_stats(stats)
Esempio n. 35
0
def mongo_stats_by_view(request):
    stats = get_client().select(group_by=['view', 'query'],
                                where={'type': 'mongo'})
    return _display_by_view(request, stats)
Esempio n. 36
0
def views_stats(request):
    stats = get_client().select(group_by=['code_part'], where={'type': 'code'})
    for s in stats:
        s['average_time'] = s['time'] / s['count']
    return render_to_response('profiler/views.html', {'parts': stats},
                              context_instance=RequestContext(request))