2020-02-25 17:24:28 +08:00
|
|
|
import logging
|
2020-02-23 21:24:44 +08:00
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from envs.monkey_zoo.blackbox.analyzers.analyzer import Analyzer
|
|
|
|
from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIslandClient
|
|
|
|
|
2020-02-25 18:19:57 +08:00
|
|
|
MAX_ALLOWED_SINGLE_PAGE_TIME = timedelta(seconds=2)
|
|
|
|
MAX_ALLOWED_TOTAL_TIME = timedelta(seconds=5)
|
2020-02-23 21:24:44 +08:00
|
|
|
|
2020-02-25 20:57:50 +08:00
|
|
|
REPORT_URLS = [
|
|
|
|
"api/report/security",
|
|
|
|
"api/attack/report",
|
|
|
|
"api/report/zero_trust/findings",
|
|
|
|
"api/report/zero_trust/principles",
|
|
|
|
"api/report/zero_trust/pillars"
|
|
|
|
]
|
|
|
|
|
2020-02-25 17:24:28 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-02-23 21:24:44 +08:00
|
|
|
|
|
|
|
class PerformanceAnalyzer(Analyzer):
|
|
|
|
|
2020-02-25 17:24:28 +08:00
|
|
|
def __init__(self, island_client: MonkeyIslandClient, break_if_took_too_long=False):
|
|
|
|
self.break_if_took_too_long = break_if_took_too_long
|
2020-02-23 21:24:44 +08:00
|
|
|
self.island_client = island_client
|
|
|
|
|
|
|
|
def analyze_test_results(self) -> bool:
|
2020-02-25 17:24:28 +08:00
|
|
|
if not self.island_client.is_all_monkeys_dead():
|
2020-02-25 20:57:50 +08:00
|
|
|
raise RuntimeError("Can't test report times since not all Monkeys have died.")
|
2020-02-25 17:24:28 +08:00
|
|
|
|
2020-03-16 20:31:13 +08:00
|
|
|
# Collect timings for all pages
|
2020-02-23 21:24:44 +08:00
|
|
|
self.island_client.clear_caches()
|
2020-02-25 20:57:50 +08:00
|
|
|
report_resource_to_response_time = {}
|
|
|
|
for url in REPORT_URLS:
|
|
|
|
report_resource_to_response_time[url] = self.island_client.get_elapsed_for_get_request(url)
|
|
|
|
|
2020-03-16 20:31:13 +08:00
|
|
|
# Calculate total time and check each page
|
2020-02-23 21:24:44 +08:00
|
|
|
single_page_time_less_then_max = True
|
2020-03-16 20:31:13 +08:00
|
|
|
total_time = timedelta()
|
|
|
|
for page, elapsed in report_resource_to_response_time.items():
|
2020-02-25 18:19:57 +08:00
|
|
|
logger.info(f"page {page} took {str(elapsed)}")
|
2020-02-23 21:24:44 +08:00
|
|
|
total_time += elapsed
|
|
|
|
if elapsed > MAX_ALLOWED_SINGLE_PAGE_TIME:
|
|
|
|
single_page_time_less_then_max = False
|
|
|
|
|
|
|
|
total_time_less_then_max = total_time < MAX_ALLOWED_TOTAL_TIME
|
|
|
|
|
2020-02-25 18:19:57 +08:00
|
|
|
logger.info(f"total time is {str(total_time)}")
|
|
|
|
|
|
|
|
performance_is_good_enough = total_time_less_then_max and single_page_time_less_then_max
|
2020-02-23 21:24:44 +08:00
|
|
|
|
2020-02-25 18:19:57 +08:00
|
|
|
if self.break_if_took_too_long and not performance_is_good_enough:
|
2020-02-25 17:24:28 +08:00
|
|
|
logger.warning(
|
|
|
|
"Calling breakpoint - pausing to enable investigation of island. Type 'c' to continue once you're done "
|
2020-02-25 18:19:57 +08:00
|
|
|
"investigating. Type 'p timings' and 'p total_time' to see performance information."
|
2020-02-25 17:24:28 +08:00
|
|
|
)
|
|
|
|
breakpoint()
|
|
|
|
|
2020-02-25 18:19:57 +08:00
|
|
|
return performance_is_good_enough
|