Finished telemetry performance test (time measurements, refactoring)

This commit is contained in:
VakarisZ 2020-04-23 19:46:58 +03:00
parent 91d68113f9
commit 986e52716f
6 changed files with 75 additions and 35 deletions

View File

@ -14,11 +14,11 @@ class PerformanceAnalyzer(Analyzer):
self.endpoint_timings = endpoint_timings self.endpoint_timings = endpoint_timings
def analyze_test_results(self): def analyze_test_results(self):
# Calculate total time and check each page # Calculate total time and check each endpoint
single_page_time_less_then_max = True single_page_time_less_then_max = True
total_time = timedelta() total_time = timedelta()
for page, elapsed in self.endpoint_timings.items(): for endpoint, elapsed in self.endpoint_timings.items():
LOGGER.info(f"page {page} took {str(elapsed)}") LOGGER.info(f"Endpoint {endpoint} took {str(elapsed)}")
total_time += elapsed total_time += elapsed
if elapsed > self.performance_test_config.max_allowed_single_page_time: if elapsed > self.performance_test_config.max_allowed_single_page_time:
single_page_time_less_then_max = False single_page_time_less_then_max = False

View File

@ -1,8 +1,12 @@
from typing import Dict from typing import Dict
from datetime import timedelta
import requests import requests
import functools import functools
from envs.monkey_zoo.blackbox.island_client.supported_reuqest_method import SupportedRequestMethod
# SHA3-512 of '1234567890!@#$%^&*()_nothing_up_my_sleeve_1234567890!@#$%^&*()' # SHA3-512 of '1234567890!@#$%^&*()_nothing_up_my_sleeve_1234567890!@#$%^&*()'
import logging import logging
@ -16,6 +20,26 @@ class MonkeyIslandRequests(object):
def __init__(self, server_address): def __init__(self, server_address):
self.addr = "https://{IP}/".format(IP=server_address) self.addr = "https://{IP}/".format(IP=server_address)
self.token = self.try_get_jwt_from_server() self.token = self.try_get_jwt_from_server()
self.supported_request_methods = {SupportedRequestMethod.GET: self.get,
SupportedRequestMethod.POST: self.post,
SupportedRequestMethod.PATCH: self.patch,
SupportedRequestMethod.DELETE: self.delete}
def get_request_time(self, url, method: SupportedRequestMethod, data=None):
response = self.send_request_by_method(url, method, data)
if response.ok:
LOGGER.debug(f"Got ok for {url} content peek:\n{response.content[:120].strip()}")
return response.elapsed
else:
LOGGER.error(f"Trying to get {url} but got unexpected {str(response)}")
# instead of raising for status, mark failed responses as maxtime
return timedelta.max
def send_request_by_method(self, url, method=SupportedRequestMethod.GET, data=None):
if data:
return self.supported_request_methods[method](url, data)
else:
return self.supported_request_methods[method](url)
def try_get_jwt_from_server(self): def try_get_jwt_from_server(self):
try: try:
@ -64,9 +88,9 @@ class MonkeyIslandRequests(object):
verify=False) verify=False)
@_Decorators.refresh_jwt_token @_Decorators.refresh_jwt_token
def patch_json(self, url, data: Dict): def patch(self, url, data: Dict):
return requests.patch(self.addr + url, # noqa: DUO123 return requests.patch(self.addr + url, # noqa: DUO123
json=data, data=data,
headers=self.get_jwt_header(), headers=self.get_jwt_header(),
verify=False) verify=False)

View File

@ -0,0 +1,8 @@
from enum import Enum
class SupportedRequestMethod(Enum):
GET = "GET"
POST = "POST"
PATCH = "PATCH"
DELETE = "DELETE"

View File

@ -1,10 +1,10 @@
import logging import logging
from datetime import timedelta
from envs.monkey_zoo.blackbox.tests.basic_test import BasicTest from envs.monkey_zoo.blackbox.tests.basic_test import BasicTest
from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIslandClient from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIslandClient
from envs.monkey_zoo.blackbox.tests.performance.performance_test_config import PerformanceTestConfig from envs.monkey_zoo.blackbox.tests.performance.performance_test_config import PerformanceTestConfig
from envs.monkey_zoo.blackbox.analyzers.performance_analyzer import PerformanceAnalyzer from envs.monkey_zoo.blackbox.analyzers.performance_analyzer import PerformanceAnalyzer
from envs.monkey_zoo.blackbox.island_client.supported_reuqest_method import SupportedRequestMethod
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -25,18 +25,8 @@ class EndpointPerformanceTest(BasicTest):
self.island_client.clear_caches() self.island_client.clear_caches()
endpoint_timings = {} endpoint_timings = {}
for endpoint in self.test_config.endpoints_to_test: for endpoint in self.test_config.endpoints_to_test:
endpoint_timings[endpoint] = self.get_elapsed_for_get_request(endpoint) endpoint_timings[endpoint] = self.island_client.requests.get_request_time(endpoint,
SupportedRequestMethod.GET)
analyzer = PerformanceAnalyzer(self.test_config, endpoint_timings) analyzer = PerformanceAnalyzer(self.test_config, endpoint_timings)
return analyzer.analyze_test_results() return analyzer.analyze_test_results()
def get_elapsed_for_get_request(self, url):
response = self.island_client.requests.get(url)
if response.ok:
LOGGER.debug(f"Got ok for {url} content peek:\n{response.content[:120].strip()}")
return response.elapsed
else:
LOGGER.error(f"Trying to get {url} but got unexpected {str(response)}")
# instead of raising for status, mark failed responses as maxtime
return timedelta.max

