Skip to content
Open
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
167 changes: 167 additions & 0 deletions benchmarks/asynchronous/test_basic_doc_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import asyncio
from asyncio import AbstractEventLoop
from typing import Callable

from pytest_benchmark.fixture import BenchmarkFixture

from mongoengine import Document, StringField, IntField, ListField, BooleanField, EmailField, EmbeddedDocument, \
EmbeddedDocumentField
from mongoengine import async_connect, async_disconnect


class Book(Document):
meta = {"collection": "book"}
name = StringField()
pages = IntField()
tags = ListField(StringField())
is_published = BooleanField()
author_email = EmailField()


class Contact(EmbeddedDocument):
name = StringField()
title = StringField()
address = StringField()


class Company(Document):
meta = {"collection": "company"}
name = StringField()
contacts = ListField(EmbeddedDocumentField(Contact))


class TestBasicDocOps:

@staticmethod
async def drop_collections():
await Book.adrop_collection()

@classmethod
def setup_class(cls):
cls.loop = asyncio.new_event_loop()
asyncio.set_event_loop(cls.loop)
cls.loop.run_until_complete(cls.connect())
cls.loop.run_until_complete(cls.drop_collections())
cls.loop.run_until_complete(cls.disconnect())

@classmethod
def teardown_class(cls):
cls.loop = asyncio.new_event_loop()
asyncio.set_event_loop(cls.loop)
cls.loop.run_until_complete(cls.connect())
cls.loop.run_until_complete(cls.drop_collections())
cls.loop.run_until_complete(cls.disconnect())

@staticmethod
async def connect():
await async_connect(db="testDB")

@staticmethod
async def disconnect():
await async_disconnect()

@staticmethod
def book_doc() -> Book:
return Book(
name="Always be closing",
pages=100,
tags=["-help", "sales"],
is_published=True,
author_email="alec@example.com",
)

def async_benchmark(self, benchmark: BenchmarkFixture, func: Callable):
async_loop: AbstractEventLoop = asyncio.new_event_loop()
async_loop.run_until_complete(self.connect())

def run(event_loop: AbstractEventLoop):
return event_loop.run_until_complete(
func()
)

benchmark(run, async_loop)
async_loop.run_until_complete(self.disconnect())

async def doc_save_and_delete(self):
doc = await self.book_doc().asave()
await doc.adelete()

def test_doc_to_mongo(self, benchmark: BenchmarkFixture):
benchmark(lambda: self.book_doc().to_mongo())

def test_doc_create(self, benchmark: BenchmarkFixture):
benchmark(lambda: self.book_doc())

def test_doc_save_and_delete(self, benchmark: BenchmarkFixture):
self.async_benchmark(benchmark, lambda: self.doc_save_and_delete())

def test_doc_validate(self, benchmark: BenchmarkFixture):
benchmark(lambda: self.book_doc())


class TestBasicLargeDocOps:
@staticmethod
async def drop_collections():
await Company.adrop_collection()

@classmethod
def setup_class(cls):
cls.loop = asyncio.new_event_loop()
asyncio.set_event_loop(cls.loop)
cls.loop.run_until_complete(cls.connect())
cls.loop.run_until_complete(cls.drop_collections())
cls.loop.run_until_complete(cls.disconnect())

@classmethod
def teardown_class(cls):
cls.loop = asyncio.new_event_loop()
asyncio.set_event_loop(cls.loop)
cls.loop.run_until_complete(cls.connect())
cls.loop.run_until_complete(cls.drop_collections())
cls.loop.run_until_complete(cls.disconnect())

@staticmethod
async def connect():
await async_connect(db="testDB")

@staticmethod
async def disconnect():
await async_disconnect()

@staticmethod
def company_doc() -> Company:
return Company(
name="MongoDB, Inc.",
contacts=[
Contact(name="Contact %d" % x, title="CEO", address="Address %d" % x)
for x in range(1000)
],
)

def async_benchmark(self, benchmark: BenchmarkFixture, func: Callable):
async_loop: AbstractEventLoop = asyncio.new_event_loop()
async_loop.run_until_complete(self.connect())

