Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit 15430fa

Browse files
tisutisupmacik
authored andcommitted
* Adding support to upload performance kpi data to opensearch (redhat-developer#1331) * Fixed the environment issue in CI (redhat-developer#1335) * Added metadata with performance kpi data upload (redhat-developer#1363) Signed-off-by: Pavel Macík <[email protected]>
1 parent b82c512 commit 15430fa

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

make/performance.mk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ test-performance-thresholds: yq
4949
@[ $$($(YQ) eval '(.kpi[] | select(.name == "usage").metrics.[] | select(.name == "CPU_millicores").average) < $(TEST_PERFORMANCE_AVG_CPU)' $(TEST_PERFORMANCE_OUTPUT_DIR)/results/kpi.yaml) == "true" ]
5050
@echo "Checking if maximal value of CPU "$$($(YQ) eval '(.kpi[] | select(.name == "usage").metrics.[] | select(.name == "CPU_millicores").maximum)' $(TEST_PERFORMANCE_OUTPUT_DIR)/results/kpi.yaml)" < $(TEST_PERFORMANCE_MAX_CPU) milicores of vCPU"
5151
@[ $$($(YQ) eval '(.kpi[] | select(.name == "usage").metrics.[] | select(.name == "CPU_millicores").maximum) < $(TEST_PERFORMANCE_MAX_CPU)' $(TEST_PERFORMANCE_OUTPUT_DIR)/results/kpi.yaml) == "true" ]
52+
53+
.PHONY: test-performance-upload-kpi
54+
## Upload KPI data to the open search instance
55+
test-performance-upload-kpi: setup-venv
56+
@echo Uploading kpi to the open search instance
57+
$(Q)$(PYTHON_VENV_DIR)/bin/pip install -q -r ./test/performance/requirements.txt
58+
OS_HOST=$(OPENSEARCH_HOST) OS_REGION=$(OPENSEARCH_REGION) KPI_YAML_FILE=$(TEST_PERFORMANCE_OUTPUT_DIR)/results/kpi.yaml TEST_PERFORMANCE_AVG_MEMORY=$(TEST_PERFORMANCE_AVG_MEMORY) TEST_PERFORMANCE_MAX_MEMORY=$(TEST_PERFORMANCE_MAX_MEMORY) TEST_PERFORMANCE_AVG_CPU=$(TEST_PERFORMANCE_AVG_CPU) TEST_PERFORMANCE_MAX_CPU=$(TEST_PERFORMANCE_MAX_CPU) $(PYTHON_VENV_DIR)/bin/python3 ./test/performance/upload_data.py
59+

test/performance/__init__.py

Whitespace-only changes.

test/performance/collect-kpi.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ NS_PREFIX=${NS_PREFIX:-entanglement}
1616

1717
SBO_METRICS=$(find $METRICS -type f -name 'pod-info.service-binding-operator-*.csv')
1818

19+
SBO_VERSION=$(make operator-version --no-print-directory)
20+
OPENSHIFT_RELEASE=$(oc version -o yaml | yq e '.openshiftVersion')
21+
OPENSHIFT_VERSION=$(oc version -o yaml | yq e '.openshiftVersion' | grep -oP '^\d{1,2}.\d{1,2}.\d{1,2}')
22+
RUN_TYPE=${RUN_TYPE:-default}
23+
PULL_NUMBER=${PULL_NUMBER:-n/a}
24+
PULL_PULL_SHA=${PULL_PULL_SHA:-n/a}
25+
1926
SCENARIOS="nosb-inv nosb-val sb-inc sb-inv sb-val"
2027
#SCENARIOS="nosb-val"
2128

@@ -52,3 +59,11 @@ for scenario in $SCENARIOS; do
5259
cat $output >>$kpi_yaml
5360
done
5461
done
62+
63+
echo "execution_timestamp: $(date +%F\ %T)" >>$kpi_yaml
64+
echo "sbo_version: $SBO_VERSION" >>$kpi_yaml
65+
echo "openshift_version: $OPENSHIFT_VERSION" >>$kpi_yaml
66+
echo "openshift_release: $OPENSHIFT_RELEASE" >>$kpi_yaml
67+
echo "run_type: $RUN_TYPE" >>$kpi_yaml
68+
echo "pull_number: $PULL_NUMBER" >> $kpi_yaml
69+
echo "commit_id: ${PULL_PULL_SHA}" >> $kpi_yaml

test/performance/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
boto3==1.23.10
2+
botocore==1.26.10
3+
requests==2.27.1
4+
opensearch-py==2.0.1
5+
PyYAML==6.0

test/performance/upload_data.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
"""
2+
Python Module to interact with aws opensearch instance
3+
"""
4+
5+
import os
6+
import string
7+
import random
8+
from datetime import datetime
9+
10+
import yaml
11+
import boto3
12+
from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth
13+
14+
15+
def create_index(os_client, index_name, number_of_shards=4):
16+
""" Method to create index in opensearch instance """
17+
index_body = {
18+
'settings': {
19+
'index': {
20+
'number_of_shards': number_of_shards
21+
}
22+
}
23+
}
24+
response = os_client.indices.create(index_name, body=index_body)
25+
print('\nCreating index:')
26+
print(response)
27+
28+
29+
def delete_index(os_client, index_name):
30+
""" Method to delete index from opensearch instance """
31+
response = os_client.indices.delete(
32+
index=index_name
33+
)
34+
print('\nDeleting index:')
35+
print(response)
36+
37+
38+
def add_document_to_index(os_client, index_name, doc_id, document):
39+
""" Add a document to the index """
40+
response = os_client.index(
41+
index=index_name,
42+
body=document,
43+
id=doc_id,
44+
refresh=True
45+
)
46+
print('\nAdding document:')
47+
print(response)
48+
49+
50+
def delete_a_document(os_client, index_name, doc_id):
51+
""" Delete a document from index """
52+
response = os_client.delete(
53+
index=index_name,
54+
id=doc_id
55+
)
56+
print('\nDeleting document:')
57+
print(response)
58+
59+
60+
def search_document(os_client, index_name):
61+
""" Sample search for the document """
62+
qval = 'miller'
63+
query = {
64+
'size': 5,
65+
'query': {
66+
'multi_match': {
67+
'query': qval,
68+
'fields': ['title^2', 'director']
69+
}
70+
}
71+
}
72+
response = os_client.search(
73+
body=query,
74+
index=index_name
75+
)
76+
print('\nSearch results:')
77+
print(response)
78+
79+
80+
def setup_os_client():
81+
""" Setup the open search client """
82+
host = os.environ['OS_HOST'] # cluster endpoint, for ex: my-domain.us-east-1.es.amazonaws.com
83+
region = os.environ['OS_REGION']
84+
credentials = boto3.Session().get_credentials()
85+
auth = AWSV4SignerAuth(credentials, region)
86+
87+
os_client = OpenSearch(
88+
hosts=[{'host': host, 'port': 443}],
89+
http_auth=auth,
90+
use_ssl=True,
91+
verify_certs=True,
92+
connection_class=RequestsHttpConnection
93+
)
94+
return os_client
95+
96+
97+
def read_metric_data(file_name):
98+
""" Read from the metrics file and construct the document """
99+
with open(file_name, encoding="utf-8") as file_d:
100+
content = yaml.load(file_d, Loader=yaml.FullLoader)
101+
102+
kpi_data = content['kpi']
103+
execution_timestamp = content['execution_timestamp']
104+
sbo_version = content['sbo_version']
105+
openshift_version = content['openshift_version']
106+
openshift_release = content['openshift_release']
107+
run_type = content['run_type']
108+
pull_number = content['pull_number']
109+
commit_id = content['commit_id']
110+
111+
for data in kpi_data:
112+
if data['name'] == 'usage':
113+
for metric in data['metrics']:
114+
if metric['name'] == 'Memory_MiB':
115+
memory_average = metric['average']
116+
memory_maximum = metric['maximum']
117+
elif metric['name'] == 'CPU_millicores':
118+
cpu_average = metric['average']
119+
cpu_maximum = metric['maximum']
120+
dt_string = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
121+
return {'upload_date': dt_string,
122+
'memory_average': float(memory_average),
123+
'memory_maximum': float(memory_maximum),
124+
'cpu_average': float(cpu_average),
125+
'cpu_maximum': float(cpu_maximum),
126+
'memory_average_threshold': get_average_threshold_mem(),
127+
'memory_maximum_threshold': get_maximum_threshold_mem(),
128+
'cpu_average_threshold': get_average_threshold_cpu(),
129+
'cpu_maximum_threshold': get_maximum_threshold_cpu(),
130+
'run_type': run_type,
131+
'pull_number': pull_number,
132+
'commit_id': commit_id,
133+
'execution_timestamp': execution_timestamp,
134+
'sbo_version': sbo_version,
135+
'openshift_version': openshift_version,
136+
'openshift_release': openshift_release}
137+
138+
139+
def generate_id():
140+
""" Generate a random id of length 6 """
141+
length = 6
142+
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length))
143+
144+
145+
def get_average_threshold_mem():
146+
""" returns current threshold value 150 """
147+
return float(os.environ['TEST_PERFORMANCE_AVG_MEMORY'])
148+
149+
150+
def get_maximum_threshold_mem():
151+
""" returns current threshold value 200 """
152+
return float(os.environ['TEST_PERFORMANCE_MAX_MEMORY'])
153+
154+
155+
def get_average_threshold_cpu():
156+
""" returns current thresholds value 20 """
157+
return float(os.environ['TEST_PERFORMANCE_AVG_CPU'])
158+
159+
160+
def get_maximum_threshold_cpu():
161+
""" returns current thresholds value 100 """
162+
return float(os.environ['TEST_PERFORMANCE_MAX_CPU'])
163+
164+
165+
if __name__ == '__main__':
166+
OS_INDEX_NAME = 'sbo-perf-data'
167+
client = setup_os_client()
168+
# create_index(client, index_name)
169+
# delete_index(client, index_name)
170+
# delete_a_document(client, index_name, id)
171+
172+
metric_file_name = os.environ['KPI_YAML_FILE']
173+
doc = read_metric_data(metric_file_name)
174+
# doc = {'upload_date': '2022-12-14 06:30:30',
175+
# 'memory_average' : 68.2,
176+
# 'memory_maximum': 98.2,
177+
# 'cpu_average': 10.5,
178+
# 'cpu_maximum': 90.2,
179+
# 'memory_average_threshold': 150,
180+
# 'memory_maximum_threshold': 200,
181+
# 'cpu_average_threshold': 20,
182+
# 'cpu_maximum_threshold': 100}
183+
RANDOM_DOC_ID = generate_id()
184+
print(f"Random Generated ID: {RANDOM_DOC_ID}")
185+
add_document_to_index(client, OS_INDEX_NAME, RANDOM_DOC_ID, doc)

0 commit comments

Comments
 (0)