从您的Django数据库性能测试中生成性能报告。
项目描述
使用方法
安装pytest-django-queries
,编写您的pytest测试,并标记任何需要计数或使用count_queries
固定装置的测试。
注意:要使用最新开发版本,请使用pip install --pre pytest-django-queries
import pytest
@pytest.mark.count_queries
def test_query_performances():
Model.objects.all()
# Or...
def test_another_query_performances(count_queries):
Model.objects.all()
每个测试文件和/或包被视为一个类别。类别内的每个测试都组成其数据,有关详细信息,请参阅可视化结果。
您可以在此处找到完整文档。
使用固定装置时的建议
您可能会遇到想要添加生成您不想计数的查询的固定装置的情况,或者您只想与pytest-django-queries
一起使用pytest-django
插件,这将生成您不需要的结果中的查询。
为此,您需要将count_queries
固定装置放在最后一个执行。
但与此同时,您可能还想使用pytest标记的强大功能,将查询计数测试与其他测试分开。在这种情况下,您可能想这样做来告诉标记不要自动将count_queries
固定装置注入到测试中
import pytest
@pytest.mark.count_queries(autouse=False)
def test_retrieve_main_menu(fixture_making_queries, count_queries):
pass
请注意关键字参数autouse=False
的使用,以及count_queries
测试夹具放在最后。
将pytest-django与pytest-django-queries一起使用
当使用pytest-django
时,我们建议您执行以下操作
import pytest
@pytest.mark.django_db
@pytest.mark.count_queries(autouse=False)
def test_retrieve_main_menu(any_fixture, other_fixture, count_queries):
pass
集成GitHub
待定。
本地测试
只需通过pip安装pytest-django-queries
,然后使用pytest
运行您的测试。一个报告应该已经生成在您的当前工作目录中,文件名为.pytest-queries
。
注意:要覆盖保存路径,请将--django-db-bench PATH
选项传递给pytest。
可视化结果
您可以使用show
命令从测试结果生成表格
django-queries show
您将得到如下所示的表示结果
+---------+--------------------------------------+
| Module | Tests |
+---------+--------------------------------------+
| module1 | +-----------+---------+------------+ |
| | | Test Name | Queries | Duplicated | |
| | +-----------+---------+------------+ |
| | | test1 | 0 | 0 | |
| | +-----------+---------+------------+ |
| | | test2 | 1 | 0 | |
| | +-----------+---------+------------+ |
+---------+--------------------------------------+
| module2 | +-----------+---------+------------+ |
| | | Test Name | Queries | Duplicated | |
| | +-----------+---------+------------+ |
| | | test1 | 123 | 0 | |
| | +-----------+---------+------------+ |
+---------+--------------------------------------+
导出结果(HTML)
为了更好的展示,使用html
命令导出结果为HTML。
django-queries html
它将生成如下。
比较结果
在运行测试后,您可以运行django-queries backup
(可以指定路径,可选)然后重新运行。之后,您可以运行django-queries diff
以生成类似以下的结果
开发
首先,在本地克隆项目。然后,使用以下命令安装它。
./setup.py develop
之后,您需要安装开发和测试需求。为此,请运行以下命令。
pip install -e .[test]
项目详情
pytest-django-queries-1.2.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 7d683d06af711c4f3ac049b3a7035070ce3888a3575ce8ae4b8a41b7b7869f1c |
|
MD5 | 6458a60c5215dac13f9d7f594341c182 |
|
BLAKE2b-256 | 04d28d07b2319c06b00e4bd0e164b56cbb12167121a9bd0d7610fb5a54a04264 |