Skip to content

Commit 95f9467

Browse files
authored
add benchmarks for get_for_dialect (#1862)
1 parent 3a5e836 commit 95f9467

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

CHANGELOG.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ Changelog
66

77
.. rst-class:: emphasize-children
88

9+
0.25
10+
====
11+
12+
0.25.0 (unreleased)
13+
------
14+
Fixed
15+
^^^^^
16+
17+
Changed
18+
^^^^^^^
19+
- add benchmarks for `get_for_dialect` (#1862)
20+
921
0.24
1022
====
1123

tests/benchmarks/conftest.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66

77
import pytest
88

9-
from tests.testmodels import BenchmarkFewFields, BenchmarkManyFields
9+
from tests.testmodels import (
10+
BenchmarkFewFields,
11+
BenchmarkManyFields,
12+
Tournament,
13+
Event,
14+
Team,
15+
DecimalFields,
16+
)
1017
from tortoise.contrib.test import _restore_default, truncate_all_models
1118

1219

@@ -87,3 +94,22 @@ def _gen():
8794
}
8895

8996
return _gen
97+
98+
99+
@pytest.fixture
100+
def create_team_with_participants() -> None:
101+
async def _create() -> None:
102+
tournament = await Tournament.create(name="New Tournament")
103+
event = await Event.create(name="Test", tournament_id=tournament.id)
104+
team = await Team.create(name="Some Team")
105+
await event.participants.add(team)
106+
107+
asyncio.get_event_loop().run_until_complete(_create())
108+
109+
110+
@pytest.fixture
111+
def create_decimals() -> None:
112+
async def _create() -> None:
113+
await DecimalFields.create(decimal=Decimal("1.23456"), decimal_nodec=18.7)
114+
115+
asyncio.get_event_loop().run_until_complete(_create())
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import asyncio
2+
3+
from tests.testmodels import BenchmarkFewFields, DecimalFields
4+
from tortoise.expressions import F
5+
from tortoise.functions import Count
6+
7+
8+
def test_expressions_count(benchmark, few_fields_benchmark_dataset):
9+
loop = asyncio.get_event_loop()
10+
11+
@benchmark
12+
def bench():
13+
async def _bench():
14+
await BenchmarkFewFields.annotate(text_count=Count("text"))
15+
16+
loop.run_until_complete(_bench())
17+
18+
19+
def test_expressions_f(benchmark, create_decimals):
20+
loop = asyncio.get_event_loop()
21+
22+
@benchmark
23+
def bench():
24+
async def _bench():
25+
await DecimalFields.annotate(d=F("decimal")).all()
26+
27+
loop.run_until_complete(_bench())
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from tortoise.fields import Field
2+
3+
4+
class MyField(Field):
5+
@property
6+
def MY_PROPERTY(self):
7+
return f"hi from {self.__class__.__name__}!"
8+
9+
OTHER_PROPERTY = "something else"
10+
11+
class _db_property:
12+
def __init__(self, field: "Field"):
13+
self.field = field
14+
15+
@property
16+
def MY_PROPERTY(self):
17+
return f"hi from {self.__class__.__name__} of {self.field.__class__.__name__}!"
18+
19+
class _db_cls_attribute:
20+
MY_PROPERTY = "cls_attribute"
21+
22+
23+
def test_field_attribute_lookup_get_for_dialect(benchmark):
24+
field = MyField()
25+
26+
@benchmark
27+
def bench():
28+
field.get_for_dialect("property", "MY_PROPERTY")
29+
field.get_for_dialect("postgres", "MY_PROPERTY")
30+
field.get_for_dialect("cls_attribute", "MY_PROPERTY")
31+
field.get_for_dialect("property", "OTHER_PROPERTY")
32+
field.get_for_dialect("property", "MY_PROPERTY")

tests/benchmarks/test_relations.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import asyncio
2+
3+
from tests.testmodels import Event
4+
5+
6+
def test_relations_values_related_m2m(benchmark, create_team_with_participants):
7+
loop = asyncio.get_event_loop()
8+
9+
@benchmark
10+
def bench():
11+
async def _bench():
12+
await Event.all().values("participants__name")
13+
14+
loop.run_until_complete(_bench())

0 commit comments

Comments
 (0)