I have a Django application which I decided to port to Google App Engine.
I decided to use NDB as my database and have ported all the models (including django user). After one week of reading through the documentation (the App Engine documentation is awful, the examples given are often outdated and don't work anymore) and porting the application, when I ran it it was really slow: 1s-2s latency with an empty database.
The ndb queries don't take much time (less then 50 ms) and the Appstats application doesn't show me anything else.
I decided to use cProfile and have created an wsgi middleware but I can't figure out how to print the output. The methods pstats gives me either prints the output or saves it to file and I can't do any inside the wsgi handler.
My code is as follows:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.handlers import wsgi
from webob import Request
class AppProfiler(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
from django.core.files.base import ContentFile
self.req = Request(environ)
import cProfile, pstats
prof = cProfile.Profile()
prof = prof.runctx("self.get_resp()", globals(), locals())
print "<pre>"
stats = pstats.Stats(prof)
#stats.dump_stats(output)
stats.print_stats(80)
print "</pre>"
body = self.resp.body # here i should append the stats data
self.resp.body = body
return self.resp(environ, start_response)
def get_resp(self):
self.resp = self.req.get_response(self.app)
app = wsgi.WSGIHandler()
profiler = AppProfiler(app)
How can I append the profiler stats to the body?
Or is there a better way to find out what is slowing down my application?
I'm using a lot of module imports in my django views is there an App Engine way to import modules?