forked from cubewise-code/optimus-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutors.py
211 lines (166 loc) · 8.16 KB
/
executors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import logging
import random
import time
from enum import Enum
from itertools import chain
from typing import List, Dict
from TM1py import TM1Service, Process
from results import PermutationResult
def swap(order: list, i1, i2) -> List[str]:
seq = order[:]
seq[i1], seq[i2] = seq[i2], seq[i1]
return seq
def swap_random(order: list) -> List[str]:
idx = range(len(order))
i1, i2 = random.sample(idx, 2)
return swap(order, i1, i2)
class ExecutionMode(Enum):
ORIGINAL_ORDER = 0
ITERATIONS = 1
RESULT = 2
@classmethod
def _missing_(cls, value):
for member in cls:
if member.name.lower() == value.lower():
return member
# default
return cls.ALL
class OptipyzerExecutor:
def __init__(self, tm1: TM1Service, cube_name: str, view_names: list, process_name: str,
displayed_dimension_order: List[str],
executions: int, measure_dimension_only_numeric: bool):
self.tm1 = tm1
self.cube_name = cube_name
self.view_names = view_names
self.process_name = process_name
self.dimensions = displayed_dimension_order
self.executions = executions
self.measure_dimension_only_numeric = measure_dimension_only_numeric
self.mode = None
self.include_process = bool(process_name)
def _determine_query_permutation_result(self) -> Dict[str, List[float]]:
query_times_by_view = {}
for view_name in self.view_names:
query_times = []
for _ in range(self.executions):
self.clear_cube_cache()
before = time.time()
self.tm1.cells.create_cellset_from_view(cube_name=self.cube_name, view_name=view_name, private=False)
query_times.append(time.time() - before)
query_times_by_view[view_name] = query_times
return query_times_by_view
def _determine_process_permutation_result(self) -> Dict[str, List[float]]:
execution_times = []
for _ in range(self.executions):
self.clear_cube_cache()
before = time.time()
try:
success, status, _ = self.tm1.processes.execute_with_return(process_name=self.process_name)
except Exception as e:
raise e
if not success:
raise RuntimeError(f"Process: '{self.process_name}' not successful; Status: '{status}'")
execution_times.append(time.time() - before)
return {self.process_name: execution_times}
def _evaluate_permutation(self, permutation: List[str], retrieve_ram: bool = False,
reset_counter: bool = False) -> PermutationResult:
ram_percentage_change = self.tm1.cubes.update_storage_dimension_order(self.cube_name, permutation)
query_times_by_view = self._determine_query_permutation_result()
process_times_by_process = None
if self.include_process:
process_times_by_process = self._determine_process_permutation_result()
ram_usage = None
if retrieve_ram:
ram_usage = self._retrieve_ram_usage()
permutation_result = PermutationResult(self.mode, self.cube_name, self.view_names, self.process_name,
permutation,
query_times_by_view, process_times_by_process, ram_usage,
ram_percentage_change, reset_counter)
process_log = " - No process included in test"
if self.include_process:
process_log = f" - Process time [s]: {permutation_result.median_process_time():.5f}"
logging.info(f"Evaluated order: {permutation} "
f"- RAM [GB]: {permutation_result.ram_usage / 1024 ** 3:.2f} "
f"- Query time [s]: {permutation_result.median_query_time():.5f}"
+ process_log)
return permutation_result
def _retrieve_ram_usage(self):
number_of_iterations = 4
for i in range(number_of_iterations):
mdx = """
SELECT
{{ [}}PerfCubes].[{}] }} ON ROWS,
{{ [}}StatsStatsByCube].[Total Memory Used] }} ON COLUMNS
FROM [}}StatsByCube]
WHERE ([}}TimeIntervals].[LATEST])
""".format(self.cube_name)
value = list(self.tm1.cells.execute_mdx_values(mdx=mdx))[0]
if value:
return value
logging.info("Failed to retrieve RAM consumption. Waiting 15s before retry")
if i < number_of_iterations - 1:
time.sleep(15)
raise RuntimeError("Performance Monitor must be activated")
def clear_cube_cache(self):
process = Process(name="", prolog_procedure=f"DebugUtility(125 ,0 ,0 ,'{self.cube_name}' ,'' ,'');")
success, status, error_log_file = self.tm1.processes.execute_process_with_return(process)
if not success:
raise RuntimeError(f"Failed to clear cache for cube '{self.cube_name}'. Status: '{status}'")
class OriginalOrderExecutor(OptipyzerExecutor):
def __init__(self, tm1: TM1Service, cube_name: str, view_names: List[str], process_name: str, dimensions: List[str],
executions: int,
measure_dimension_only_numeric: bool, original_dimension_order: List[str]):
super().__init__(tm1, cube_name, view_names, process_name, dimensions, executions,
measure_dimension_only_numeric)
self.mode = ExecutionMode.ORIGINAL_ORDER
self.original_dimension_order = original_dimension_order
def execute(self, reset_counter=True):
# at initial execution ram must be retrieved
return [self._evaluate_permutation(
self.original_dimension_order,
retrieve_ram=True,
reset_counter=reset_counter)]
class MainExecutor(OptipyzerExecutor):
def __init__(self, tm1: TM1Service, cube_name: str, view_names: List[str], process_name: str, dimensions: List[str],
executions: int, measure_dimension_only_numeric: bool, fast: bool = False):
super().__init__(tm1, cube_name, view_names, process_name, dimensions, executions,
measure_dimension_only_numeric)
self.mode = ExecutionMode.ITERATIONS
self.fast = fast
if len(view_names) > 1:
logging.warning("BestExecutor mode will use first view and ignore other views: " + str(view_names[1:]))
self.view_name = view_names[0]
def execute(self) -> List[PermutationResult]:
dimensions = self.dimensions[:]
resulting_order = self.dimensions[:]
permutation_results = []
dimension_pool = self.dimensions[:]
mid = int(len(dimension_pool) / 2)
if not self.measure_dimension_only_numeric:
dimension_pool.remove(self.dimensions[-1])
dimensions.remove(self.dimensions[-1])
# iteration through positions like: n, 0, n-1, 1, n-2, 2, ...
for iteration, position in enumerate(chain(*zip(reversed(range(len(dimensions))), range(len(dimensions))))):
if self.fast and iteration == 2:
break
if position == mid:
break
results_per_dimension = list()
for dimension in dimension_pool:
original_position = resulting_order.index(dimension)
permutation = list(resulting_order)
permutation = swap(permutation, position, original_position)
permutation_result = self._evaluate_permutation(permutation)
permutation_results.append(permutation_result)
results_per_dimension.append(permutation_result)
if position > mid:
best_order = sorted(
results_per_dimension,
key=lambda r: r.ram_usage)[0]
else:
best_order = sorted(
results_per_dimension,
key=lambda r: r.median_query_time(self.view_name))[0]
resulting_order = list(best_order.dimension_order)
dimension_pool.remove(resulting_order[position])
return permutation_results