Skip to main content

Decorator for measuring the time and number of database queries

Project description

Class that supports the function of decorator, iterator and context manager for measuring the time and number of database queries

Project Status
CI/CD Latest Release
Quality Coverage
Package PyPI - Version PyPI - Support Python Versions Project PyPI - Downloads
Meta types - Mypy License - MIT code style - Ruff

Class that supports the function of decorator, iterator and context manager for measuring the time and number of database queries

Install

  1. Install package
    pip install capture-db-queries
    

About decorator

CaptureQueries class as decorator can call the body of the decorated function or class as iterator can run code inside for loop the specified number of times for multiple measurements, it can validate the total number of queries. The functionality of the classic context manager is also available.

  • Optional parameters:
    • assert_q_count: The expected number of database requests is otherwise "AssertionError: N not less than or equal to N queries"
    • number_runs: The number of runs of the test function _
    • verbose: Displaying the final results of the test measurements
    • advanced_verb: Displaying the result of each test measurement
    • auto_call_func: Autorun of the decorated function (without arguments)
    • queries: Displaying raw SQL queries to the database
    • connection: Connecting to your database, by default: django.db.connection

Usage examples

from capture_db_queries.decorators import CaptureQueries

for ctx in CaptureQueries(number_runs=2, advanced_verb=True):
    response = self.client.get(url)

# OR

@CaptureQueries(number_runs=2, advanced_verb=True)
def test_request():
    response = self.client.get(url)

# OR

# NOTE: The with context manager does not support multi-launch number_runs > 1
with CaptureQueries(number_runs=1, advanced_verb=True) as ctx:
    response = self.client.get(url)

>>> Test 1 | Queries count: 10 | Execution time: 0.04s
>>> Test 2 | Queries count: 10 | Execution time: 0.04s
>>> Tests count: 2  |  Total queries count: 20  |  Total execution time: 0.08s  |  Median time one test is: 0.041s  |  Vendor: sqlite

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

capture_db_queries-1.1.0.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

capture_db_queries-1.1.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file capture_db_queries-1.1.0.tar.gz.

File metadata

  • Download URL: capture_db_queries-1.1.0.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for capture_db_queries-1.1.0.tar.gz
Algorithm Hash digest
SHA256 fcff9c980b74cf071f4fa1555f428ccf24e5012d328fa1fba376626fe30182ed
MD5 a4082a055a4a45f9c1ee44c6a7f74cd9
BLAKE2b-256 09a92f9184c5924e1df22a8764a813d4e8a62743f38af5544d97c78266c49d56

See more details on using hashes here.

Provenance

The following attestation bundles were made for capture_db_queries-1.1.0.tar.gz:

Publisher: publish-to-pypi.yml on Friskes/capture-db-queries

Attestations:

File details

Details for the file capture_db_queries-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for capture_db_queries-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbfcbf7fb5b4df46234c03d58af50c76aa45ee4722e589a4e03bee8963632ad7
MD5 64f74030c6c1f5c8e2b3b70e02396e04
BLAKE2b-256 4ded5881caf19f8e324f30dfc99628c2931932882ae32f37a147810cb6b2d912

See more details on using hashes here.

Provenance

The following attestation bundles were made for capture_db_queries-1.1.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on Friskes/capture-db-queries

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page