diff options
author | Benoit HERARD <benoit.herard@orange.com> | 2017-06-30 13:30:19 +0200 |
---|---|---|
committer | Jose Lausuch <jose.lausuch@ericsson.com> | 2017-07-01 08:28:19 +0000 |
commit | ff2e2227b28f18ebbc265eee6ee5a50cabacc075 (patch) | |
tree | 1814a224a05e7ff530d10ded81df8b42e098142f /functest/energy/energy.py | |
parent | 1b838b4bd75f21e45e8a40407b1e3279dd5935d2 (diff) |
Energy monitoring SDK update.
add get_current scenario in EnergyRecorder class
enable_recording decorator change:
If a recording session was existing at startup, restart it at the end,
else stop recording
Change-Id: I91524824acb60275e0329452b7d7df2346d7884f
Signed-off-by: Benoit HERARD <benoit.herard@orange.com>
Diffstat (limited to 'functest/energy/energy.py')
-rw-r--r-- | functest/energy/energy.py | 96 |
1 files changed, 87 insertions, 9 deletions
diff --git a/functest/energy/energy.py b/functest/energy/energy.py index a20c7992..71b71239 100644 --- a/functest/energy/energy.py +++ b/functest/energy/energy.py @@ -20,6 +20,8 @@ import functest.utils.functest_utils as ft_utils def enable_recording(method): """ + Record energy during method execution. + Decorator to record energy during "method" exection. param method: Method to suround with start and stop @@ -29,10 +31,21 @@ def enable_recording(method): attribute """ def wrapper(*args): - """Wrapper for decorator to handle method arguments.""" + """ + Record energy during method execution (implementation). + + Wrapper for decorator to handle method arguments. + """ + current_scenario = EnergyRecorder.get_current_scenario() EnergyRecorder.start(args[0].case_name) return_value = method(*args) - EnergyRecorder.stop() + if current_scenario is None: + EnergyRecorder.stop() + else: + EnergyRecorder.submit_scenario( + current_scenario["scenario"], + current_scenario["step"] + ) return return_value return wrapper @@ -47,7 +60,7 @@ class EnergyRecorder(object): energy_recorder_api = None # Default initial step - INITIAL_STEP = "starting" + INITIAL_STEP = "running" @staticmethod def load_config(): @@ -92,22 +105,24 @@ class EnergyRecorder(object): } @staticmethod - def start(scenario): + def submit_scenario(scenario, step): """ - Start a recording session for scenario. + Submit a complet scenario definition to Energy recorder API. - param scenario: Starting scenario + param scenario: Scenario name :type scenario: string + param step: Step name + :type step: string """ return_status = True try: - EnergyRecorder.logger.debug("Starting recording") + EnergyRecorder.logger.debug("Submitting scenario") # Ensure that connectyvity settings are loaded EnergyRecorder.load_config() # Create API payload payload = { - "step": EnergyRecorder.INITIAL_STEP, + "step": step, "scenario": scenario } # Call API to start energy recording @@ -120,10 +135,34 @@ class EnergyRecorder(object): } ) if response.status_code != 200: - log_msg = "Error while starting energy recording session\n{}" + log_msg = "Error while submitting scenario\n{}" log_msg = log_msg.format(response.text) EnergyRecorder.logger.info(log_msg) return_status = False + except Exception: # pylint: disable=broad-except + # Default exception handler to ensure that method + # is safe for caller + EnergyRecorder.logger.exception( + "Error while submitting scenarion to energy recorder API" + ) + return_status = False + return return_status + + @staticmethod + def start(scenario): + """ + Start a recording session for scenario. + + param scenario: Starting scenario + :type scenario: string + """ + return_status = True + try: + EnergyRecorder.logger.debug("Starting recording") + return_status = EnergyRecorder.submit_scenario( + scenario, + EnergyRecorder.INITIAL_STEP + ) except Exception: # pylint: disable=broad-except # Default exception handler to ensure that method @@ -201,3 +240,42 @@ class EnergyRecorder(object): ) return_status = False return return_status + + @staticmethod + def get_current_scenario(): + """Get current running scenario (if any, None else).""" + EnergyRecorder.logger.debug("Getting current scenario") + return_value = None + print "In get current" + try: + # Ensure that connectyvity settings are loaded + EnergyRecorder.load_config() + + # Call API get running scenario + response = requests.get( + EnergyRecorder.energy_recorder_api["uri"], + auth=EnergyRecorder.energy_recorder_api["auth"] + ) + if response.status_code == 200: + return_value = json.loads(response.text) + elif response.status_code == 404: + log_msg = "No current running scenario at {}" + log_msg = log_msg.format( + EnergyRecorder.energy_recorder_api["uri"]) + EnergyRecorder.logger.error(log_msg) + print log_msg + return_value = None + else: + log_msg = "Error while getting current scenario\n{}" + log_msg = log_msg.format(response.text) + EnergyRecorder.logger.error(log_msg) + print log_msg + return_value = None + except Exception: # pylint: disable=broad-except + # Default exception handler to ensure that method + # is safe for caller + EnergyRecorder.logger.exception( + "Error while getting current scenario from energy recorder API" + ) + return_value = None + return return_value |