Skip to content

Commit 759797b

Browse files
committed
update documentation
1 parent 138a471 commit 759797b

File tree

2 files changed

+88
-57
lines changed

2 files changed

+88
-57
lines changed

README.md

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Class that supports the function of decorator, iterator and context manager for measuring the time and number of database queries
1+
# Class to simplify the search for slow and suboptimal sql queries to the database in django projects.
22

33
<div align="center">
44

@@ -11,55 +11,73 @@
1111

1212
</div>
1313

14-
> Class that supports the function of decorator, iterator and context manager for measuring the time and number of database queries
14+
## About class
15+
- Class allows you to track any sql queries that are executed inside the body of a loop, a decorated function, or a context manager, the body can be executed a specified number of times to get the average query execution time.
16+
17+
- The class allows you to display formatted output data containing brief information about the current iteration of the measurement, display sql queries and explain information on them, as well as summary information containing data on all measurements.
18+
19+
> **Do not use the class inside the business logic of your application, this will greatly slow down the execution of the queries, the class is intended only for the test environment.**
1520
1621
## Install
1722
1. Install package
1823
```bash
1924
pip install capture-db-queries
2025
```
2126

22-
## About decorator
23-
`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.
24-
The functionality of the classic context manager is also available.
27+
## About class parameters
28+
> *All parameters are purely optional.*
2529

2630
- Optional parameters:
27-
- `assert_q_count`: The expected number of database requests is otherwise "AssertionError: N not less than or equal to N queries"
28-
- `number_runs`: The number of runs of the test function / test for loop
29-
- `verbose`: Displaying the final results of the test measurements
30-
- `advanced_verb`: Displaying the result of each test measurement
31-
- `auto_call_func`: Autorun of the decorated function (without arguments)
32-
- `queries`: Displaying raw SQL queries to the database
33-
- `explain`: Displaying additional information about each request (has no effect on the orig. requests)
34-
- `explain_opts`: For more information about the parameters for explain, see the documentation for your DBMS
31+
- `assert_q_count`: The expected number of database queries during all `number_runs`, otherwise an exception will be raised: **"AssertionError: `N` not less than or equal to `N` queries"**.
32+
- `number_runs`: The number of runs of the decorated function or test for loop.
33+
- `verbose`: Displaying the final results of test measurements within all `number_runs`.
34+
- `advanced_verb`: Displaying the result of each test measurement.
35+
- `auto_call_func`: Autorun of the decorated function. *(without passing arguments to the function, since the launch takes place inside the class)*.
36+
- `queries`: Displaying colored and formatted SQL queries to the database.
37+
- `explain`: Displaying explain information about each query. (has no effect on the original query).
38+
- `explain_opts`: Parameters for explain. *(for more information about the parameters for explain, see the documentation for your DBMS).*
3539
- `connection`: Connecting to your database, by default: django.db.connection
3640

3741
## Usage examples
3842

3943
```python
40-
from capture_db_queries.decorators import CaptureQueries
44+
from capture_db_queries import CaptureQueries
45+
```
4146

47+
```python
4248
for ctx in CaptureQueries(number_runs=2, advanced_verb=True):
4349
response = self.client.get(url)
4450
45-
# OR
51+
>>> Test №1 | Queries count: 10 | Execution time: 0.04s
52+
>>> Test №2 | Queries count: 10 | Execution time: 0.04s
53+
>>> Tests count: 2 | Total queries count: 20 | Total execution time: 0.08s | Median time one test is: 0.041s | Vendor: sqlite
54+
```
55+
56+
#### OR
4657
58+
```python
4759
@CaptureQueries(number_runs=2, advanced_verb=True)
4860
def test_request():
4961
response = self.client.get(url)
5062
51-
# OR
63+
>>> Test №1 | Queries count: 10 | Execution time: 0.04s
64+
>>> Test №2 | Queries count: 10 | Execution time: 0.04s
65+
>>> Tests count: 2 | Total queries count: 20 | Total execution time: 0.08s | Median time one test is: 0.041s | Vendor: sqlite
66+
```
5267
68+
#### OR
69+
70+
```python
5371
# NOTE: The with context manager does not support multi-launch number_runs > 1
5472
with CaptureQueries(number_runs=1, advanced_verb=True) as ctx:
5573
response = self.client.get(url)
5674
57-
>>> Test №1 | Queries count: 10 | Execution time: 0.04s
58-
>>> Test №2 | Queries count: 10 | Execution time: 0.04s
59-
>>> Tests count: 2 | Total queries count: 20 | Total execution time: 0.08s | Median time one test is: 0.041s | Vendor: sqlite
75+
>>> Queries count: 10 | Execution time: 0.04s | Vendor: sqlite
76+
```
6077
61-
# Example of output when using queries and explain:
78+
> ### Example of output when using queries and explain:
6279
80+
```python
6381
for _ in CaptureQueries(advanced_verb=True, queries=True, explain=True):
6482
list(Reporter.objects.filter(pk=1))
6583
list(Article.objects.filter(pk=1))
@@ -71,7 +89,7 @@ for _ in CaptureQueries(advanced_verb=True, queries=True, explain=True):
7189
>>> SELECT "tests_reporter"."id",
7290
>>> "tests_reporter"."full_name"
7391
>>> FROM "tests_reporter"
74-
>>> WHERE "tests_reporter"."id" = %s
92+
>>> WHERE "tests_reporter"."id" = 1
7593
>>>
7694
>>>
7795
>>> №[2] time=[0.109] explain=['2 0 0 SEARCH TABLE tests_article USING INTEGER PRIMARY KEY (rowid=?)']
@@ -81,7 +99,7 @@ for _ in CaptureQueries(advanced_verb=True, queries=True, explain=True):
8199
>>> "tests_article"."content",
82100
>>> "tests_article"."reporter_id"
83101
>>> FROM "tests_article"
84-
>>> WHERE "tests_article"."id" = %s
102+
>>> WHERE "tests_article"."id" = 1
85103
>>>
86104
>>>
87105
>>> Tests count: 1 | Total queries count: 2 | Total execution time: 0.22s | Median time one test is: 0.109s | Vendor: sqlite