def run(event_loop: AbstractEventLoop):
return event_loop.run_until_complete(
func()
)

benchmark(run, async_loop)
async_loop.run_until_complete(self.disconnect())

async def big_doc_save_and_delete(self):
doc = await self.company_doc().asave()
await doc.adelete()

def test_big_doc_to_mongo(self, benchmark: BenchmarkFixture):
benchmark(lambda: self.company_doc().to_mongo())

def test_big_doc_create(self, benchmark: BenchmarkFixture):
benchmark(lambda: self.company_doc())

def test_big_doc_save_and_delete(self, benchmark: BenchmarkFixture):
self.async_benchmark(benchmark, lambda: self.big_doc_save_and_delete())

def test_big_doc_validate(self, benchmark: BenchmarkFixture):
benchmark(lambda: self.company_doc())
100 changes: 100 additions & 0 deletions benchmarks/asynchronous/test_save_with_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import asyncio
from asyncio import AbstractEventLoop
from typing import Callable

from pytest_benchmark.fixture import BenchmarkFixture

from mongoengine import Document, IntField, StringField
from mongoengine import async_connect, async_disconnect


class User0(Document):
name = StringField()
age = IntField()
meta = {"collection": "company"}


class User1(Document):
name = StringField()
age = IntField()
meta = {"indexes": [["name"]]}


class User2(Document):
name = StringField()
age = IntField()
meta = {"indexes": [["name", "age"]]}


class User3(Document):
name = StringField()
age = IntField()
meta = {"indexes": [["name"]], "auto_create_index_on_save": True}


class User4(Document):
name = StringField()
age = IntField()
meta = {"indexes": [["name", "age"]], "auto_create_index_on_save": True}


class TestSaveWithIndexes:

@staticmethod
async def drop_collections():
await User0.adrop_collection()
await User1.adrop_collection()
await User2.adrop_collection()
await User3.adrop_collection()
await User4.adrop_collection()

@classmethod
def setup_class(cls):
cls.loop = asyncio.new_event_loop()
asyncio.set_event_loop(cls.loop)
cls.loop.run_until_complete(cls.connect())
cls.loop.run_until_complete(cls.drop_collections())
cls.loop.run_until_complete(cls.disconnect())

@classmethod
def teardown_class(cls):
cls.loop = asyncio.new_event_loop()
asyncio.set_event_loop(cls.loop)
cls.loop.run_until_complete(cls.connect())
cls.loop.run_until_complete(cls.drop_collections())
cls.loop.run_until_complete(cls.disconnect())

@staticmethod
async def connect():
await async_connect(db="testDB")

@staticmethod
async def disconnect():
await async_disconnect()

def async_benchmark(self, benchmark: BenchmarkFixture, func: Callable):
async_loop: AbstractEventLoop = asyncio.new_event_loop()
async_loop.run_until_complete(self.connect())

def run(event_loop: AbstractEventLoop):
return event_loop.run_until_complete(
func()
)

benchmark(run, async_loop)
async_loop.run_until_complete(self.disconnect())

def test_doc_without_index(self, benchmark: BenchmarkFixture):
self.async_benchmark(benchmark, lambda: User0(name="Nunu", age=9).asave())

def test_doc_with_1_index(self, benchmark: BenchmarkFixture):
self.async_benchmark(benchmark, lambda: User1(name="Nunu", age=9).asave())

def test_doc_with_2_index(self, benchmark: BenchmarkFixture):
self.async_benchmark(benchmark, lambda: User2(name="Nunu", age=9).asave())

def test_doc_with_1_auto_created_index(self, benchmark: BenchmarkFixture):
self.async_benchmark(benchmark, lambda: User3(name="Nunu", age=9).asave())

def test_doc_with_2_auto_created_index(self, benchmark: BenchmarkFixture):
self.async_benchmark(benchmark, lambda: User4(name="Nunu", age=9).asave())
File renamed without changes.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ test = [
"pillow (>=7.0.0)",
"tox (>=4.32.0)",
"tox-uv>=1.29.0",
"pytest-benchmark>=5.2.3",
]

[project.urls]
Expand Down
24 changes: 24 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.