Skip to content

pass flake8 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
*.log
*.pot
*.pyc
local_settings.py
# python compiled source and temporary files #
##############################################

*.py[co]
*~

# HG, SVN #
###########

.svn
.hg
.hgignore

# Sqlite base #
###############

sqlite3

# gettext compiled file #
#########################

*.pot
*.mo

# Buildout directories and files #
##################################

*.sublime-project
*.sublime-workspace
*.egg-info
docs
local

bin
lib
dist
eggs
parts
build
include
downloads
src_eggs
uploads
develop-eggs
cfc.egg-info

.installed.cfg
Makefile
bootstrap.py
buildout.cfg
mydatabase.db
versions.cfg

# Misc #
########

.tox
profiler_test
*.log
2 changes: 2 additions & 0 deletions profiler/__init__.py
Original file line number Diff line number Diff line change
@@ -2,8 +2,10 @@

_local = threading.local()


def _set_current_view(view):
_local.current_view = view


def _get_current_view():
return getattr(_local, 'current_view', None)
18 changes: 13 additions & 5 deletions profiler/instrument.py
Original file line number Diff line number Diff line change
@@ -3,13 +3,14 @@
from django.db.models.sql.compiler import SQLCompiler
from django.db.models.sql.datastructures import EmptyResultSet
from django.db.models.sql.constants import MULTI
from django.db import connection

from aggregate.client import get_client

from profiler import _get_current_view


def execute_sql(self, *args, **kwargs):

client = get_client()
if client is None:
return self.__execute_sql(*args, **kwargs)
@@ -27,13 +28,20 @@ def execute_sql(self, *args, **kwargs):
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})
client.insert(
{
'query': q,
'view': _get_current_view(),
'type': 'sql'
},
{
'time': 0.0 + d.seconds * 1000 + d.microseconds / 1000,
'count': 1
}
)

INSTRUMENTED = False



if not INSTRUMENTED:
SQLCompiler.__execute_sql = SQLCompiler.execute_sql
SQLCompiler.execute_sql = execute_sql
47 changes: 26 additions & 21 deletions profiler/middleware.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
from datetime import datetime
import inspect

import statprof

from django.db import connection
from django.core.cache import cache
from django.conf import settings


from aggregate.client import get_client

from profiler import _set_current_view


class ProfilerMiddleware(object):

def process_view(self, request, view_func, view_args, view_kwargs):
if inspect.ismethod(view_func):
view_name = view_func.im_class.__module__+ '.' + view_func.im_class.__name__ + view_func.__name__
view_name = (
view_func.im_class.__module__
+ '.' + view_func.im_class.__name__
+ view_func.__name__
)
else:
view_name = view_func.__module__ + '.' + view_func.__name__

_set_current_view(view_name)


def process_response(self, request, response):
_set_current_view(None)
return response
@@ -31,9 +32,11 @@ def process_response(self, request, response):
class StatProfMiddleware(object):

def process_request(self, request):
statprof.reset(getattr(settings, 'LIVEPROFILER_STATPROF_FREQUENCY', 100))
statprof.reset(getattr(
settings, 'LIVEPROFILER_STATPROF_FREQUENCY', 100
))
statprof.start()

def process_response(self, request, response):
statprof.stop()
client = get_client()
@@ -43,18 +46,20 @@ def process_response(self, request, 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()])


{
'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
4 changes: 2 additions & 2 deletions profiler/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from django.db import models # NOQA

# Create your models here.

from profiler.instrument import *
from profiler.instrument import * # NOQA
16 changes: 0 additions & 16 deletions profiler/tests.py

This file was deleted.

4 changes: 4 additions & 0 deletions profiler/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# coding=utf-8
from __future__ import unicode_literals

from .tests import ProfilerTests # NOQA
19 changes: 19 additions & 0 deletions profiler/tests/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# coding=utf-8
from __future__ import absolute_import, unicode_literals
"""
django-live-profiler.tests.settings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Contains the settings to be used when running the test suite.
"""
from os.path import dirname, join

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase'
}
}

LOCALE_PATHS = (join(dirname(dirname(__file__)), 'locale'),)
INSTALLED_APPS = ['profiler']
SECRET_KEY = '*'
9 changes: 9 additions & 0 deletions profiler/tests/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.test import SimpleTestCase

from ..models import SQLCompiler


class ProfilerTests(SimpleTestCase):

def test_first(self):
self.assertTrue(SQLCompiler.execute_sql)
5 changes: 2 additions & 3 deletions profiler/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.conf.urls.defaults import *
from django.conf.urls.defaults import url, patterns

urlpatterns = patterns(
'profiler.views',
url(r'^$', 'global_stats', name='profiler_global_stats'),
url(r'^by_view/$', 'stats_by_view', name='profiler_stats_by_view'),
url(r'^code/$', 'python_stats', name='profiler_python_stats'),
url(r'^reset/$', 'reset', name='profiler_reset'),
)

)
89 changes: 57 additions & 32 deletions profiler/views.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,90 @@
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.core.cache import cache
from django.contrib.auth.decorators import user_passes_test
from django.core.urlresolvers import reverse
from django.utils import simplejson

from aggregate.client import get_client

@user_passes_test(lambda u:u.is_superuser)

@user_passes_test(lambda u: u.is_superuser)
def global_stats(request):
stats = get_client().select(group_by=['query'], where={'type':'sql'})
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))
return render_to_response(
'profiler/index.html',
{'queries': stats},
context_instance=RequestContext(request)
)


@user_passes_test(lambda u:u.is_superuser)
@user_passes_test(lambda u: u.is_superuser)
def stats_by_view(request):
stats = get_client().select(group_by=['view','query'], where={'type':'sql'})
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': [],
'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']
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))
r['normtime'] = (0.0 + r['average_time']) / maxtime

@user_passes_test(lambda u:u.is_superuser)
return render_to_response(
'profiler/by_view.html',
{'queries': grouped,
'stats': simplejson.dumps(stats)},
context_instance=RequestContext(request)
)


@user_passes_test(lambda u: u.is_superuser)
def reset(request):
next = request.GET.get('next') or request.POST.get('next') or request.META.get('HTTP_REFERER') or reverse('profiler_global_stats')
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))

return render_to_response(
'profiler/reset.html',
{'next': next},
context_instance=RequestContext(request)
)


@user_passes_test(lambda u:u.is_superuser)
@user_passes_test(lambda u: u.is_superuser)
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))
stats = get_client().select(
group_by=['file', 'lineno'],
where={'type': 'python'}
)
return render_to_response(
'profiler/code.html',
{'stats': stats},
context_instance=RequestContext(request)
)
Loading