7

I want to use line profiler to profile my django project to show the analysis of code performance.

I am following these links:

  1. http://djangotricks.blogspot.in/2015/01/performance-bottlenecks-in-django-views.html

  2. https://github.com/dcramer/django-devserver

but this is not working for me.I am getting an error regarding the devserver as below:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/.virtualenvs/test/lib/python3.4/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/.virtualenvs/test/lib/python3.4/site-packages/django/core/management/__init__.py", line 342, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/.virtualenvs/test/lib/python3.4/site-packages/django/core/management/__init__.py", line 195, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/.virtualenvs/test/lib/python3.4/site-packages/django/core/management/__init__.py", line 39, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/.virtualenvs/test/lib64/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1467, in exec_module
  File "<frozen importlib._bootstrap>", line 1572, in get_code
  File "<frozen importlib._bootstrap>", line 1532, in source_to_code
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/.virtualenvs/test/lib/python3.4/site-packages/devserver/management/commands/runserver.py", line 29
    raise exc_type, exc_value, tb
                  ^
SyntaxError: invalid syntax

Note: Versions I am using - Django : 1.9 and python : 3.4

Sneha Shinde
  • 343
  • 7
  • 18

3 Answers3

11

Solution found here: https://lothiraldan.github.io/2018-02-18-python-line-profiler-without-magic/

Tested on Python 3.8 and Django 3.2.

Just before your function in your django code, paste this:

import line_profiler
import atexit
profile = line_profiler.LineProfiler()
atexit.register(profile.print_stats)

@profile
def my_function_in_django_code():
    ....

Then, launch the normal server ./manage.py runserver.

When you exit the server (Ctrl+C), it will show on exit the result of Line_profiler.

Other solution: Simply output to a file:

import line_profiler
profile = line_profiler.LineProfiler()


@profile
def my_function_in_django_code():
    ....

    with open('output.txt', 'w') as stream:
        profile.print_stats(stream=stream)        

    return something
ThePhi
  • 2,373
  • 3
  • 28
  • 38
1

It appears that django-devserver currently doesn't support python3.

There is a github issue and pull request to fix that. You either have to use python2 or wait till it completely supports python3.

v1k45
  • 8,070
  • 2
  • 30
  • 38
1

I wanted to run line-profiler on a Django app and stumbled on this StackOverflow post. The accepted answer doesn't appear to work on Python 3, and the last PyPI release of django-devserver was in 2014.

I ended up using pyinstrument instead, which can be added to a Django app using a single update to the middleware list. Rather than a line-by-line timing view, pyinstrument provides a stack-based view of the timings, which actually worked out better for my use case.

Tony S Yu
  • 3,003
  • 30
  • 40