-
-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Codspeed performance benchmarks (#1831)
* Add Codspeed * Add create and get tests with bigger model * Use Python 3.12 * Skip benchmarks during regular tests
- Loading branch information
Showing
9 changed files
with
544 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: CodSpeed | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
# `workflow_dispatch` allows CodSpeed to trigger backtest | ||
# performance analysis in order to generate initial data. | ||
workflow_dispatch: | ||
|
||
jobs: | ||
benchmarks: | ||
name: Run benchmarks | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-python@v5 | ||
with: | ||
# 3.12 is the minimum reqquired version for profiling enabled | ||
python-version: "3.12" | ||
|
||
- name: Install and configure Poetry | ||
run: | | ||
pip install -U pip poetry | ||
poetry config virtualenvs.create false | ||
- name: Install dependencies | ||
run: make build | ||
|
||
- name: Run benchmarks | ||
uses: CodSpeedHQ/action@v3 | ||
with: | ||
token: ${{ secrets.CODSPEED_TOKEN }} | ||
run: pytest tests/benchmarks --codspeed |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
from decimal import Decimal | ||
import random | ||
|
||
import pytest | ||
|
||
from tests.testmodels import BenchmarkFewFields, BenchmarkManyFields | ||
from tortoise.contrib.test import _restore_default, truncate_all_models | ||
|
||
|
||
@pytest.fixture(scope="function", autouse=True) | ||
def setup_database(): | ||
_restore_default() | ||
yield | ||
asyncio.get_event_loop().run_until_complete(truncate_all_models()) | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def skip_if_codspeed_not_enabled(request): | ||
if not request.config.getoption("--codspeed", default=None): | ||
pytest.skip("codspeed is not enabled") | ||
|
||
|
||
@pytest.fixture | ||
def few_fields_benchmark_dataset() -> list[BenchmarkFewFields]: | ||
async def _create() -> list[BenchmarkFewFields]: | ||
res = [] | ||
for _ in range(100): | ||
level = random.randint(0, 100) # nosec | ||
res.append(await BenchmarkFewFields.create(level=level, text="test")) | ||
return res | ||
|
||
return asyncio.get_event_loop().run_until_complete(_create()) | ||
|
||
|
||
@pytest.fixture | ||
def many_fields_benchmark_dataset() -> list[BenchmarkManyFields]: | ||
async def _create() -> list[BenchmarkManyFields]: | ||
res = [] | ||
for _ in range(100): | ||
res.append( | ||
await BenchmarkManyFields.create( | ||
level=random.randint(0, 100), # nosec | ||
text="test", | ||
col_float1=2.2, | ||
col_smallint1=2, | ||
col_int1=2000000, | ||
col_bigint1=99999999, | ||
col_char1="value1", | ||
col_text1="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa", | ||
col_decimal1=Decimal("2.2"), | ||
col_json1={"a": 1, "b": "b", "c": [2], "d": {"e": 3}, "f": True}, | ||
col_float2=0.2, | ||
col_smallint2=None, | ||
col_int2=22, | ||
col_bigint2=None, | ||
col_char2=None, | ||
col_text2=None, | ||
col_decimal2=None, | ||
col_json2=None, | ||
col_float3=2.2, | ||
col_smallint3=2, | ||
col_int3=2000000, | ||
col_bigint3=99999999, | ||
col_char3="value1", | ||
col_text3="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa", | ||
col_decimal3=Decimal("2.2"), | ||
col_json3={"a": 1, "b": 2, "c": [2]}, | ||
col_float4=0.00004, | ||
col_smallint4=None, | ||
col_int4=4, | ||
col_bigint4=99999999000000, | ||
col_char4="value4", | ||
col_text4="AAAAAAAA", | ||
col_decimal4=None, | ||
col_json4=None, | ||
) | ||
) | ||
return res | ||
|
||
return asyncio.get_event_loop().run_until_complete(_create()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import asyncio | ||
from decimal import Decimal | ||
import random | ||
|
||
from tests.testmodels import BenchmarkFewFields, BenchmarkManyFields | ||
|
||
|
||
def test_create_few_fields(benchmark): | ||
loop = asyncio.get_event_loop() | ||
|
||
@benchmark | ||
def bench(): | ||
async def _bench(): | ||
level = random.randint(0, 100) # nosec | ||
await BenchmarkFewFields.create(level=level, text="test") | ||
|
||
loop.run_until_complete(_bench()) | ||
|
||
|
||
def test_create_many_fields(benchmark): | ||
loop = asyncio.get_event_loop() | ||
|
||
@benchmark | ||
def bench(): | ||
async def _bench(): | ||
await BenchmarkManyFields.create( | ||
level=random.randint(0, 100), # nosec | ||
text="test", | ||
col_float1=2.2, | ||
col_smallint1=2, | ||
col_int1=2000000, | ||
col_bigint1=99999999, | ||
col_char1="value1", | ||
col_text1="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa", | ||
col_decimal1=Decimal("2.2"), | ||
col_json1={"a": 1, "b": "b", "c": [2], "d": {"e": 3}, "f": True}, | ||
col_float2=0.2, | ||
col_smallint2=None, | ||
col_int2=22, | ||
col_bigint2=None, | ||
col_char2=None, | ||
col_text2=None, | ||
col_decimal2=None, | ||
col_json2=None, | ||
col_float3=2.2, | ||
col_smallint3=2, | ||
col_int3=2000000, | ||
col_bigint3=99999999, | ||
col_char3="value1", | ||
col_text3="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa", | ||
col_decimal3=Decimal("2.2"), | ||
col_json3={"a": 1, "b": 2, "c": [2]}, | ||
col_float4=0.00004, | ||
col_smallint4=None, | ||
col_int4=4, | ||
col_bigint4=99999999000000, | ||
col_char4="value4", | ||
col_text4="AAAAAAAA", | ||
col_decimal4=None, | ||
col_json4=None, | ||
) | ||
|
||
loop.run_until_complete(_bench()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import asyncio | ||
import random | ||
|
||
from tests.testmodels import BenchmarkFewFields | ||
|
||
|
||
def test_filter_few_fields(benchmark, few_fields_benchmark_dataset): | ||
loop = asyncio.get_event_loop() | ||
levels = list(set([o.level for o in few_fields_benchmark_dataset])) | ||
|
||
@benchmark | ||
def bench(): | ||
async def _bench(): | ||
await BenchmarkFewFields.filter(level__in=random.sample(levels, 5)).limit(5) | ||
|
||
loop.run_until_complete(_bench()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import asyncio | ||
import random | ||
|
||
from tests.testmodels import BenchmarkFewFields, BenchmarkManyFields | ||
|
||
|
||
def test_get_few_fields(benchmark, few_fields_benchmark_dataset): | ||
loop = asyncio.get_event_loop() | ||
minid = min(o.id for o in few_fields_benchmark_dataset) | ||
maxid = max(o.id for o in few_fields_benchmark_dataset) | ||
|
||
@benchmark | ||
def bench(): | ||
async def _bench(): | ||
randid = random.randint(minid, maxid) # nosec | ||
await BenchmarkFewFields.get(id=randid) | ||
|
||
loop.run_until_complete(_bench()) | ||
|
||
|
||
def test_get_many_fields(benchmark, many_fields_benchmark_dataset): | ||
loop = asyncio.get_event_loop() | ||
minid = min(o.id for o in many_fields_benchmark_dataset) | ||
maxid = max(o.id for o in many_fields_benchmark_dataset) | ||
|
||
@benchmark | ||
def bench(): | ||
async def _bench(): | ||
randid = random.randint(minid, maxid) # nosec | ||
await BenchmarkManyFields.get(id=randid) | ||
|
||
loop.run_until_complete(_bench()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters