Skip to content
This repository has been archived by the owner on Mar 24, 2020. It is now read-only.

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
acdha committed Sep 10, 2010
0 parents commit 94f4750
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 0 deletions.
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright 2010 Chris Adams. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.

THIS SOFTWARE IS PROVIDED BY Chris Adams ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Chris Adams OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Chris Adams.
23 changes: 23 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Django Speed Tracer
===================

Simple performance monitoring for Django using Google Chrome's Speed Tracer

Installation
------------

#. Download and install Speed Tracer: http://code.google.com/webtoolkit/speedtracer/get-started.html

#. Add ``"speedtracer"`` to your ``INSTALLED_APPS``

#. Add ``"speedtracer.middleware.SpeedTracerMiddleware"`` to the beginning of
your ``MIDDLEWARE_CLASSES`` (this is important if you're also using projects like
``django-localeurl`` which alter normal URL routing)

#. Load your page inside Chrome with SpeedTracer enabled

#. Open SpeedTracer and expand the "Server Trace" in the page's detailed
report which should look something like this:

.. image:: http://farm5.static.flickr.com/4115/4815493734_4c20d6894f.jpg

24 changes: 24 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from distutils.core import setup

setup(
name='django-speedtracer',
version='0.1',
license='BSD',
description="Profile your Django request processing using Google Chrome's SpeedTracer",
author='Chris Adams',
author_email='chris@improbable.org',
url='http://github.com/acdha/django-speedtracer',
packages=[
'speedtracer',
],
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Framework :: Django',
],
)

Empty file added speedtracer/__init__.py
Empty file.
193 changes: 193 additions & 0 deletions speedtracer/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# encoding: utf-8

import os
import re
import inspect
import time
import uuid
import sys

from django.conf import settings
from django.core.cache import cache
from django.http import HttpResponse
from django.utils import simplejson


class SpeedTracerMiddleware(object):
"""
Record server-side performance data for Google Chrome's SpeedTracer
Getting started:
1. Download and install Speed Tracer:
http://code.google.com/webtoolkit/speedtracer/get-started.html
2. Add this middleware to your MIDDLEWARE_CLASSES
3. Reload your page
4. Open SpeedTracer and expand the "Server Trace" in the page's detailed
report which should look something like http://flic.kr/p/8kwEw3
NOTE: Trace data is store in the Django cache. Yours must be functional.
"""

#: Traces will be stored in the cache with keys using this prefix:
CACHE_PREFIX = getattr(settings, "SPEEDTRACER_CACHE_PREFIX", 'speedtracer-%s')

#: Help debug SpeedTracerMiddleware:
DEBUG = getattr(settings, 'SPEEDTRACER_DEBUG', False)

#: Trace into Django code:
TRACE_DJANGO = getattr(settings, 'SPEEDTRACER_TRACE_DJANGO', False)

#: Trace data will be retrieved from here:
TRACE_URL = getattr(settings, "SPEEDTRACER_API_URL", '/__speedtracer__/')

def __init__(self):
self.traces = []
self.call_stack = []

file_filter = getattr(settings, "SPEEDTRACER_FILE_FILTER_RE", None)
if isinstance(file_filter, basestring):
file_filter = re.compile(file_filter)
elif file_filter is None:
# We'll build a list of installed app modules from INSTALLED_APPS
app_dirs = set()
for app in settings.INSTALLED_APPS:
try:
if app.startswith("django.") and not self.TRACE_DJANGO:
continue

for k, v in sys.modules.items():
if k.startswith(app):
app_dirs.add(*sys.modules[app].__path__)
except KeyError:
print >>sys.stderr, "Can't get path for app: %s" % app

app_dir_re = "(%s)" % "|".join(map(re.escape, app_dirs))

print >> sys.stderr, "Autogenerated settings.SPEEDTRACER_FILE_FILTER_RE: %s" % app_dir_re

file_filter = re.compile(app_dir_re)

self.file_filter = file_filter

def trace_callback(self, frame, event, arg):
if not event in ('call', 'return'):
return

if not self.file_filter.match(frame.f_code.co_filename):
return # No trace

if self.DEBUG:
print "%s: %s %s[%s]" % (
event,
frame.f_code.co_name,
frame.f_code.co_filename,
frame.f_lineno,
)

if event == 'call':
code = frame.f_code

class_name = module_name = ""

module = inspect.getmodule(code)
if module:
module_name = module.__name__

try:
class_name = frame.f_locals['self'].__class__.__name__
except (KeyError, AttributeError):
pass

new_record = {
'operation': {
'sourceCodeLocation': {
'className' : frame.f_code.co_filename,
'methodName' : frame.f_code.co_name,
'lineNumber' : frame.f_lineno,
},
'type': 'METHOD',
'label': '.'.join(filter(None, (module_name, class_name, frame.f_code.co_name))),
},
'children': [],
'range': {"start_time": time.time() },
}

new_record['id'] = id(new_record)

self.call_stack.append(new_record)

return self.trace_callback

elif event == 'return':
end_time = time.time()

if not self.call_stack:
print >>sys.stderr, "Return without stack?"
return

current_frame = self.call_stack.pop()

current_frame['range'] = self._build_range(current_frame['range']["start_time"], end_time)

if not self.call_stack:
self.traces.append(current_frame)
else:
self.call_stack[-1]['children'].append(current_frame)

return

def process_request(self, request):
if not request.path.startswith(self.TRACE_URL):
request._speedtracer_start_time = time.time()
sys.settrace(self.trace_callback)
return

trace_id = self.CACHE_PREFIX % request.path[len(self.TRACE_URL):]

data = cache.get(trace_id, {})

return HttpResponse(simplejson.dumps(data), mimetype="application/json; charset=UTF-8")

def process_response(self, request, response):
sys.settrace(None)

try:
start_time = request._speedtracer_start_time
except AttributeError:
return response

end_time = time.time()

trace_id = uuid.uuid4()

data = {
'trace': {
'id': str(trace_id),
'application': 'Django SpeedTracer',
'date': time.time(),
'range': self._build_range(start_time, end_time),
'frameStack': {
'id': 0,
'range': self._build_range(start_time, end_time),
'operation': {
'type': 'HTTP',
'label': "{0.method} {0.path}".format(request)
},
'children': self.traces,
}
}
}

cache.set(self.CACHE_PREFIX % trace_id, data, getattr(settings, "SPEEDTRACER_TRACE_TTL", 3600))

response['X-TraceUrl'] = "%s%s" % (self.TRACE_URL, trace_id)

return response

def _build_range(self, start_time, end_time):
return {
"start": start_time,
"end": end_time,
"duration": end_time - start_time,
}

0 comments on commit 94f4750

Please sign in to comment.