Login

Simple profile middleware

Author:
limodou
Posted:
March 1, 2007
Language:
Python
Version:
Pre .96
Score:
6 (after 6 ratings)

Intall

In your settings.py, set it in MIDDLEWARE_CLASSES. Default all the profile files will be save in ./profile folder(if there is no this directory, it'll automatically create one), and you can set a PROFILE_DATA_DIR option in settings.py, so that the profile files can be saved in this folder.

How does it works

Record every request in a profile file. As you can see in the code:

profname = "%s.prof" % (request.path.strip("/").replace('/', '.'))

so if you request an url multi-times, only the last request will be saved, because previous profile files will be overriden. And you can find a commentted line, it'll save each request in different file according the time, so if you like that you can change the code.

#        profname = "%s.%.3f.prof" % (request.path.strip("/").replace('/', '.'), time.time())

and if you want to see the output, you can run below code, but you should change the filename value:

import hotshot, hotshot.stats
stats = hotshot.stats.load(filename)
#stats.strip_dirs()
#stats.sort_stats('time', 'calls')
stats.print_stats()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Author: [email protected]
# version: 0.2
# Profile request of django
#

import hotshot
import os
import time
from django.conf import settings

PROFILE_DATA_DIR = "./profile"
class ProfileMiddleware(object):
    def process_request(self, request):
        path = getattr(settings, 'PROFILE_DIR', PROFILE_DATA_DIR)
        if not os.path.exists(path):
            os.makedirs(path)
            os.chmod(path, 0755)
#        profname = "%s.%.3f.prof" % (request.path.strip("/").replace('/', '.'), time.time())
        profname = "%s.prof" % (request.path.strip("/").replace('/', '.'))
        profname = os.path.join(PROFILE_DATA_DIR, profname)
        try:
            self.prof = prof = hotshot.Profile(profname)
            prof.start()
        except:
            self.prof = None
        
#    def process_view(self, request, callback, callback_args, callback_kwargs):
#        try:
#            return prof.runcall(callback, request, *callback_args, **callback_kwargs)
#        finally:
#            prof.close()
#            
            
    def process_response(self, request, response):
        if self.prof:
            self.prof.close()
        return response

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

polarbear (on March 31, 2007):

How should I see, analyze this generated *.prof files?

#

polarbear (on March 31, 2007):

Problem with hotshot.stats:

>>> import hotshot, hotshot.stats
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "hotshot/stats.py", line 3, in ?
    ImportError: No module named profile

Should I install something?

#

limodou (on April 2, 2007):

No, You don't need to install third package, see the example:

import hotshot, hotshot.stats
stats = hotshot.stats.load(filename)
#stats.strip_dirs()
#stats.sort_stats('time', 'calls')
stats.print_stats()

But this middleware seems has bug in current version now, I don't know why.

#

Please login first before commenting.