diff --git a/tools/run_tests/performance/prometheus.py b/tools/run_tests/performance/prometheus.py index ade1aa31c6137..58f0aed28030e 100644 --- a/tools/run_tests/performance/prometheus.py +++ b/tools/run_tests/performance/prometheus.py @@ -14,21 +14,21 @@ # See the License for the specific language governing permissions and # limitations under the License. - # example usage: python3 prometheus.py --url=http://10.108.4.94:9090 # # --pod_type=driver --pod_type=clients --container_name=main # # --container_name=sidecar import argparse import json import logging -import requests import statistics +from typing import Any, Dict, List from dateutil import parser -from typing import Any, Dict, List +import requests class Prometheus: + def __init__( self, url: str, @@ -43,53 +43,50 @@ def _fetch_by_query(self, query: str) -> Any: """Fetches the given query with time range.""" resp = requests.get( self.url + "/api/v1/query_range", - {"query": query, "start": self.start, "end": self.end, "step": 5}, + { + "query": query, + "start": self.start, + "end": self.end, + "step": 5 + }, ) resp.raise_for_status() return resp.json() - def _fetch_cpu_for_pod( - self, container_matcher: str, pod_name: str - ) -> Dict[str, List[float]]: + def _fetch_cpu_for_pod(self, container_matcher: str, + pod_name: str) -> Dict[str, List[float]]: """Fetches the cpu data for each pod and construct the container name to cpu data list Dict.""" query = ( 'container_cpu_usage_seconds_total{job="kubernetes-cadvisor",pod="' - + pod_name - + '",container=' - + container_matcher - + "}" - ) + + pod_name + '",container=' + container_matcher + "}") logging.debug("running prometheus query for cpu:" + query) cpu_data = self._fetch_by_query(query) logging.debug("raw cpu data:" + str(cpu_data)) - cpu_container_name_to_data_list = get_data_list_from_timeseries(cpu_data) + cpu_container_name_to_data_list = get_data_list_from_timeseries( + cpu_data) return cpu_container_name_to_data_list - def _fetch_memory_for_pod( - self, container_matcher: str, pod_name: str - ) -> Dict[str, List[float]]: + def _fetch_memory_for_pod(self, container_matcher: str, + pod_name: str) -> Dict[str, List[float]]: """Fetches the memory data for each pod and construct the container name to memory data list Dict.""" query = ( - 'container_memory_usage_bytes{job="kubernetes-cadvisor",pod="' - + pod_name - + '",container=' - + container_matcher - + "}" - ) + 'container_memory_usage_bytes{job="kubernetes-cadvisor",pod="' + + pod_name + '",container=' + container_matcher + "}") logging.debug("running prometheus query for memory:" + query) memory_data = self._fetch_by_query(query) logging.debug("raw memory data:" + str(memory_data)) - memory_container_name_to_data_list = get_data_list_from_timeseries(memory_data) + memory_container_name_to_data_list = get_data_list_from_timeseries( + memory_data) return memory_container_name_to_data_list def fetch_cpu_and_memory_data( - self, container_list: List[str], pod_dict: Dict[str, List[str]] - ) -> Dict[str, Any]: + self, container_list: List[str], + pod_dict: Dict[str, List[str]]) -> Dict[str, Any]: """Fetches and process min, max, mean, std for the memory and cpu data for each container in the container_list for each pod in the pod_list and construct processed data group first by metric @@ -102,19 +99,15 @@ def fetch_cpu_and_memory_data( for pod in pod_names: container_data = {} for container, data in self._fetch_cpu_for_pod( - container_matcher, pod - ).items(): + container_matcher, pod).items(): container_data[container] = {} - container_data[container]["cpuSeconds"] = compute_total_cpu_seconds( - data - ) + container_data[container][ + "cpuSeconds"] = compute_total_cpu_seconds(data) for container, data in self._fetch_memory_for_pod( - container_matcher, pod - ).items(): + container_matcher, pod).items(): container_data[container][ - "memoryMean" - ] = compute_average_memory_usage(data) + "memoryMean"] = compute_average_memory_usage(data) pod_data[pod] = container_data processed_data[role] = pod_data @@ -145,7 +138,8 @@ def get_data_list_from_timeseries(data: Any) -> Dict[str, List[float]]: if data["status"] != "success": raise Exception("command failed: " + data["status"] + str(data)) if data["data"]["resultType"] != "matrix": - raise Exception("resultType is not matrix: " + data["data"]["resultType"]) + raise Exception("resultType is not matrix: " + + data["data"]["resultType"]) container_name_to_data_list = {} for res in data["data"]["result"]: @@ -170,9 +164,8 @@ def compute_average_memory_usage(memory_data_list: List[float]) -> float: return statistics.mean(memory_data_list) -def construct_pod_dict( - node_info_file: str, pod_types: List[str] -) -> Dict[str, List[str]]: +def construct_pod_dict(node_info_file: str, + pod_types: List[str]) -> Dict[str, List[str]]: """Constructs a dict of pod names keyed by role to be queried.""" with open(node_info_file, "r") as f: pod_names = json.load(f) @@ -200,8 +193,7 @@ def convert_UTC_to_epoch(utc_timestamp: str) -> str: def main() -> None: argp = argparse.ArgumentParser( - description="Fetch cpu and memory stats from prometheus" - ) + description="Fetch cpu and memory stats from prometheus") argp.add_argument("--url", help="Prometheus base url", required=True) argp.add_argument( "--scenario_result_file", @@ -217,7 +209,8 @@ def main() -> None: argp.add_argument( "--pod_type", action="append", - help="Pod type to query the metrics for, the options are driver, client and server", + help= + "Pod type to query the metrics for, the options are driver, client and server", choices=["driver", "clients", "servers"], required=True, ) @@ -253,8 +246,7 @@ def main() -> None: pod_dict = construct_pod_dict(args.node_info_file, args.pod_type) processed_data = p.fetch_cpu_and_memory_data( - container_list=args.container_name, pod_dict=pod_dict - ) + container_list=args.container_name, pod_dict=pod_dict) logging.debug(json.dumps(processed_data, sort_keys=True, indent=4))