-
Notifications
You must be signed in to change notification settings - Fork 793
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #663 from guardicore/release/1.8.2
Release/1.8.2
- Loading branch information
Showing
201 changed files
with
14,066 additions
and
19,826 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
logs/ | ||
/blackbox/tests/performance/telem_sample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 23 additions & 34 deletions
57
envs/monkey_zoo/blackbox/analyzers/performance_analyzer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,48 @@ | ||
import logging | ||
from datetime import timedelta | ||
from typing import Dict | ||
|
||
from envs.monkey_zoo.blackbox.analyzers.analyzer import Analyzer | ||
from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIslandClient | ||
from envs.monkey_zoo.blackbox.tests.performance.performance_test_config import PerformanceTestConfig | ||
|
||
MAX_ALLOWED_SINGLE_PAGE_TIME = timedelta(seconds=2) | ||
MAX_ALLOWED_TOTAL_TIME = timedelta(seconds=5) | ||
|
||
REPORT_URLS = [ | ||
"api/report/security", | ||
"api/attack/report", | ||
"api/report/zero_trust/findings", | ||
"api/report/zero_trust/principles", | ||
"api/report/zero_trust/pillars" | ||
] | ||
|
||
logger = logging.getLogger(__name__) | ||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class PerformanceAnalyzer(Analyzer): | ||
|
||
def __init__(self, island_client: MonkeyIslandClient, break_if_took_too_long=False): | ||
self.break_if_took_too_long = break_if_took_too_long | ||
self.island_client = island_client | ||
|
||
def analyze_test_results(self) -> bool: | ||
if not self.island_client.is_all_monkeys_dead(): | ||
raise RuntimeError("Can't test report times since not all Monkeys have died.") | ||
def __init__(self, performance_test_config: PerformanceTestConfig, endpoint_timings: Dict[str, timedelta]): | ||
self.performance_test_config = performance_test_config | ||
self.endpoint_timings = endpoint_timings | ||
|
||
# Collect timings for all pages | ||
self.island_client.clear_caches() | ||
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) | ||
|
||
# Calculate total time and check each page | ||
def analyze_test_results(self): | ||
# Calculate total time and check each endpoint | ||
single_page_time_less_then_max = True | ||
total_time = timedelta() | ||
for page, elapsed in report_resource_to_response_time.items(): | ||
logger.info(f"page {page} took {str(elapsed)}") | ||
for endpoint, elapsed in self.endpoint_timings.items(): | ||
total_time += elapsed | ||
if elapsed > MAX_ALLOWED_SINGLE_PAGE_TIME: | ||
if elapsed > self.performance_test_config.max_allowed_single_page_time: | ||
single_page_time_less_then_max = False | ||
|
||
total_time_less_then_max = total_time < MAX_ALLOWED_TOTAL_TIME | ||
total_time_less_then_max = total_time < self.performance_test_config.max_allowed_total_time | ||
|
||
logger.info(f"total time is {str(total_time)}") | ||
PerformanceAnalyzer.log_slowest_endpoints(self.endpoint_timings) | ||
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 | ||
|
||
if self.break_if_took_too_long and not performance_is_good_enough: | ||
logger.warning( | ||
if self.performance_test_config.break_on_timeout and not performance_is_good_enough: | ||
LOGGER.warning( | ||
"Calling breakpoint - pausing to enable investigation of island. Type 'c' to continue once you're done " | ||
"investigating. Type 'p timings' and 'p total_time' to see performance information." | ||
) | ||
breakpoint() | ||
|
||
return performance_is_good_enough | ||
|
||
@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])}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
envs/monkey_zoo/blackbox/island_client/supported_request_method.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from enum import Enum | ||
|
||
|
||
class SupportedRequestMethod(Enum): | ||
GET = "GET" | ||
POST = "POST" | ||
PATCH = "PATCH" | ||
DELETE = "DELETE" |
Oops, something went wrong.