Skip to main content
https://img.shields.io/pypi/v/django-perf-rec.svg https://img.shields.io/travis/YPlan/django-perf-rec/master.svg

“Keep detailed records of the performance of your Django code.”

django-perf-rec is like Django’s assertNumQueries on steroids. It lets you track the individual queries and cache operations that occur in your code. Use it in your tests like so:

def test_home(self):
    with django_perf_rec.record():
        self.client.get('/')

It then stores a YAML file alongside the test file that tracks the queries and operations, looking something like:

MyTests.test_home:
- cache|get: home_data
- db: 'SELECT ... FROM myapp_table WHERE (myapp_table.id = #)'
- db: 'SELECT ... FROM myapp_table WHERE (myapp_table.id = #)'

Then if you re-run the test and the performance record doesn’t match, the test will fail. Magic! Just check the YAML file in alongside your test and you have unbreakable performance with much better clues about any regressions compared to assertNumQueries.

Installation

Use pip:

pip install django-perf-rec

Requirements

Tested with all combinations of:

  • Python: 2.7, 3.5

  • Django: 1.8, 1.9, 1.10

API

record(file_name=None, record_name=None)

Return a context manager that will be used for a single performance test. file_name is the name of the performance file to be used, and record_name is the name of the record inside that file to use. If either of these names is None, the code assumes you are inside a Django TestCase and uses magic stack inspection to find that test case, and set file_name to the name of the file containing that class with .py replaced by .perf.yml, and record_name will be named after the test case + name, plus an optional counter if you invoke record multiple times inside the same test method.

Whilst open, the context manager tracks all DB queries on all connections, and all cache operations on all defined caches. It names the connection/cache in the tracked operation it uses, except from for the default one.

When the context manager exits, it will use the list of operations it has gathered. If the file file_name doesn’t exist, or doesn’t contain data for the specific record_name, it will be created and saved and the test will pass with no assertions. However if the record does exist inside the file, the collected record will be compared with the original one, and if different, an AssertionError will be raised. This currently has an ugly message but if you’re using pytest its assertion rewriting will be used.

Example:

import django_perf_rec

from app.models import Author

class AuthorPerformanceTests(TestCase):

    def test_special_method(self):
        with django_perf_rec.record():
            list(Author.objects.special_method())

TestCaseMixin

A mixin class to be added to your custom TestCase subclass so you can use django-perf-rec across your codebase without needing to import it in each individual test file. It adds one method, record_performance(), whose signature is the same as record() above.

Example:

# yplan/test.py
from django.test import TestCase as OrigTestCase
from django_perf_rec import TestCaseMixin

class TestCase(TestCaseMixin, OrigTestCase):
    pass

# app/tests/models/test_author.py
from app.models import Author
from yplan.test import TestCase

class AuthorPerformanceTests(TestCase):

    def test_special_method(self):
        with self.record_performance():
            list(Author.objects.special_method())

History

Pending release

  • New release notes go here

1.0.2 (2016-09-23)

  • Confirmed Django 1.8 and 1.10 support.

1.0.1 (2016-09-20)

  • Fix install_requires in setup.py.

1.0.0 (2016-09-19)

  • Initial version with record() that can record database queries and cache operations and error if they change between test runs.

Metadata

Release files for django-perf-rec 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-perf-rec 1.0.2
File Size Uploaded
django-perf-rec-1.0.2.tar.gz 15.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-perf-rec 1.0.2
File Interpreter ABI Platform
django_perf_rec-1.0.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 39.1 kB

Release files / django-perf-rec-1.0.2.tar.gz

Download URL django-perf-rec-1.0.2.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d20924ad88eca8257e6b4e17c7aa039a7adf617e89a0990de350ba3802cd5fec
BLAKE2b-256 checksum
How to use checksums
3668a894290dc9c531154e756645dc9499c23e158baee044e292049ace1d8a8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_perf_rec-1.0.2-py2.py3-none-any.whl

Download URL django_perf_rec-1.0.2-py2.py3-none-any.whl
Size 23.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
107021a3720f7603143c5a0961897a40b69f8c0baa62808a525cc025c6db2e0c
BLAKE2b-256 checksum
How to use checksums
ce7502fb53d34e0f50d2dee4a34d127171eb06029408323e9527de61e6a78fb8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

4.31.0

2 release files

4.27.0

2 release files

4.26.0

2 release files

4.25.0

2 release files

4.24.0

2 release files

4.23.0

2 release files

4.22.3

2 release files

4.22.2

2 release files

4.22.1

2 release files

4.22.0

2 release files

4.20.1

2 release files

4.20.0

2 release files

4.19.0

2 release files

4.18.0

2 release files

4.16.0

2 release files

4.15.0

2 release files

4.14.1

2 release files

4.13.1

2 release files

4.13.0

2 release files

4.12.0

2 release files

4.10.0

2 release files

4.9.0

2 release files

4.8.0

2 release files

4.7.0

2 release files

4.6.1

2 release files

4.6.0

2 release files

4.5.0

2 release files

4.4.0

2 release files

4.3.0

2 release files

4.2.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page