Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add benchmarks for get_for_dialect #1862

Merged
merged 6 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ Changelog

.. rst-class:: emphasize-children

0.25
====

0.25.0 (unreleased)
------
Fixed
^^^^^

Changed
^^^^^^^
- add benchmarks for `get_for_dialect` (#1862)

0.24
====

Expand Down
28 changes: 27 additions & 1 deletion tests/benchmarks/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@

import pytest

from tests.testmodels import BenchmarkFewFields, BenchmarkManyFields
from tests.testmodels import (
BenchmarkFewFields,
BenchmarkManyFields,
Tournament,
Event,
Team,
DecimalFields,
)
from tortoise.contrib.test import _restore_default, truncate_all_models


Expand Down Expand Up @@ -87,3 +94,22 @@ def _gen():
}

return _gen


@pytest.fixture
def create_team_with_participants() -> None:
async def _create() -> None:
tournament = await Tournament.create(name="New Tournament")
event = await Event.create(name="Test", tournament_id=tournament.id)
team = await Team.create(name="Some Team")
await event.participants.add(team)

asyncio.get_event_loop().run_until_complete(_create())


@pytest.fixture
def create_decimals() -> None:
async def _create() -> None:
await DecimalFields.create(decimal=Decimal("1.23456"), decimal_nodec=18.7)

asyncio.get_event_loop().run_until_complete(_create())
27 changes: 27 additions & 0 deletions tests/benchmarks/test_expressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import asyncio

from tests.testmodels import BenchmarkFewFields, DecimalFields
from tortoise.expressions import F
from tortoise.functions import Count


def test_expressions_count(benchmark, few_fields_benchmark_dataset):
loop = asyncio.get_event_loop()

@benchmark
def bench():
async def _bench():
await BenchmarkFewFields.annotate(text_count=Count("text"))

loop.run_until_complete(_bench())


def test_expressions_f(benchmark, create_decimals):
loop = asyncio.get_event_loop()

@benchmark
def bench():
async def _bench():
await DecimalFields.annotate(d=F("decimal")).all()

loop.run_until_complete(_bench())
32 changes: 32 additions & 0 deletions tests/benchmarks/test_field_attribute_lookup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from tortoise.fields import Field


class MyField(Field):
@property
def MY_PROPERTY(self):
return f"hi from {self.__class__.__name__}!"

OTHER_PROPERTY = "something else"

class _db_property:
def __init__(self, field: "Field"):
self.field = field

@property
def MY_PROPERTY(self):
return f"hi from {self.__class__.__name__} of {self.field.__class__.__name__}!"

class _db_cls_attribute:
MY_PROPERTY = "cls_attribute"


def test_field_attribute_lookup_get_for_dialect(benchmark):
field = MyField()

@benchmark
def bench():
field.get_for_dialect("property", "MY_PROPERTY")
field.get_for_dialect("postgres", "MY_PROPERTY")
field.get_for_dialect("cls_attribute", "MY_PROPERTY")
field.get_for_dialect("property", "OTHER_PROPERTY")
field.get_for_dialect("property", "MY_PROPERTY")
14 changes: 14 additions & 0 deletions tests/benchmarks/test_relations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import asyncio

from tests.testmodels import Event


def test_relations_values_related_m2m(benchmark, create_team_with_participants):
loop = asyncio.get_event_loop()

@benchmark
def bench():
async def _bench():
await Event.all().values("participants__name")

loop.run_until_complete(_bench())
Loading