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
Class that supports the function of decorator, iterator and context manager for measuring the time and number of database queries
Install
- 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 / test for loopverbose
: Displaying the final results of the test measurementsadvanced_verb
: Displaying the result of each test measurementauto_call_func
: Autorun of the decorated function (without arguments)queries
: Displaying raw SQL queries to the databaseexplain
: Displaying additional information about each request (has no effect on the orig. requests)explain_opts
: For more information about the parameters for explain, see the documentation for your DBMSconnection
: 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
# Example of output when using queries and explain:
for _ in CaptureQueries(advanced_verb=True, queries=True, explain=True):
list(Reporter.objects.filter(pk=1))
list(Article.objects.filter(pk=1))
>>> Test №1 | Queries count: 2 | Execution time: 0.22s
>>>
>>>
>>> №[1] time=[0.109] explain=['2 0 0 SEARCH TABLE tests_reporter USING INTEGER PRIMARY KEY (rowid=?)']
>>> SELECT "tests_reporter"."id",
>>> "tests_reporter"."full_name"
>>> FROM "tests_reporter"
>>> WHERE "tests_reporter"."id" = %s
>>>
>>>
>>> №[2] time=[0.109] explain=['2 0 0 SEARCH TABLE tests_article USING INTEGER PRIMARY KEY (rowid=?)']
>>> SELECT "tests_article"."id",
>>> "tests_article"."pub_date",
>>> "tests_article"."headline",
>>> "tests_article"."content",
>>> "tests_article"."reporter_id"
>>> FROM "tests_article"
>>> WHERE "tests_article"."id" = %s
>>>
>>>
>>> Tests count: 1 | Total queries count: 2 | Total execution time: 0.22s | Median time one test is: 0.109s | 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
Built Distribution
File details
Details for the file capture_db_queries-1.2.0.tar.gz
.
File metadata
- Download URL: capture_db_queries-1.2.0.tar.gz
- Upload date:
- Size: 26.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5dfc1a6d3ebd2fa771c223656160aa9fd099c7fc4be1691a4e03c17fcba6b990 |
|
MD5 | 03ed8200b56512276ec90306f0f6533c |
|
BLAKE2b-256 | 3990bf9c53ca21be63d1b10fc14b4d7afeee8601ec0568d2bee9195caad12ee3 |
Provenance
The following attestation bundles were made for capture_db_queries-1.2.0.tar.gz
:
Publisher:
publish-to-pypi.yml
on Friskes/capture-db-queries
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
capture_db_queries-1.2.0.tar.gz
- Subject digest:
5dfc1a6d3ebd2fa771c223656160aa9fd099c7fc4be1691a4e03c17fcba6b990
- Sigstore transparency entry: 150899092
- Sigstore integration time:
- Predicate type:
File details
Details for the file capture_db_queries-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: capture_db_queries-1.2.0-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 744ed73253eda3ba631e04787c0bfa93e9a7a737e309cee2730cc240fde54aa2 |
|
MD5 | 94db996d92479f6c86c7d96ba63fb952 |
|
BLAKE2b-256 | 958185548b5b42ced6d8e786b39826101f7963c4679dde8a42105ae536b69afe |
Provenance
The following attestation bundles were made for capture_db_queries-1.2.0-py3-none-any.whl
:
Publisher:
publish-to-pypi.yml
on Friskes/capture-db-queries
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
capture_db_queries-1.2.0-py3-none-any.whl
- Subject digest:
744ed73253eda3ba631e04787c0bfa93e9a7a737e309cee2730cc240fde54aa2
- Sigstore transparency entry: 150899094
- Sigstore integration time:
- Predicate type: