Skip to content

Commit

Permalink
Add data graph stress tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blokhin committed Apr 12, 2023
1 parent 6ab64c1 commit 985a0d8
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
53 changes: 53 additions & 0 deletions tests/stress/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import random

import set_path
from i_data import Data_type
from i_structures.chemical_formulae import common_chem_elements
from i_calculations.topas import get_pattern_name


common_chem_elements = common_chem_elements.split()
POSSIBLE_CONTENT = list(range(42))


def gen_data_item(data_type):
meta = {}
content = random.choice(POSSIBLE_CONTENT)

if data_type == Data_type.calculation:
meta["name"] = gen_chem_formula()

elif data_type == Data_type.structure:
meta["name"] = gen_chem_formula()

elif data_type == Data_type.property:
meta["name"] = gen_chem_formula() # FIXME

elif data_type == Data_type.pattern:
meta["name"] = get_pattern_name()

else:
raise RuntimeError

return (meta, content, data_type)


def gen_chem_formula():
els = set()
pseudo_formula = ""

for _ in range(random.randint(1, 7)):
els.add(random.choice(common_chem_elements))

els = list(els)

for n in range(len(els)):
coeff = random.randint(1, 10)
coeff = "" if coeff == 1 else str(coeff)
pseudo_formula += els[n] + coeff

return pseudo_formula


if __name__ == "__main__":
print(gen_chem_formula())
8 changes: 8 additions & 0 deletions tests/stress/set_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import sys, os.path

INCL_PATH = os.path.realpath(
os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../"))
)

if not INCL_PATH in sys.path:
sys.path.insert(0, INCL_PATH)
62 changes: 62 additions & 0 deletions tests/stress/test_prepare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python

import sys
import random
import logging

from helpers import gen_data_item

import set_path
from i_data import Data_type
from utils import get_data_storage


FAKE_NODES = 5000
FAKE_LINKS = 5000

ALLOWED_TRANSITIONS = (
(Data_type.structure, Data_type.property),
(Data_type.structure, Data_type.pattern),
(Data_type.pattern, Data_type.structure),
(Data_type.pattern, Data_type.property),
(Data_type.property, Data_type.structure),
(Data_type.property, Data_type.pattern),
)

db = get_data_storage()
used_uuids = {}

for _ in range(FAKE_NODES):
selected_dtype = random.choice(
(
Data_type.calculation,
Data_type.structure,
Data_type.property,
Data_type.pattern,
)
)
used_uuids.setdefault(selected_dtype, []).append(
db.put_item(*gen_data_item(selected_dtype))
)

count_links, num_iters = 0, 0

while True:
source_dtype, target_dtype = random.choice(ALLOWED_TRANSITIONS)
source, target = random.choice(used_uuids[source_dtype]), random.choice(
used_uuids[target_dtype]
)

if db.put_link(source, target):
count_links += 1
# else:
# logging.warning('Duplicate link between %s and %s occured on %s-th step' % (source, target, count_links))

if count_links == FAKE_LINKS:
break

num_iters += 1
if num_iters > FAKE_LINKS * 3:
break

db.close()
39 changes: 39 additions & 0 deletions tests/stress/test_stress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python

import sys
import random
import logging

from helpers import POSSIBLE_CONTENT

import set_path
from utils import get_data_storage


db = get_data_storage()

accumulated = []

for _ in range(100):
logging.warning("=" * 100)

found = db.search_item(random.choice(POSSIBLE_CONTENT))
assert found

item = db.get_item(found["uuid"], with_links=True)

accumulated += [found["uuid"]]
accumulated += item["children"]
accumulated += item["parents"]
logging.warning("Collected %s items" % len(accumulated))

items = db.get_items(accumulated, with_links=True)

children, parents = [], []
for one in items:
children += one["children"]
parents += one["parents"]

logging.warning("Got %s children and %s parents" % (len(children), len(parents)))

db.close()

0 comments on commit 985a0d8

Please sign in to comment.