.. include:: ./refs.rst

===========
Performance
===========

Django management command startup time has the potential to be noticeably slow, especially for large
projects with many apps and models. Django must be fully bootstrapped before management commands can
run. This can make the CLI feel sluggish and presents particular challenges for shell completion as
this delay is incurred each time ``<tab>`` is pressed.

If you are experiencing difficulty with shell completion we recommend:

1. Enabling menu based completion navigation in your terminal. This can reduce the number of calls
   to :django-admin:`shellcompletion`. For z-shell this would look like:

   .. code-block:: bash

       zstyle ':completion:*' menu select

2. Use import and performance analysis tools to determine if there are import bottle necks that can
   be lazily loaded.

The following benchmarks show total module loads, import and runtimes. All times are in seconds.

The first benchmark is for a minimal project that uses the settings file generated by
:django-admin:`startproject`. You can see that when :pypi:`rich` is installed several hundred extra
modules must be imported. This adds a few 10s of milliseconds to the import time. To avoid this,
uninstall :pypi:`rich`. As of :pypi:`Typer` 0.17, :pypi:`rich` is lazily loaded, but `until certain
improvements <https://github.com/django-commons/django-typer/issues/170>`_ are made to :pypi:`Typer`
, the import time penalty is still incurred by :pypi:`django-typer`.

.. raw:: html
    :file: _static/img/minimal_profile.svg


The second benchmark shows the same stats for the :ref:`polls example code <building_commands>`.
These benchmarks compare the :class:`~django.core.management.BaseCommand` native implementation
to the :class:`~django_typer.management.TyperCommand` implementation. They show that the overhead of
using :pypi:`django-typer` is minimal. On the order of a few 10s of milliseconds. Adding
:pypi:`rich` increases this overhead slightly, but the impact is still barely noticeable.


.. raw:: html
    :file: _static/img/polls_profile.svg