View File

@ -5,7 +5,7 @@ from typing import List
class PerformanceTestConfig: class PerformanceTestConfig:
def __init__(self, max_allowed_single_page_time: timedelta, max_allowed_total_time: timedelta, def __init__(self, max_allowed_single_page_time: timedelta, max_allowed_total_time: timedelta,
endpoints_to_test: List[str], break_on_timeout=False): endpoints_to_test: List[str] = None, break_on_timeout=False):
self.max_allowed_single_page_time = max_allowed_single_page_time self.max_allowed_single_page_time = max_allowed_single_page_time
self.max_allowed_total_time = max_allowed_total_time self.max_allowed_total_time = max_allowed_total_time
self.endpoints_to_test = endpoints_to_test self.endpoints_to_test = endpoints_to_test

View File

@ -1,7 +1,17 @@
import logging
from datetime import timedelta
import json
from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIslandClient from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIslandClient
from envs.monkey_zoo.blackbox.tests.performance.utils.telem_parser import TelemParser from envs.monkey_zoo.blackbox.tests.performance.utils.telem_parser import TelemParser
from envs.monkey_zoo.blackbox.analyzers.performance_analyzer import PerformanceAnalyzer
from envs.monkey_zoo.blackbox.tests.performance.performance_test_config import PerformanceTestConfig
from envs.monkey_zoo.blackbox.island_client.supported_reuqest_method import SupportedRequestMethod
LOGGER = logging.getLogger(__name__)
MAX_ALLOWED_SINGLE_TELEM_PARSE_TIME = timedelta(seconds=2)
MAX_ALLOWED_TOTAL_TIME = timedelta(seconds=60)
class TelemetryPerformanceTest: class TelemetryPerformanceTest:
@ -10,24 +20,32 @@ class TelemetryPerformanceTest:
self.island_client = island_client self.island_client = island_client
def test_telemetry_performance(self): def test_telemetry_performance(self):
all_telemetries = TelemParser.get_all_telemetries() LOGGER.info("Starting telemetry performance test.")
try:
all_telemetries = TelemParser.get_all_telemetries()
except FileNotFoundError:
LOGGER.error("Telemetries to send not found. Refer to readme to figure out how to generate telemetries "
"and where to put them.")
return False
LOGGER.info("Telemetries imported successfully.")
all_telemetries.sort(key=lambda telem: telem['time']['$date']) all_telemetries.sort(key=lambda telem: telem['time']['$date'])
telemetry_parse_times = {}
for telemetry in all_telemetries: for telemetry in all_telemetries:
self.send_telemetry(telemetry) telemetry_endpoint = TelemetryPerformanceTest.get_verbose_telemetry_endpoint(telemetry)
telemetry_parse_times[telemetry_endpoint] = self.get_telemetry_time(telemetry)
test_config = PerformanceTestConfig(MAX_ALLOWED_SINGLE_TELEM_PARSE_TIME, MAX_ALLOWED_TOTAL_TIME)
PerformanceAnalyzer(test_config, telemetry_parse_times).analyze_test_results()
def send_telemetry(self, telemetry): def get_telemetry_time(self, telemetry):
content = telemetry['content'] content = telemetry['content']
url = telemetry['endpoint'] url = telemetry['endpoint']
method = telemetry['method'] method = SupportedRequestMethod.__getattr__(telemetry['method'])
if method == 'POST': return self.island_client.requests.get_request_time(url=url, method=method, data=content)
result = self.island_client.requests.post(url=url, data=content)
elif method == 'GET': @staticmethod
result = self.island_client.requests.get(url=url) def get_verbose_telemetry_endpoint(telemetry):
elif method == 'PATCH': telem_category = ""
result = self.island_client.requests.patch_json(url=url, data=content) if "telem_category" in telemetry['content']:
elif method == 'DELETE': telem_category = "_" + json.loads(telemetry['content'])['telem_category']
result = self.island_client.requests.delete(url=url) return telemetry['endpoint'] + telem_category
else:
raise Exception
return result