Skip to content
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

Actually create a --noprogress command line option and bind it to existing argument #7

Open
wants to merge 7 commits into
base: 1d8db11
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
keywords = "django",
description = "Performance logging middlware and analysis tools for Django",
install_requires=[
'texttable',
'progressbar',
],
classifiers = [
Expand Down
52 changes: 45 additions & 7 deletions src/timelog/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,44 @@
from re import compile
from django.conf import settings

from texttable import Texttable
from progressbar import ProgressBar, Percentage, Bar

from django.core.urlresolvers import resolve, Resolver404


PATTERN = r"""^([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9:]{8},[0-9]{3}) (GET|POST|PUT|DELETE|HEAD) "(.*)" \((.*)\) (.*)"""
PATTERN = r"""^([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9:]{8},[0-9]{3}) (GET|POST|PUT|DELETE|HEAD) "(.*)" \((.*)\) (.*?) \((\d+)q, (.*?)\)"""

CACHED_VIEWS = {}

IGNORE_PATHS = getattr(settings, 'TIMELOG_IGNORE_URIS', ())

def getTerminalWidth():

import os

def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
except:
return None
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (env['LINES'], env['COLUMNS'])
except:
cr = (25, 80)
return int(cr[1])


def count_lines_in(filename):
"Count lines in a file"
f = open(filename)
Expand All @@ -40,22 +66,28 @@ def view_name_from(path):

def generate_table_from(data):
"Output a nicely formatted ascii table"
table = Texttable(max_width=120)
table.add_row(["view", "method", "status", "count", "minimum", "maximum", "mean", "stdev"])
table.set_cols_align(["l", "l", "l", "r", "r", "r", "r", "r"])

import csv
import sys

writer = csv.writer(sys.stdout)

writer.writerow(["view", "method", "status", "count", "minimum", "maximum", "mean", "stdev", "queries", "querytime"])

for item in data:
mean = round(sum(data[item]['times'])/data[item]['count'], 3)

mean_sql = round(sum(data[item]['sql'])/data[item]['count'], 3)
mean_sqltime = round(sum(data[item]['sqltime'])/data[item]['count'], 3)

sdsq = sum([(i - mean) ** 2 for i in data[item]['times']])
try:
stdev = '%.2f' % ((sdsq / (len(data[item]['times']) - 1)) ** .5)
except ZeroDivisionError:
stdev = '0.00'

table.add_row([data[item]['view'], data[item]['method'], data[item]['status'], data[item]['count'], data[item]['minimum'], data[item]['maximum'], '%.3f' % mean, stdev])
writer.writerow([data[item]['view'], data[item]['method'], data[item]['status'], data[item]['count'], data[item]['minimum'], data[item]['maximum'], '%.3f' % mean, stdev, mean_sql, mean_sqltime])

return table.draw()

def analyze_log_file(logfile, pattern, reverse_paths=True, progress=True):
"Given a log file and regex group and extract the performance data"
Expand All @@ -78,6 +110,8 @@ def analyze_log_file(logfile, pattern, reverse_paths=True, progress=True):
path = parsed[2]
status = parsed[3]
time = parsed[4]
sql = parsed[5]
sqltime = parsed[6]

try:
ignore = False
Expand All @@ -98,6 +132,8 @@ def analyze_log_file(logfile, pattern, reverse_paths=True, progress=True):
if time > data[key]['maximum']:
data[key]['maximum'] = time
data[key]['times'].append(float(time))
data[key]['sql'].append(int(sql))
data[key]['sqltime'].append(float(sqltime))
except KeyError:
data[key] = {
'count': 1,
Expand All @@ -107,6 +143,8 @@ def analyze_log_file(logfile, pattern, reverse_paths=True, progress=True):
'view': view,
'method': method,
'times': [float(time)],
'sql': [int(sql)],
'sqltime': [float(sqltime)],
}
except Resolver404:
pass
Expand Down
7 changes: 6 additions & 1 deletion src/timelog/management/commands/analyze_timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class Command(BaseCommand):
action='store_false',
default=True,
help='Show paths instead of views'),
make_option('--noprogress',
dest='progress',
action='store_false',
default=True,
help='Don''t show the progress bar'),
)

def handle(self, *args, **options):
Expand All @@ -31,7 +36,7 @@ def handle(self, *args, **options):
LOGFILE = options.get('file')

try:
data = analyze_log_file(LOGFILE, PATTERN, reverse_paths=options.get('reverse'))
data = analyze_log_file(LOGFILE, PATTERN, reverse_paths=options.get('reverse'), progress=options.get('progress'))
except IOError:
print "File not found"
exit(2)
Expand Down
21 changes: 16 additions & 5 deletions src/timelog/middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
import logging
from django.db import connection
from django.utils.encoding import smart_str

logger = logging.getLogger(__name__)
Expand All @@ -15,11 +16,21 @@ def process_response(self, request, response):
# before TimeLogMiddleware then request won't have '_start' attribute
# and the original traceback will be lost (original exception will be
# replaced with AttributeError)

sqltime = 0.0

for q in connection.queries:
sqltime += float(getattr(q, 'time', 0.0))

if hasattr(request, '_start'):
d = {'method': request.method,
'time': time.time() - request._start,
'code': response.status_code,
'url': smart_str(request.path_info)}
msg = '%(method)s "%(url)s" (%(code)s) %(time).2f' % d
d = {
'method': request.method,
'time': time.time() - request._start,
'code': response.status_code,
'url': smart_str(request.path_info),
'sql': len(connection.queries),
'sqltime': sqltime,
}
msg = '%(method)s "%(url)s" (%(code)s) %(time).2f (%(sql)dq, %(sqltime).4f)' % d
logger.info(msg)
return response