kamalbanga
kamalbanga

Reputation: 2011

Django + Gunicorn + nginx yields very poor performance. Can't get even 8 qps

I am using nginx + gunicorn for serving a django app and have deployed it on EC2 (m1.small instance).

I have this view:

def hi(request):
    return HttpResponse('hi', content_type='text/plain')

mapped to url /hi/. So it basically just returns hi at [myurl]/hi.

Now when I load test this domain ([myurl]/hi) from loader.io, this doesn't even pass the 250 clients over 30 secs test. (Approx 8 requests per second)

This is (part of) my nginx access.log file. It basically just gives 499s after few 200s. (Timeout in loader.io is set to 10 secs)

I must be doing something seriously wrong. How do I find out?

I profiled it using yet-another-django-profiler and following is the output: enter image description here

I deployed this django app on Elastic Beanstalk (which uses Apache server) too (m3.large instance), and there too I get terrible performance. My middleware as of now is:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    # 'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    # 'silk.middleware.SilkyMiddleware',
    # 'yet_another_django_profiler.middleware.ProfilerMiddleware',
    # 'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    # 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    # 'django.contrib.messages.middleware.MessageMiddleware',
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    # 'django.middleware.security.SecurityMiddleware',
)

Earlier none of it was commented out. When I commented out these 9 lines, I got a performance boost. Now I can get 60 qps out of this app. But I think I am doing more blunders and that it can scale further.

Upvotes: 7

Views: 1911

Answers (2)

epi.log
epi.log

Reputation: 373

I have no clue about what you may did wrong but may I suggest you to test again using uWSGI instead of Gunicorn ? According to many benchmarks, uWSGI seems to provides better performance than Gunicorn and if the performance doesn't change significantly you'll be able to focus your investigation on nginx or EC2 configurations. I don't want to proselytize, just encouraging you to find through a process of elimination.

Hope it will help !

Upvotes: 0

Andrei Avram
Andrei Avram

Reputation: 948

This is just a stab in the dark without more to go on, and I don't have enought reputation to comment on your original question: I noticed that your gunicorn startup script has

--log-level=debug

If you have debug level logging (in gunicorn and especially in the Django project) it would explain why the performance was increased by commenting out the middleware and why you would keep getting degraded performance out of a different deployment architecture.

Debug level logging outputs A LOT of information.

Upvotes: 2

Related Questions