Skip to content

Commit

Permalink
python/performance: Expand profiling section
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed May 6, 2024
1 parent ddc50c1 commit 84cda2f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/python/file_formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ File formats

We read and write a lot of CSV and JSON files. Their format should be consistent.

.. _format-json:

JSON
----

Expand Down
28 changes: 23 additions & 5 deletions docs/python/performance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,27 @@ Performance

:ref:`Django performance<django-performance>`

CPU profiling
-------------
Software performance depends on many choices: language (like :doc:`../rust/index` versus Python), framework (like `FastAPI versus Django <https://www.techempower.com/benchmarks/>`__), architecture (e.g. map-reduce), networking (e.g. batch requests), etc. Many choices are costly to change at a later date (e.g. full rewrite).

Profiling
---------

Use profiling to:

- Identify slow dependencies, in case faster alternatives can be easily swapped in
- Find major hotspots, like a loop that runs in exponential time instead of quadratic time
- Find minor hotspots, if changing language, etc. is too costly

Once a hotspot is found, the solution might be to:

- Call it once, via refactoring: for example, traversing JSON once for all CoVE calculations, instead of once for each calculation, in `lib-cove <https://github.com/OpenDataServices/lib-cove/issues/65>`__
- Call it less, via batching: for example, :ref:`reducing the number of SQL queries in Django projects<django-performance>`
- Cache the results: for example, `caching mergers in Kingfisher Process <https://github.com/open-contracting/kingfisher-process/blob/c4b05204faf08d00ed7914a41c2fd0770e0f6b3e/process/processors/compiler.py#L52>`__
- Process in parallel: for example, distributing work to multiple threads, like we do with :doc:`../services/rabbitmq`
- Replace it entirely: for example, using the :ref:`orjson<format-json>` package instead of the ``json`` library

CPU
~~~

For example:

Expand All @@ -22,8 +41,8 @@ For example:
To see where a running program is spending its time, use `py-spy top <https://github.com/benfred/py-spy>`__.

Memory profiling
----------------
Memory
~~~~~~

For example:

Expand All @@ -33,7 +52,6 @@ For example:
time mprof run libcoveoc4ids data.json
mprof plot
Reference
---------

Expand Down

0 comments on commit 84cda2f

Please sign in to comment.