Simple profile middleware

 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: limodou@gmail.com
# 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. Format transition middleware by limodou 7 years, 1 month ago
  2. Database file storage by powerfox 5 years, 2 months ago
  3. Url filter middleware by limodou 7 years, 1 month ago
  4. staticview for app by limodou 6 years, 10 months ago
  5. db_dump.py - for dumpping and loading data from database by limodou 7 years, 1 month 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.

#

(Forgotten your password?)