diff options
author | Jan Gelety <jgelety@cisco.com> | 2019-11-12 05:27:43 +0100 |
---|---|---|
committer | Jan Gelety <jgelety@cisco.com> | 2019-11-28 18:26:21 +0100 |
commit | d68951ac245150eeefa6e0f4156e4c1b5c9e9325 (patch) | |
tree | 487554a7547218d27f0a61ec02b70502c32cdcb4 /resources/tools/wrk/wrk_traffic_profile_parser.py | |
parent | ed0258a440cfad7023d643f717ab78ac568dc59b (diff) |
Python3: resources and libraries
Change-Id: I1392c06b1d64f62b141d24c0d42a8e36913b15e2
Signed-off-by: Jan Gelety <jgelety@cisco.com>
Diffstat (limited to 'resources/tools/wrk/wrk_traffic_profile_parser.py')
-rw-r--r-- | resources/tools/wrk/wrk_traffic_profile_parser.py | 150 |
1 files changed, 83 insertions, 67 deletions
diff --git a/resources/tools/wrk/wrk_traffic_profile_parser.py b/resources/tools/wrk/wrk_traffic_profile_parser.py index 1d40aa3d8a..1994b6195d 100644 --- a/resources/tools/wrk/wrk_traffic_profile_parser.py +++ b/resources/tools/wrk/wrk_traffic_profile_parser.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018 Cisco and / or its affiliates. +# Copyright (c) 2019 Cisco and / or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); you may not # use this file except in compliance with the License. You may obtain a copy # of the License at: @@ -26,22 +26,26 @@ from robot.api import logger from resources.tools.wrk.wrk_errors import WrkError -class WrkTrafficProfile(object): +class WrkTrafficProfile: """The wrk traffic profile. """ - MANDATORY_PARAMS = ("urls", - "first-cpu", - "cpus", - "duration", - "nr-of-threads", - "nr-of-connections") - - INTEGER_PARAMS = (("cpus", 1), - ("first-cpu", 0), - ("duration", 1), - ("nr-of-threads", 1), - ("nr-of-connections", 1)) + MANDATORY_PARAMS = ( + u"urls", + u"first-cpu", + u"cpus", + u"duration", + u"nr-of-threads", + u"nr-of-connections" + ) + + INTEGER_PARAMS = ( + (u"cpus", 1), + (u"first-cpu", 0), + (u"duration", 1), + (u"nr-of-threads", 1), + (u"nr-of-connections", 1) + ) def __init__(self, profile_name): """Read the traffic profile from the yaml file. @@ -57,29 +61,34 @@ class WrkTrafficProfile(object): self.profile_name = profile_name try: - with open(self.profile_name, 'r') as profile_file: + with open(self.profile_name, "r") as profile_file: self.traffic_profile = load(profile_file) except IOError as err: - raise WrkError(msg="An error occurred while opening the file '{0}'." - .format(self.profile_name), - details=str(err)) + raise WrkError( + msg=f"An error occurred while opening the file " + f"'{self.profile_name}'.", details=str(err) + ) except YAMLError as err: - raise WrkError(msg="An error occurred while parsing the traffic " - "profile '{0}'.".format(self.profile_name), - details=str(err)) + raise WrkError( + msg=f"An error occurred while parsing the traffic profile " + f"'{self.profile_name}'.", details=str(err) + ) self._validate_traffic_profile() if self.traffic_profile: - logger.debug("\nThe wrk traffic profile '{0}' is valid.\n". - format(self.profile_name)) - logger.debug("wrk traffic profile '{0}':".format(self.profile_name)) + logger.debug( + f"\nThe wrk traffic profile '{self.profile_name}' is valid.\n" + ) + logger.debug(f"wrk traffic profile '{self.profile_name}':") logger.debug(pformat(self.traffic_profile)) else: - logger.debug("\nThe wrk traffic profile '{0}' is invalid.\n". - format(self.profile_name)) - raise WrkError("\nThe wrk traffic profile '{0}' is invalid.\n". - format(self.profile_name)) + logger.debug( + f"\nThe wrk traffic profile '{self.profile_name}' is invalid.\n" + ) + raise WrkError( + f"\nThe wrk traffic profile '{self.profile_name}' is invalid.\n" + ) def __repr__(self): return pformat(self.traffic_profile) @@ -94,8 +103,9 @@ class WrkTrafficProfile(object): doc/wrk_lld.rst """ - logger.debug("\nValidating the wrk traffic profile '{0}'...\n". - format(self.profile_name)) + logger.debug( + f"\nValidating the wrk traffic profile '{self.profile_name}'...\n" + ) if not (self._validate_mandatory_structure() and self._validate_mandatory_values() and self._validate_optional_values() @@ -110,14 +120,14 @@ class WrkTrafficProfile(object): """ # Level 1: Check if the profile is a dictionary: if not isinstance(self.traffic_profile, dict): - logger.error("The wrk traffic profile must be a dictionary.") + logger.error(u"The wrk traffic profile must be a dictionary.") return False # Level 2: Check if all mandatory parameters are present: is_valid = True for param in self.MANDATORY_PARAMS: if self.traffic_profile.get(param, None) is None: - logger.error("The parameter '{0}' in mandatory.".format(param)) + logger.error(f"The parameter '{param}' in mandatory.") is_valid = False return is_valid @@ -129,8 +139,8 @@ class WrkTrafficProfile(object): """ # Level 3: Mandatory params: Check if urls is a list: is_valid = True - if not isinstance(self.traffic_profile["urls"], list): - logger.error("The parameter 'urls' must be a list.") + if not isinstance(self.traffic_profile[u"urls"], list): + logger.error(u"The parameter 'urls' must be a list.") is_valid = False # Level 3: Mandatory params: Check if integers are not below minimum @@ -147,60 +157,64 @@ class WrkTrafficProfile(object): """ is_valid = True # Level 4: Optional params: Check if script is present: - script = self.traffic_profile.get("script", None) + script = self.traffic_profile.get(u"script", None) if script is not None: if not isinstance(script, str): - logger.error("The path to LuaJIT script in invalid") + logger.error(u"The path to LuaJIT script in invalid") is_valid = False else: if not isfile(script): - logger.error("The file '{0}' does not exist.". - format(script)) + logger.error(f"The file '{script}' does not exist.") is_valid = False else: - self.traffic_profile["script"] = None - logger.debug("The optional parameter 'LuaJIT script' is not " - "defined. No problem.") + self.traffic_profile[u"script"] = None + logger.debug( + u"The optional parameter 'LuaJIT script' is not defined. " + u"No problem." + ) # Level 4: Optional params: Check if header is present: - header = self.traffic_profile.get("header", None) + header = self.traffic_profile.get(u"header", None) if header is not None: if isinstance(header, dict): - header = ", ".join("{0}: {1}".format(*item) - for item in header.items()) - self.traffic_profile["header"] = header + header = u", ".join( + f"{0}: {1}".format(*item) for item in header.items() + ) + self.traffic_profile[u"header"] = header elif not isinstance(header, str): - logger.error("The parameter 'header' type is not valid.") + logger.error(u"The parameter 'header' type is not valid.") is_valid = False if not header: - logger.error("The parameter 'header' is defined but " - "empty.") + logger.error(u"The parameter 'header' is defined but empty.") is_valid = False else: - self.traffic_profile["header"] = None - logger.debug("The optional parameter 'header' is not defined. " - "No problem.") + self.traffic_profile[u"header"] = None + logger.debug( + u"The optional parameter 'header' is not defined. No problem." + ) # Level 4: Optional params: Check if latency is present: - latency = self.traffic_profile.get("latency", None) + latency = self.traffic_profile.get(u"latency", None) if latency is not None: if not isinstance(latency, bool): - logger.error("The parameter 'latency' must be boolean.") + logger.error(u"The parameter 'latency' must be boolean.") is_valid = False else: - self.traffic_profile["latency"] = False - logger.debug("The optional parameter 'latency' is not defined. " - "No problem.") + self.traffic_profile[u"latency"] = False + logger.debug( + u"The optional parameter 'latency' is not defined. No problem." + ) # Level 4: Optional params: Check if timeout is present: - if 'timeout' in self.traffic_profile: - if not self._validate_int_param('timeout', 1): + if u"timeout" in self.traffic_profile: + if not self._validate_int_param(u"timeout", 1): is_valid = False else: - self.traffic_profile["timeout"] = None - logger.debug("The optional parameter 'timeout' is not defined. " - "No problem.") + self.traffic_profile[u"timeout"] = None + logger.debug( + u"The optional parameter 'timeout' is not defined. No problem." + ) return is_valid @@ -211,9 +225,10 @@ class WrkTrafficProfile(object): :rtype: bool """ # Level 5: Check urls and cpus: - if self.traffic_profile["cpus"] % len(self.traffic_profile["urls"]): - logger.error("The number of CPUs must be a multiple of the " - "number of URLs.") + if self.traffic_profile[u"cpus"] % len(self.traffic_profile[u"urls"]): + logger.error( + u"The number of CPUs must be a multiple of the number of URLs." + ) return False return True @@ -229,7 +244,7 @@ class WrkTrafficProfile(object): :rtype: bool """ value = self._traffic_profile[param] - if isinstance(value, (str, unicode)): + if isinstance(value, str): if value.isdigit(): value = int(value) else: @@ -237,8 +252,9 @@ class WrkTrafficProfile(object): if isinstance(value, int) and value >= minimum: self.traffic_profile[param] = value return True - logger.error("The parameter '{param}' must be an integer and " - "at least {minimum}".format(param=param, minimum=minimum)) + logger.error( + f"The parameter '{param}' must be an integer and at least {minimum}" + ) return False @property |