diff --git a/locust/configuration.py b/locust/configuration.py index f16c5ec818..4550b86549 100644 --- a/locust/configuration.py +++ b/locust/configuration.py @@ -1,32 +1,57 @@ import os, json, logging logger = logging.getLogger(__name__) +config_path = '/tests/settings/config.json' def read_file(): + """ + Will read the file and return it as a string with tree view. + """ try: - with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + '/tests/settings/config.json', "r") as data_file: - datas = data_file.read() + with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + config_path, "r") as data_file: + data = data_file.read() data_file.close() - return datas except Exception as err: logger.info(err) - return "{}" + data = "{}" + return data -def write_file(stringJSON): +def write_file(string_json): + """ + The `string_json` will overwrite existing configuration. + If the previous configuration doesn't exist, then it will create the file. + """ + status, message = None, None try: - with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + '/tests/settings/config.json', "w") as data_file: - datas = data_file.write(stringJSON) - return True, 'JSON saved' + with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + config_path, "w") as data_file: + data_file.write(string_json) + status = True + message = 'Configuration has been saved' except Exception as err: logger.info(err) - return False, err + status = False + message = "Can't save the configuration :" + err + return status, message + +class ClientConfiguration: + """ + This class is a handler for data configuration with JSON data structure. + """ + + config_data = None + + def read_json(self): + """ + Will get the data of configuration as JSON. + It reads configuration file once. + """ + if self.config_data is None: + try: + with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + config_path, "r") as data_file: + self.config_data = json.load(data_file) + data_file.close() + except Exception as err: + logger.info(err) + self.config_data = json.load({}) + return self.config_data -def read_JSON(): - try: - with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + '/tests/settings/config.json', "r") as data_file: - datas = json.load(data_file) - data_file.close() - return datas - except Exception as err: - logger.info(err) - return "{}" diff --git a/locust/core.py b/locust/core.py index 6c66e68ea2..fecdfba869 100644 --- a/locust/core.py +++ b/locust/core.py @@ -12,7 +12,8 @@ import logging from .clients import HttpSession -from . import events, configuration +from . import events +from .configuration import ClientConfiguration from .exception import LocustError, InterruptTaskSet, RescheduleTask, RescheduleTaskImmediately, StopLocust @@ -223,6 +224,9 @@ class ForumPage(TaskSet): instantiated. Useful for nested TaskSet classes. """ + config = None + """Will refer to the ClientConfiguration class instance when the TaskSet has been instantiated""" + def __init__(self, parent): self._task_queue = [] self._time_start = time() @@ -235,7 +239,8 @@ def __init__(self, parent): raise LocustError("TaskSet should be called with Locust instance or TaskSet instance as first argument") self.parent = parent - + self.config = ClientConfiguration() + # if this class doesn't have a min_wait or max_wait defined, copy it from Locust if not self.min_wait: self.min_wait = self.locust.min_wait @@ -358,5 +363,5 @@ def configuration(self): """ Reference to configuration.py """ - return configuration.read_JSON() + return self.config.read_json() diff --git a/locust/web.py b/locust/web.py index 0f3c1dfc61..63606430a1 100644 --- a/locust/web.py +++ b/locust/web.py @@ -30,7 +30,6 @@ app.root_path = os.path.dirname(os.path.abspath(__file__)) _ramp = False greenlet_spawner = None -load_config="" @app.route('/') def index():