src/capture_db_queries/decorators.py

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -214,34 +214,70 @@ def _assert_queries_count(self, queries_count: int) -> None:
214214

215215
class CaptureQueries(AbcCapture):
216216
"""
217-
Measures the number of database requests and
218-
displays detailed information about the measurements,
219-
validates the number of requests.
217+
#### Class to simplify the search for slow and suboptimal sql queries\
218+
to the database in django projects.
220219
221220
---
222221
223-
UseCases::
222+
#### About class
223+
- Class allows you to track any sql queries that are executed inside the body of a loop,
224+
a decorated function, or a context manager,
225+
the body can be executed a specified number of times to get the average query execution time.
226+
227+
- The class allows you to display formatted output data
228+
containing brief information about the current iteration of the measurement,
229+
display sql queries and explain information on them,
230+
as well as summary information containing data on all measurements.
231+
232+
`Do not use the class inside the business logic of your application,
233+
this will greatly slow down the execution of the queries,
234+
the class is intended only for the test environment.`
235+
236+
---
237+
238+
#### - Optional parameters:
239+
- `assert_q_count`: The expected number of database queries during all `number_runs`, otherwise an exception will be raised: "AssertionError: `N` not less than or equal to `N` queries".
240+
- `number_runs`: The number of runs of the decorated function or test for loop.
241+
- `verbose`: Displaying the final results of test measurements within all `number_runs`.
242+
- `advanced_verb`: Displaying the result of each test measurement.
243+
- `auto_call_func`: Autorun of the decorated function. (without passing arguments to the function, since the launch takes place inside the class).
244+
- `queries`: Displaying colored and formatted SQL queries to the database.
245+
- `explain`: Displaying explain information about each query. (has no effect on the original query).
246+
- `explain_opts`: Parameters for explain. (for more information about the parameters for explain, see the documentation for your DBMS).
247+
- `connection`: Connecting to your database, by default: django.db.connection
248+
249+
---
250+
251+
#### Usage examples::
224252
225253
for ctx in CaptureQueries(number_runs=2, advanced_verb=True):
226254
response = self.client.get(url)
227255
228-
OR
256+
>>> Test №1 | Queries count: 10 | Execution time: 0.04s
257+
>>> Test №2 | Queries count: 10 | Execution time: 0.04s
258+
>>> Tests count: 2 | Total queries count: 20 | Total execution time: 0.08s | Median time one test is: 0.041s | Vendor: sqlite
259+
260+
# OR
229261
230262
@CaptureQueries(number_runs=2, advanced_verb=True)
231263
def test_request():
232264
response = self.client.get(url)
233265
234-
OR
266+
>>> Test №1 | Queries count: 10 | Execution time: 0.04s
267+
>>> Test №2 | Queries count: 10 | Execution time: 0.04s
268+
>>> Tests count: 2 | Total queries count: 20 | Total execution time: 0.08s | Median time one test is: 0.041s | Vendor: sqlite
269+
270+
# OR
235271
236272
# NOTE: The with context manager does not support multi-launch number_runs > 1
237273
with CaptureQueries(number_runs=1, advanced_verb=True) as ctx:
238274
response = self.client.get(url)
239275
240-
>>> Test №1 | Queries count: 10 | Execution time: 0.04s
241-
>>> Test №2 | Queries count: 10 | Execution time: 0.04s
242-
>>> Tests count: 2 | Total queries count: 20 | Total execution time: 0.08s | Median time one test is: 0.041s | Vendor: sqlite
276+
>>> Queries count: 10 | Execution time: 0.04s | Vendor: sqlite
277+
278+
---
243279
244-
# Example of output when using queries and explain parameters:
280+
#### Example of output when using queries and explain::
245281
246282
for _ in CaptureQueries(advanced_verb=True, queries=True, explain=True):
247283
list(Reporter.objects.filter(pk=1))
@@ -254,7 +290,7 @@ def test_request():
254290
>>> SELECT "tests_reporter"."id",
255291
>>> "tests_reporter"."full_name"
256292
>>> FROM "tests_reporter"
257-
>>> WHERE "tests_reporter"."id" = %s
293+
>>> WHERE "tests_reporter"."id" = 1
258294
>>>
259295
>>>
260296
>>> №[2] time=[0.109] explain=['2 0 0 SEARCH TABLE tests_article USING INTEGER PRIMARY KEY (rowid=?)']
@@ -264,33 +300,10 @@ def test_request():
264300
>>> "tests_article"."content",
265301
>>> "tests_article"."reporter_id"
266302
>>> FROM "tests_article"
267-
>>> WHERE "tests_article"."id" = %s
303+
>>> WHERE "tests_article"."id" = 1
268304
>>>
269305
>>>
270306
>>> Tests count: 1 | Total queries count: 2 | Total execution time: 0.22s | Median time one test is: 0.109s | Vendor: sqlite
271-
272-
---
273-
274-
- assert_q_count: The expected number of database requests otherwise an
275-
"AssertionError: N not less than or equal to N queries"
276-
- number_runs: The number of runs of the test function / test cycle
277-
- verbose: Displaying the final results of the test measurements
278-
- advanced_verb: Displaying the result of each test measurement
279-
- auto_call_func: Autorun of the decorated function (without arguments)
280-
- queries: Displaying raw SQL queries to the database
281-
- explain: Displaying additional information about each request (has no effect on the orig. requests)
282-
- explain_opts: Parameters for explain, find out more in the documentation for your DBMS
283-
- connection: Connecting to your database, by default: django.db.connection
284-
285-
---
286-
287-
Tests count: Total number of test measurements
288-
Total queries count: The total number of database requests
289-
within the test function within all measurements
290-
Total execution time: Total time for executing database queries
291-
inside the test function within all measurements
292-
Median time one test is: Average execution time of one test measurement
293-
Vendor: The database under test
294307
""" # noqa: E501
295308

296309
def _assert_queries_count(self, queries_count: int) -> None:

0 commit comments

Comments
 (0)