2020-02-25 17:24:28 +08:00
|
|
|
import logging
|
2020-02-23 21:24:44 +08:00
|
|
|
from datetime import timedelta
|
2020-04-09 23:23:01 +08:00
|
|
|
from typing import Dict
|
2020-02-23 21:24:44 +08:00
|
|
|
|
|
|
|
from envs.monkey_zoo.blackbox.analyzers.analyzer import Analyzer
|
2020-10-07 15:32:33 +08:00
|
|
|
from envs.monkey_zoo.blackbox.tests.performance.performance_test_config import PerformanceTestConfig
|
2020-04-24 18:19:07 +08:00
|
|
|
|
2020-04-09 23:23:01 +08:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
2020-02-23 21:24:44 +08:00
|
|
|
|
|
|
|
|
2020-04-09 23:23:01 +08:00
|
|
|
class PerformanceAnalyzer(Analyzer):
|
2021-04-06 21:19:27 +08:00
|
|
|
def __init__(
|
|
|
|
self, performance_test_config: PerformanceTestConfig, endpoint_timings: Dict[str, timedelta]
|
|
|
|
):
|
2020-04-09 23:23:01 +08:00
|
|
|
self.performance_test_config = performance_test_config
|
|
|
|
self.endpoint_timings = endpoint_timings
|
2020-02-25 17:24:28 +08:00
|
|
|
|
2020-04-09 23:23:01 +08:00
|
|
|
def analyze_test_results(self):
|
2020-04-24 00:46:58 +08:00
|
|
|
# Calculate total time and check each endpoint
|
2020-04-09 23:23:01 +08:00
|
|
|
single_page_time_less_then_max = True
|
|
|
|
total_time = timedelta()
|
2020-04-24 00:46:58 +08:00
|
|
|
for endpoint, elapsed in self.endpoint_timings.items():
|
2020-04-09 23:23:01 +08:00
|
|
|
total_time += elapsed
|
|
|
|
if elapsed > self.performance_test_config.max_allowed_single_page_time:
|
|
|
|
single_page_time_less_then_max = False
|
2020-02-23 21:24:44 +08:00
|
|
|
|
2020-04-09 23:23:01 +08:00
|
|
|
total_time_less_then_max = total_time < self.performance_test_config.max_allowed_total_time
|
2020-02-23 21:24:44 +08:00
|
|
|
|
2020-04-24 21:41:00 +08:00
|
|
|
PerformanceAnalyzer.log_slowest_endpoints(self.endpoint_timings)
|
|
|
|
LOGGER.info(f"Total time is {str(total_time)}")
|
2020-02-23 21:24:44 +08:00
|
|
|
|
2020-04-09 23:23:01 +08:00
|
|
|
performance_is_good_enough = total_time_less_then_max and single_page_time_less_then_max
|
2020-02-25 17:24:28 +08:00
|
|
|
|
2020-04-09 23:23:01 +08:00
|
|
|
if self.performance_test_config.break_on_timeout and not performance_is_good_enough:
|
|
|
|
LOGGER.warning(
|
2021-04-07 16:11:09 +08:00
|
|
|
"Calling breakpoint - pausing to enable investigation of island. "
|
|
|
|
"Type 'c' to continue once you're done "
|
2020-04-09 23:23:01 +08:00
|
|
|
"investigating. Type 'p timings' and 'p total_time' to see performance information."
|
|
|
|
)
|
|
|
|
breakpoint()
|
2020-02-25 20:57:50 +08:00
|
|
|
|
2020-04-09 23:23:01 +08:00
|
|
|
return performance_is_good_enough
|
2020-04-24 21:41:00 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def log_slowest_endpoints(endpoint_timings, max_endpoints_to_display=100):
|
|
|
|
slow_endpoint_list = list(endpoint_timings.items())
|
|
|
|
slow_endpoint_list.sort(key=lambda x: x[1], reverse=True)
|
|
|
|
slow_endpoint_list = slow_endpoint_list[:max_endpoints_to_display]
|
|
|
|
for endpoint in slow_endpoint_list:
|
|
|
|
LOGGER.info(f"{endpoint[0]} took {str(endpoint[1])}")
|