Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
art049 committed Jul 27, 2023
1 parent 739a683 commit 5e77078
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 3 deletions.
6 changes: 4 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"python.formatting.provider": "black",
"editor.rulers": [88],
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
Expand All @@ -22,5 +21,8 @@
"--no-pretty"
],
"python.languageServer": "Pylance",
"python.linting.pydocstyleArgs": ["--config=./.pydocstyle"]
"python.linting.pydocstyleArgs": ["--config=./.pydocstyle"],
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ tasks:

setup:deps-setup:
cmds:
- flit install --deps=all
- flit install --deps=all --python python
sources:
- pyproject.toml

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ test = [
"typer ~= 0.4.1",
"semver ~= 2.13.0",
"types-pytz ~= 2022.1.1",
"pytest-speed ~= 0.3.5",
"pytest-codspeed ~= 2.1.0",
]
doc = [
"pydocstyle[toml] ~= 6.1.1",
Expand Down
Empty file.
62 changes: 62 additions & 0 deletions tests/integration/benchmarks/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Models based on https://github.com/tortoise/orm-benchmarks"""

from datetime import datetime
from decimal import Decimal
from random import choice
from typing import Iterator, Literal, Optional, get_args

from odmantic import Field, Model

Level = Literal[10, 20, 30, 40, 50]
VALID_LEVELS = list(get_args(Level))


class SmallJournal(Model):
timestamp: datetime = Field(default_factory=datetime.utcnow)
level: Level = Field(index=True)
text: str = Field(index=True)

@classmethod
def get_random_instances(cls, context: str, count: int) -> Iterator["SmallJournal"]:
for i in range(count):
yield cls(level=choice(VALID_LEVELS), text=f"From {context}, item {i}")


class JournalWithRelations(Model):
timestamp: datetime = Field(default_factory=datetime.utcnow)
level: Level = Field(index=True)
text: str = Field(index=True)

# parent


class BigJournal(Model):
timestamp: datetime = Field(default_factory=datetime.utcnow)
level: Level = Field(index=True)
text: str = Field(index=True)

col_float1: float = Field(default=2.2)
col_smallint1: int = Field(default=2)
col_int1: int = Field(default=2000000)
col_bigint1: int = Field(default=99999999)
col_char1: str = Field(default=255, max_length=255)
col_text1: str = Field(
default="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa",
)
col_decimal1: Decimal = Field(default=Decimal("2.2"))
col_json1: dict = Field(
default={"a": 1, "b": "b", "c": [2], "d": {"e": 3}, "f": True},
)

col_float2: Optional[float] = Field(default=None)
col_smallint2: Optional[int] = Field(default=None)
col_int2: Optional[int] = Field(default=None)
col_bigint2: Optional[int] = Field(default=None)
col_char2: Optional[str] = Field(default=None, max_length=255)
col_text2: Optional[str] = Field(
default=None,
)
col_decimal2: Optional[Decimal] = Field(default=None)
col_json2: Optional[dict] = Field(
default=None,
)
23 changes: 23 additions & 0 deletions tests/integration/benchmarks/test_bench_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from contextlib import nullcontext

import pytest
from pytest_codspeed import BenchmarkFixture

from odmantic import AIOEngine, SyncEngine

from .models import VALID_LEVELS, SmallJournal

pytestmark = pytest.mark.asyncio


@pytest.mark.parametrize("count", [10, 50, 100])
async def test_insert_small_single(
benchmark, aio_engine: AIOEngine, count: int, use_session: bool
):
instances = list(SmallJournal.get_random_instances("test_write_small", count))

@benchmark
async def _():
async with aio_engine.session() as session:
for instance in instances:
await session.save(instance)
87 changes: 87 additions & 0 deletions tests/integration/benchmarks/test_bench_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from contextlib import nullcontext

import pytest
from pytest_codspeed import BenchmarkFixture

from odmantic import AIOEngine, SyncEngine

from .models import VALID_LEVELS, SmallJournal


@pytest.mark.parametrize("count", [10, 50, 100])
@pytest.mark.parametrize(
"use_session", [pytest.param(True, id="session"), pytest.param(False, id="direct")]
)
def test_insert_small_single(
benchmark, aio_engine: AIOEngine, count: int, use_session: bool
):
instances = list(SmallJournal.get_random_instances("test_write_small", count))

@benchmark
async def _():
with aio_engine.session() if use_session else nullcontext(aio_engine) as engine:
for instance in instances:
engine.save(instance)


@pytest.mark.parametrize("count", [10, 50, 100])
@pytest.mark.parametrize(
"use_session", [pytest.param(True, id="session"), pytest.param(False, id="direct")]
)
def test_write_small_bulk(
benchmark, sync_engine: SyncEngine, count: int, use_session: bool
):
instances = list(SmallJournal.get_random_instances("test_write_small", count))

@benchmark
def _():
with sync_engine.session() if use_session else nullcontext(
sync_engine
) as engine:
engine.save_all(instances)


@pytest.mark.parametrize("count", [10, 50, 100])
def test_filter_by_level_small(benchmark, sync_engine: SyncEngine, count: int):
instances = list(SmallJournal.get_random_instances("test_write_small", count))
sync_engine.save_all(instances)

@benchmark
def _():
total = 0
for level in VALID_LEVELS:
total += len(
list(sync_engine.find(SmallJournal, SmallJournal.level == level))
)


@pytest.mark.parametrize("count", [10, 50, 100])
def test_filter_limit_skip_by_level_small(
benchmark, sync_engine: SyncEngine, count: int
):
instances = list(SmallJournal.get_random_instances("test_write_small", count))
sync_engine.save_all(instances)

@benchmark
def _():
total = 0
for level in VALID_LEVELS:
total += len(
list(
sync_engine.find(
SmallJournal, SmallJournal.level == level, limit=20, skip=20
)
)
)


@pytest.mark.parametrize("count", [10, 50, 100])
def test_find_one_by_id(benchmark, sync_engine: SyncEngine, count: int):
instances = list(SmallJournal.get_random_instances("test_write_small", count))
sync_engine.save_all(instances)
ids = [instance.id for instance in instances]

@benchmark
def _():
for id_ in ids:
sync_engine.find_one(SmallJournal, SmallJournal.id == id_)

0 comments on commit 5e77078

Please sign in to comment.