Skip to content

Commit 8448881

Browse files
Static test fixes
1 parent beecc46 commit 8448881

File tree

3 files changed

+131
-4
lines changed

3 files changed

+131
-4
lines changed

mops/mixins/internal_mixin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from mops.utils.internal_utils import (
99
get_child_elements_with_names,
1010
get_child_elements,
11+
get_all_attributes_from_object,
1112
)
1213

1314

@@ -35,9 +36,9 @@ def get_element_info(element: Any, _is_initial_call: bool = True) -> str:
3536
def get_static_with_bases(cls: Any) -> dict:
3637
return get_child_elements_with_names(cls)
3738

38-
@lru_cache(maxsize=16)
39+
@lru_cache(maxsize=64)
3940
def get_static_without_bases(cls: Any) -> dict:
40-
return cls.__dict__
41+
return get_all_attributes_from_object(cls)
4142

4243
class InternalMixin:
4344

tests/static_tests/integration/test_child_elements.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def test_child_elements(mocked_selenium_driver):
2929
section3 = Section3()
3030
assert section3.some_element
3131
assert section3.some_element._initialized # noqa
32-
assert section3.child_elements
32+
assert section3.sub_elements
3333

3434
assert section2.some_element
3535
assert section2.some_element._initialized # noqa
36-
assert section2.child_elements
36+
assert section2.sub_elements
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import cProfile
2+
import pstats
3+
import tracemalloc
4+
import time
5+
6+
import pytest
7+
8+
from mops.base.element import Element
9+
from mops.base.group import Group
10+
from mops.base.page import Page
11+
12+
13+
section_sub_elements_count = 5000
14+
15+
16+
class AnotherSection1(Group):
17+
18+
def __init__(self):
19+
super().__init__('AnotherSection')
20+
21+
22+
class AnotherSection(Group):
23+
24+
def __init__(self):
25+
super().__init__('AnotherSection')
26+
27+
another_some_element = Element('AnotherSection_another_some_element')
28+
29+
30+
class SomeSection(Group):
31+
32+
def __init__(self, locator):
33+
super().__init__(locator)
34+
35+
AnotherSection = AnotherSection()
36+
37+
38+
class SomePage(Page):
39+
40+
def __init__(self, driver_wrapper = None):
41+
super().__init__('SomePage', driver_wrapper=driver_wrapper)
42+
43+
44+
45+
@pytest.fixture(scope='module')
46+
def set_elements_class_var_objects():
47+
for _i in range(section_sub_elements_count):
48+
_element = Element(f'{_i}_element')
49+
setattr(AnotherSection1, _element.name, _element)
50+
51+
52+
@pytest.mark.parametrize('case', range(5))
53+
def test_performance_element_initialisation(mocked_selenium_driver, case, set_elements_class_var_objects):
54+
tracemalloc.start()
55+
start_cpu = time.process_time()
56+
57+
with cProfile.Profile() as pr:
58+
section = AnotherSection1()
59+
60+
end_cpu = time.process_time()
61+
cpu_time = end_cpu - start_cpu # CPU time used
62+
63+
peak_mem = tracemalloc.get_traced_memory()[1] / 1024**2
64+
tracemalloc.stop()
65+
66+
stats: pstats.Stats = pstats.Stats(pr)
67+
stats.strip_dirs().sort_stats("time").print_stats(20)
68+
69+
print('stats.total_tt=', stats.total_tt)
70+
print('peak_mem=', peak_mem)
71+
print('cpu_time=', cpu_time)
72+
73+
assert stats.total_tt < 0.45, f"Execution time too high: {stats.total_tt:.3f} sec"
74+
assert peak_mem < 11, f"Peak memory usage too high: {peak_mem:.2f} MB"
75+
assert len(section.sub_elements) == section_sub_elements_count, \
76+
f"Expected {section_sub_elements_count} elements, got {len(section.sub_elements)}"
77+
assert cpu_time < 0.45, f"CPU execution time too high: {cpu_time:.3f} sec"
78+
79+
80+
@pytest.fixture(scope='module')
81+
def set_groups_class_var_objects():
82+
for _i in range(20):
83+
_element = Element(f'AnotherSection_another_some_element_{_i}')
84+
setattr(AnotherSection, _element.name, _element)
85+
86+
for _i in range(50):
87+
_element = Element(f'SomeSection_some_element_{_i}')
88+
setattr(SomeSection, _element.name, _element)
89+
90+
for _i in range(50):
91+
_section = SomeSection(f'{_i}_SomeSection')
92+
setattr(SomePage, _section.name, _section)
93+
94+
95+
@pytest.mark.parametrize('case', range(5))
96+
def test_performance_group_initialisation(mocked_selenium_driver, case, set_groups_class_var_objects):
97+
98+
tracemalloc.start()
99+
start_cpu = time.process_time()
100+
101+
with cProfile.Profile() as pr:
102+
page = SomePage()
103+
104+
end_cpu = time.process_time()
105+
cpu_time = end_cpu - start_cpu # CPU time used
106+
107+
peak_mem = tracemalloc.get_traced_memory()[1] / 1024**2
108+
tracemalloc.stop()
109+
110+
stats: pstats.Stats = pstats.Stats(pr)
111+
stats.strip_dirs().sort_stats("time").print_stats(20)
112+
113+
count = len(page.sub_elements)
114+
for page_object in page.sub_elements.values():
115+
count += len(page_object.sub_elements)
116+
for sub_element in page_object.sub_elements.values():
117+
count += len(sub_element.sub_elements)
118+
119+
print('stats.total_tt=', stats.total_tt)
120+
print('peak_mem=', peak_mem)
121+
print('cpu_time=', cpu_time)
122+
123+
assert stats.total_tt < 1.5, f"Execution time too high: {stats.total_tt:.3f} sec"
124+
assert peak_mem < 4, f"Peak memory usage too high: {peak_mem:.2f} MB"
125+
assert cpu_time < 1.5, f"CPU execution time too high: {cpu_time:.3f} sec"
126+
assert count > 3600, f"Expected 3600 elements, got {count}"

0 commit comments

Comments
 (0)