Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 23, 2024
1 parent 93966cd commit 9293c67
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ pplot_out/
docker-bake.override.json

# benchmark
.benchmarks/
.benchmarks/
95 changes: 55 additions & 40 deletions tests/benchmark/test_json_contains.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import pytest
import random
import string

import pytest
from aiida import orm
from aiida.orm.querybuilder import QueryBuilder

GROUP_NAME = 'json-contains'


COMPLEX_JSON_DEPTH_RANGE=[2**i for i in range(4)]
COMPLEX_JSON_BREADTH_RANGE=[2**i for i in range(4)]
LARGE_TABLE_SIZE_RANGE=[2**i for i in range(1, 11)]
COMPLEX_JSON_DEPTH_RANGE = [2**i for i in range(4)]
COMPLEX_JSON_BREADTH_RANGE = [2**i for i in range(4)]
LARGE_TABLE_SIZE_RANGE = [2**i for i in range(1, 11)]


def gen_json(depth: int, breadth: int):
def gen_str(n: int, with_digits: bool = True):
population = string.ascii_letters
if with_digits: population += string.digits
if with_digits:
population += string.digits
return ''.join(random.choices(population, k=n))

if depth == 0: # random primitive value
# real numbers are not included as their equivalence is tricky
return random.choice([
random.randint(-114, 514), # integers
gen_str(6), # strings
random.choice([True, False]), # booleans
None, # nulls
])
return random.choice(
[
random.randint(-114, 514), # integers
gen_str(6), # strings
random.choice([True, False]), # booleans
None, # nulls
]
)

else:
gen_dict = random.choice([True, False])
Expand Down Expand Up @@ -59,18 +62,22 @@ def extract_component(data, p: float = -1):
@pytest.mark.usefixtures('aiida_profile_clean')
def test_deep_json(benchmark, depth, breadth):
lhs = gen_json(depth, breadth)
rhs = extract_component(lhs, p=1./depth)
rhs = extract_component(lhs, p=1.0 / depth)
assert 0 == len(QueryBuilder().append(orm.Dict).all())

orm.Dict({
'id': f'{depth}-{breadth}',
'data': lhs,
}).store()
qb = QueryBuilder().append(orm.Dict, filters={
'attributes.data': {'contains': rhs},
}, project=[
'attributes.id'
])
orm.Dict(
{
'id': f'{depth}-{breadth}',
'data': lhs,
}
).store()
qb = QueryBuilder().append(
orm.Dict,
filters={
'attributes.data': {'contains': rhs},
},
project=['attributes.id'],
)
qb.all()
result = benchmark(qb.all)
assert len(result) == 1
Expand All @@ -82,18 +89,22 @@ def test_deep_json(benchmark, depth, breadth):
@pytest.mark.usefixtures('aiida_profile_clean')
def test_wide_json(benchmark, depth, breadth):
lhs = gen_json(depth, breadth)
rhs = extract_component(lhs, p=1./depth)
rhs = extract_component(lhs, p=1.0 / depth)
assert 0 == len(QueryBuilder().append(orm.Dict).all())

orm.Dict({
'id': f'{depth}-{breadth}',
'data': lhs,
}).store()
qb = QueryBuilder().append(orm.Dict, filters={
'attributes.data': {'contains': rhs},
}, project=[
'attributes.id'
])
orm.Dict(
{
'id': f'{depth}-{breadth}',
'data': lhs,
}
).store()
qb = QueryBuilder().append(
orm.Dict,
filters={
'attributes.data': {'contains': rhs},
},
project=['attributes.id'],
)
qb.all()
result = benchmark(qb.all)
assert len(result) == 1
Expand All @@ -108,15 +119,19 @@ def test_large_table(benchmark, num_entries):
assert 0 == len(QueryBuilder().append(orm.Dict).all())

for i in range(num_entries):
orm.Dict({
'id': f'N={num_entries}, i={i}',
'data': data,
}).store()
qb = QueryBuilder().append(orm.Dict, filters={
'attributes.data': {'contains': rhs},
}, project=[
'attributes.id'
])
orm.Dict(
{
'id': f'N={num_entries}, i={i}',
'data': data,
}
).store()
qb = QueryBuilder().append(
orm.Dict,
filters={
'attributes.data': {'contains': rhs},
},
project=['attributes.id'],
)
qb.all()
result = benchmark(qb.all)
assert len(result) == num_entries

0 comments on commit 9293c67

Please sign in to comment.