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