Skip to content

Commit 7afc90e

Browse files
authored
Add more Codspeed benchmarks (#1834)
* Add test_filter_many_filters benchmark * Add bulk_create benchmarks
1 parent 01c903a commit 7afc90e

File tree

10 files changed

+112
-85
lines changed

10 files changed

+112
-85
lines changed

.github/workflows/codspeed.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: CodSpeed
33
on:
44
push:
55
branches:
6-
- main
6+
- develop
77
pull_request:
88
# `workflow_dispatch` allows CodSpeed to trigger backtest
99
# performance analysis in order to generate initial data.

tests/benchmarks/conftest.py

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import annotations
22

33
import asyncio
4-
from decimal import Decimal
54
import random
5+
from decimal import Decimal
66

77
import pytest
88

@@ -36,48 +36,54 @@ async def _create() -> list[BenchmarkFewFields]:
3636

3737

3838
@pytest.fixture
39-
def many_fields_benchmark_dataset() -> list[BenchmarkManyFields]:
39+
def many_fields_benchmark_dataset(gen_many_fields_data) -> list[BenchmarkManyFields]:
4040
async def _create() -> list[BenchmarkManyFields]:
4141
res = []
4242
for _ in range(100):
43-
res.append(
44-
await BenchmarkManyFields.create(
45-
level=random.randint(0, 100), # nosec
46-
text="test",
47-
col_float1=2.2,
48-
col_smallint1=2,
49-
col_int1=2000000,
50-
col_bigint1=99999999,
51-
col_char1="value1",
52-
col_text1="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa",
53-
col_decimal1=Decimal("2.2"),
54-
col_json1={"a": 1, "b": "b", "c": [2], "d": {"e": 3}, "f": True},
55-
col_float2=0.2,
56-
col_smallint2=None,
57-
col_int2=22,
58-
col_bigint2=None,
59-
col_char2=None,
60-
col_text2=None,
61-
col_decimal2=None,
62-
col_json2=None,
63-
col_float3=2.2,
64-
col_smallint3=2,
65-
col_int3=2000000,
66-
col_bigint3=99999999,
67-
col_char3="value1",
68-
col_text3="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa",
69-
col_decimal3=Decimal("2.2"),
70-
col_json3={"a": 1, "b": 2, "c": [2]},
71-
col_float4=0.00004,
72-
col_smallint4=None,
73-
col_int4=4,
74-
col_bigint4=99999999000000,
75-
col_char4="value4",
76-
col_text4="AAAAAAAA",
77-
col_decimal4=None,
78-
col_json4=None,
79-
)
80-
)
43+
res.append(await BenchmarkManyFields.create(**gen_many_fields_data()))
8144
return res
8245

8346
return asyncio.get_event_loop().run_until_complete(_create())
47+
48+
49+
@pytest.fixture
50+
def gen_many_fields_data():
51+
def _gen():
52+
return {
53+
"level": random.randint(0, 100), # nosec
54+
"text": "test",
55+
"col_float1": 2.2,
56+
"col_smallint1": 2,
57+
"col_int1": 2000000,
58+
"col_bigint1": 99999999,
59+
"col_char1": "value1",
60+
"col_text1": "Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa",
61+
"col_decimal1": Decimal("2.2"),
62+
"col_json1": {"a": 1, "b": "b", "c": [2], "d": {"e": 3}, "f": True},
63+
"col_float2": 0.2,
64+
"col_smallint2": None,
65+
"col_int2": 22,
66+
"col_bigint2": None,
67+
"col_char2": None,
68+
"col_text2": None,
69+
"col_decimal2": None,
70+
"col_json2": None,
71+
"col_float3": 2.2,
72+
"col_smallint3": 2,
73+
"col_int3": 2000000,
74+
"col_bigint3": 99999999,
75+
"col_char3": "value1",
76+
"col_text3": "Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa",
77+
"col_decimal3": Decimal("2.2"),
78+
"col_json3": {"a": 1, "b": 2, "c": [2]},
79+
"col_float4": 0.00004,
80+
"col_smallint4": None,
81+
"col_int4": 4,
82+
"col_bigint4": 99999999000000,
83+
"col_char4": "value4",
84+
"col_text4": "AAAAAAAA",
85+
"col_decimal4": None,
86+
"col_json4": None,
87+
}
88+
89+
return _gen
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import asyncio
2+
import random
3+
4+
from tests.testmodels import BenchmarkFewFields, BenchmarkManyFields
5+
6+
7+
def test_bulk_create_few_fields(benchmark):
8+
loop = asyncio.get_event_loop()
9+
10+
data = [
11+
BenchmarkFewFields(
12+
level=random.choice([10, 20, 30, 40, 50]), text=f"Insert from C, item {i}" # nosec
13+
)
14+
for i in range(100)
15+
]
16+
17+
@benchmark
18+
def bench():
19+
async def _bench():
20+
await BenchmarkFewFields.bulk_create(data)
21+
22+
loop.run_until_complete(_bench())
23+
24+
25+
def test_bulk_create_many_fields(benchmark, gen_many_fields_data):
26+
loop = asyncio.get_event_loop()
27+
28+
data = [BenchmarkManyFields(**gen_many_fields_data()) for _ in range(100)]
29+
30+
@benchmark
31+
def bench():
32+
async def _bench():
33+
await BenchmarkManyFields.bulk_create(data)
34+
35+
loop.run_until_complete(_bench())

tests/benchmarks/test_create.py

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
from decimal import Decimal
32
import random
43

54
from tests.testmodels import BenchmarkFewFields, BenchmarkManyFields
@@ -17,47 +16,12 @@ async def _bench():
1716
loop.run_until_complete(_bench())
1817

1918

20-
def test_create_many_fields(benchmark):
19+
def test_create_many_fields(benchmark, gen_many_fields_data):
2120
loop = asyncio.get_event_loop()
2221

2322
@benchmark
2423
def bench():
2524
async def _bench():
26-
await BenchmarkManyFields.create(
27-
level=random.randint(0, 100), # nosec
28-
text="test",
29-
col_float1=2.2,
30-
col_smallint1=2,
31-
col_int1=2000000,
32-
col_bigint1=99999999,
33-
col_char1="value1",
34-
col_text1="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa",
35-
col_decimal1=Decimal("2.2"),
36-
col_json1={"a": 1, "b": "b", "c": [2], "d": {"e": 3}, "f": True},
37-
col_float2=0.2,
38-
col_smallint2=None,
39-
col_int2=22,
40-
col_bigint2=None,
41-
col_char2=None,
42-
col_text2=None,
43-
col_decimal2=None,
44-
col_json2=None,
45-
col_float3=2.2,
46-
col_smallint3=2,
47-
col_int3=2000000,
48-
col_bigint3=99999999,
49-
col_char3="value1",
50-
col_text3="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa",
51-
col_decimal3=Decimal("2.2"),
52-
col_json3={"a": 1, "b": 2, "c": [2]},
53-
col_float4=0.00004,
54-
col_smallint4=None,
55-
col_int4=4,
56-
col_bigint4=99999999000000,
57-
col_char4="value4",
58-
col_text4="AAAAAAAA",
59-
col_decimal4=None,
60-
col_json4=None,
61-
)
25+
await BenchmarkManyFields.create(**gen_many_fields_data())
6226

6327
loop.run_until_complete(_bench())

tests/benchmarks/test_filter.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import asyncio
22
import random
3+
from decimal import Decimal
34

4-
from tests.testmodels import BenchmarkFewFields
5+
from tests.testmodels import BenchmarkFewFields, BenchmarkManyFields
56

67

78
def test_filter_few_fields(benchmark, few_fields_benchmark_dataset):
@@ -14,3 +15,24 @@ async def _bench():
1415
await BenchmarkFewFields.filter(level__in=random.sample(levels, 5)).limit(5)
1516

1617
loop.run_until_complete(_bench())
18+
19+
20+
def test_filter_many_filters(benchmark, many_fields_benchmark_dataset):
21+
loop = asyncio.get_event_loop()
22+
levels = list(set([o.level for o in many_fields_benchmark_dataset]))
23+
24+
@benchmark
25+
def bench():
26+
async def _bench():
27+
await BenchmarkManyFields.filter(
28+
level__in=random.sample(levels, 5),
29+
col_float1__gt=0,
30+
col_smallint1=2,
31+
col_int1__lt=2000001,
32+
col_bigint1__in=[99999999],
33+
col_char1__contains="value1",
34+
col_text1="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa",
35+
col_decimal1=Decimal("2.2"),
36+
).limit(5)
37+
38+
loop.run_until_complete(_bench())

tests/schema/models_postgres_fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from tortoise import Model
2-
from tortoise.contrib.postgres.fields import TSVectorField, ArrayField
2+
from tortoise.contrib.postgres.fields import ArrayField, TSVectorField
33

44

55
class PostgresFields(Model):

tortoise/backends/asyncpg/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from tortoise.backends.asyncpg.executor import AsyncpgExecutor
88
from tortoise.backends.asyncpg.schema_generator import AsyncpgSchemaGenerator
99
from tortoise.backends.base.client import (
10-
TransactionalDBClient,
1110
ConnectionWrapper,
1211
NestedTransactionContext,
12+
TransactionalDBClient,
1313
TransactionContext,
1414
TransactionContextPooled,
1515
)

tortoise/backends/mysql/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
from tortoise import timezone
3030
from tortoise.backends.base.client import (
3131
BaseDBAsyncClient,
32-
TransactionalDBClient,
3332
Capabilities,
3433
ConnectionWrapper,
3534
NestedTransactionContext,
3635
PoolConnectionWrapper,
36+
TransactionalDBClient,
3737
TransactionContext,
3838
TransactionContextPooled,
3939
)

tortoise/backends/odbc/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
from tortoise import BaseDBAsyncClient
1010
from tortoise.backends.base.client import (
11-
TransactionalDBClient,
1211
ConnectionWrapper,
1312
NestedTransactionContext,
1413
PoolConnectionWrapper,
14+
TransactionalDBClient,
1515
TransactionContext,
1616
)
1717
from tortoise.backends.odbc.executor import ODBCExecutor

tortoise/backends/sqlite/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
from tortoise.backends.base.client import (
2222
BaseDBAsyncClient,
23-
TransactionalDBClient,
2423
Capabilities,
2524
ConnectionWrapper,
2625
NestedTransactionContext,
2726
T_conn,
27+
TransactionalDBClient,
2828
TransactionContext,
2929
)
3030
from tortoise.backends.sqlite.executor import SqliteExecutor

0 commit comments

Comments
 (0)