-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-test-templates.py
74 lines (56 loc) · 3.15 KB
/
create-test-templates.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
import time
from http import HTTPStatus
from org.metadatacenter.cedar.api.CedarAPI import CedarAPI
from settings import CEDAR_HOST, API_KEY, TARGET_FOLDER_ID
def create_resources(total_resources: int, batch_size: int = 20, max_retries: int = 3):
delay_between_retries = 0.5
cedar = CedarAPI(CEDAR_HOST, API_KEY)
cedar_template_api = cedar.get_template_api()
# cedar_element_api.set_debug(True)
cedar_template_api.load_artifact_from_file('./input-files/empty-template-unsaved.json')
start_time = time.time()
execution_times = []
formatted_start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time))
print(f"Start time: {formatted_start_time}\n")
for i in range(0, total_resources):
elapsed_time = time.time() - start_time
if i > 0:
estimated_total_time = elapsed_time * total_resources / i
remaining_time = estimated_total_time - elapsed_time
else:
remaining_time = 0
resource_start_time = time.time()
cedar_template_api.set_top_node_value('schema:name', f"Test Template {i}")
for attempt in range(max_retries):
create_response = cedar_template_api.create_template(folder_id=TARGET_FOLDER_ID)
if create_response.status_code == HTTPStatus.CREATED:
break
else:
print(f"Error on artifact {i}: {create_response.status_code}. Attempt {attempt + 1}/{max_retries}.")
if attempt < max_retries - 1:
time.sleep(delay_between_retries)
else:
print(f"Failed to create artifact {i} after {max_retries} attempts.")
resource_end_time = time.time()
execution_times.append(resource_end_time - resource_start_time)
if i % batch_size == 0 and i > 0:
print('-' * 80)
print(
f"Generated artifact {i}/{total_resources}. Estimated time left: {int(remaining_time // 60)} minutes {int(remaining_time % 60)} seconds.")
last_batch_average = sum(execution_times[-batch_size:]) / batch_size if execution_times else 0
overall_average = sum(execution_times) / len(execution_times) if execution_times else 0
print(f"\nAverage execution time for the last batch of {batch_size}: {last_batch_average:.2f} seconds.")
print(f"Overall average execution time: {overall_average:.2f} seconds.")
expected_end_time = time.time() + remaining_time
formatted_expected_end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(expected_end_time))
print(f"\nExpected end time after {i} artifacts: {formatted_expected_end_time}")
end_time = time.time()
formatted_end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time))
running_time = end_time - start_time
avg_time_per_resource = running_time / total_resources
print(f"\nStart time: {formatted_start_time}")
print(f"End time: {formatted_end_time}")
print(f"Total running time: {running_time:.2f} seconds")
print(f"Average time per artifact: {avg_time_per_resource:.2f} seconds")
if __name__ == "__main__":
create_resources(1000)