Skip to content

Commit

Permalink
Merge pull request #836 from msvinaykumar/quicktest2
Browse files Browse the repository at this point in the history
Test script  for updateRecommendation API
  • Loading branch information
dinogun authored Aug 22, 2023
2 parents 67f3e7f + 8b7f02c commit cc988f5
Show file tree
Hide file tree
Showing 11 changed files with 1,532 additions and 789 deletions.
625 changes: 288 additions & 337 deletions tests/scripts/quickTestScalability.py

Large diffs are not rendered by default.

106 changes: 75 additions & 31 deletions tests/scripts/remote_monitoring_tests/helpers/kruize.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,70 +14,90 @@
limitations under the License.
"""

import json
import subprocess

import requests
import json
import os
import time

def form_kruize_url(cluster_type, SERVER_IP = None):

def form_kruize_url(cluster_type, SERVER_IP=None):
global URL

if SERVER_IP != None:
URL = "http://" + str(SERVER_IP)
print ("\nKRUIZE AUTOTUNE URL = ", URL)
print("\nKRUIZE AUTOTUNE URL = ", URL)
return

if (cluster_type == "minikube"):
port = subprocess.run(['kubectl -n monitoring get svc kruize --no-headers -o=custom-columns=PORT:.spec.ports[*].nodePort'], shell=True, stdout=subprocess.PIPE)
port = subprocess.run(
['kubectl -n monitoring get svc kruize --no-headers -o=custom-columns=PORT:.spec.ports[*].nodePort'],
shell=True, stdout=subprocess.PIPE)

AUTOTUNE_PORT=port.stdout.decode('utf-8').strip('\n')
AUTOTUNE_PORT = port.stdout.decode('utf-8').strip('\n')

ip = subprocess.run(['minikube ip'], shell=True, stdout=subprocess.PIPE)
SERVER_IP=ip.stdout.decode('utf-8').strip('\n')
SERVER_IP = ip.stdout.decode('utf-8').strip('\n')
URL = "http://" + str(SERVER_IP) + ":" + str(AUTOTUNE_PORT)

elif (cluster_type == "openshift"):

subprocess.run(['oc expose svc/kruize -n openshift-tuning'], shell=True, stdout=subprocess.PIPE)
ip = subprocess.run(['oc status -n openshift-tuning | grep "kruize" | grep port | cut -d " " -f1 | cut -d "/" -f3'], shell=True, stdout=subprocess.PIPE)
SERVER_IP=ip.stdout.decode('utf-8').strip('\n')
ip = subprocess.run(
['oc status -n openshift-tuning | grep "kruize" | grep port | cut -d " " -f1 | cut -d "/" -f3'], shell=True,
stdout=subprocess.PIPE)
SERVER_IP = ip.stdout.decode('utf-8').strip('\n')
print("IP = ", SERVER_IP)
URL = "http://" + str(SERVER_IP)
print ("\nKRUIZE AUTOTUNE URL = ", URL)
print("\nKRUIZE AUTOTUNE URL = ", URL)


# Description: This function validates the input json and posts the experiment using createExperiment API to Kruize Autotune
# Input Parameters: experiment input json
def create_experiment(input_json_file, invalid_header = False):

def create_experiment(input_json_file, invalid_header=False):
json_file = open(input_json_file, "r")
input_json = json.loads(json_file.read())
print("\n************************************************************")
print(input_json)
pretty_json_str = json.dumps(input_json, indent=4)
print(pretty_json_str)
print("\n************************************************************")

# read the json
print("\nCreating the experiment...")

url = URL + "/createExperiment"
print("URL = ", url)

headers = {'content-type': 'application/xml'}
if invalid_header:
print("Invalid header")
response = requests.post(url, json=input_json, headers=headers)
else:
response = requests.post(url, json=input_json)

print(response)

print("Response status code = ", response.status_code)
try:
# Parse the response content as JSON into a Python dictionary
response_json = response.json()

# Check if the response_json is a valid JSON object or array
if isinstance(response_json, (dict, list)):
# Convert the response_json back to a JSON-formatted string with double quotes and pretty print it
pretty_response_json_str = json.dumps(response_json, indent=4)

# Print the JSON string
print(pretty_response_json_str)
else:
print("Invalid JSON format in the response.")
print(response.text) # Print the response text as-is
except json.JSONDecodeError:
print("Response content is not valid JSON.")
print(response.text) # Print the response text as-is
return response


# Description: This function validates the result json and posts the experiment results using updateResults API to Kruize Autotune
# Input Parameters: experiment input json
def update_results(result_json_file):

# read the json
json_file = open(result_json_file, "r")
result_json = json.loads(json_file.read())
Expand All @@ -94,9 +114,32 @@ def update_results(result_json_file):
print(response.text)
return response


# Description: This function generates recommendation for the given experiment_name , start time and end time .
def update_recommendations(experiment_name, startTime, endTime):
print("\n************************************************************")
print("\nUpdating the recommendation \n for %s for dates Start-time: %s and End-time: %s..." % (
experiment_name, startTime, endTime))
queryString = "?"
if experiment_name:
queryString = queryString + "&experiment_name=%s" % (experiment_name)
if endTime:
queryString = queryString + "&interval_end_time=%s" % (endTime)
if startTime:
queryString = queryString + "&interval_start_time=%s" % (startTime)

url = URL + "/updateRecommendations?%s" % (queryString)
print("URL = ", url)
response = requests.post(url, )
print("Response status code = ", response.status_code)
print(response.text)
print("\n************************************************************")
return response


# Description: This function obtains the recommendations from Kruize Autotune using listRecommendations API
# Input Parameters: experiment name, flag indicating latest result and monitoring end time
def list_recommendations(experiment_name = None, latest = None, monitoring_end_time = None):
def list_recommendations(experiment_name=None, latest=None, monitoring_end_time=None):
PARAMS = ""
print("\nListing the recommendations...")
url = URL + "/listRecommendations"
Expand All @@ -106,30 +149,30 @@ def list_recommendations(experiment_name = None, latest = None, monitoring_end_t
if latest == None and monitoring_end_time == None:
response = requests.get(url)
elif latest != None:
PARAMS = {'latest' : latest}
PARAMS = {'latest': latest}
elif monitoring_end_time != None:
PARAMS = {'monitoring_end_time' : monitoring_end_time}
PARAMS = {'monitoring_end_time': monitoring_end_time}
else:
if latest == None and monitoring_end_time == None:
PARAMS = {'experiment_name': experiment_name}
elif latest != None:
PARAMS = {'experiment_name': experiment_name, 'latest' : latest}
PARAMS = {'experiment_name': experiment_name, 'latest': latest}
elif monitoring_end_time != None:
PARAMS = {'experiment_name': experiment_name, 'monitoring_end_time' : monitoring_end_time}
PARAMS = {'experiment_name': experiment_name, 'monitoring_end_time': monitoring_end_time}

print("PARAMS = ", PARAMS)
response = requests.get(url = url, params = PARAMS)
response = requests.get(url=url, params=PARAMS)

print("Response status code = ", response.status_code)
print("\n************************************************************")
print(response.text)
print("\n************************************************************")
return response


# Description: This function deletes the experiment and posts the experiment using createExperiment API to Kruize Autotune
# Input Parameters: experiment input json
def delete_experiment(input_json_file, invalid_header = False):

def delete_experiment(input_json_file, invalid_header=False):
json_file = open(input_json_file, "r")
input_json = json.loads(json_file.read())

Expand All @@ -149,15 +192,15 @@ def delete_experiment(input_json_file, invalid_header = False):
response = requests.delete(url, json=delete_json, headers=headers)
else:
response = requests.delete(url, json=delete_json)

print(response)
print("Response status code = ", response.status_code)
return response


# Description: This function creates a performance profile using the Kruize createPerformanceProfile API
# Input Parameters: performance profile json
def create_performance_profile(perf_profile_json_file):

json_file = open(perf_profile_json_file, "r")
perf_profile_json = json.loads(json_file.read())

Expand All @@ -170,6 +213,7 @@ def create_performance_profile(perf_profile_json_file):
print(response.text)
return response


# Description: This function obtains the experiments from Kruize Autotune using listExperiments API
# Input Parameters: None
def list_experiments():
Expand All @@ -178,7 +222,7 @@ def list_experiments():
url = URL + "/listExperiments"
print("URL = ", url)

response = requests.get(url = url, params = PARAMS)
response = requests.get(url=url, params=PARAMS)

print("Response status code = ", response.status_code)
return response
Loading

0 comments on commit cc988f5

Please sign in to comment.