diff --git a/tests/stress/helpers.py b/tests/stress/helpers.py new file mode 100644 index 0000000..58f9398 --- /dev/null +++ b/tests/stress/helpers.py @@ -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()) diff --git a/tests/stress/set_path.py b/tests/stress/set_path.py new file mode 100644 index 0000000..d1d3cfb --- /dev/null +++ b/tests/stress/set_path.py @@ -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) diff --git a/tests/stress/test_prepare.py b/tests/stress/test_prepare.py new file mode 100644 index 0000000..6efd694 --- /dev/null +++ b/tests/stress/test_prepare.py @@ -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() diff --git a/tests/stress/test_stress.py b/tests/stress/test_stress.py new file mode 100644 index 0000000..d4ad553 --- /dev/null +++ b/tests/stress/test_stress.py @@ -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()