def run_scenario(settings, params, curr_exp, n_exp): """Run a single scenario experiment Parameters ---------- settings : Settings The simulator settings params : dict Dictionary of parameters curr_exp : int sequence number of the experiment n_exp : int Number of scheduled experiments Returns ------- results : 3-tuple A (params, results, duration) 3-tuple. The first element is a dictionary which stores all the attributes of the experiment. The second element is a dictionary which stores the results. The third element is an integer expressing the wall-clock duration of the experiment (in seconds) """ try: start_time = time.time() proc_name = mp.current_process().name logger = logging.getLogger('runner-%s' % proc_name) alpha = params['alpha'] topology_name = params['topology_name'] network_cache = params['network_cache'] strategy_name = params['strategy_name'] n_contents = params['n_contents'] strategy_params = params['strategy_params'] cache_policy = params['cache_policy'] if 'cache_policy' in params \ and params['cache_policy'] is not None \ else settings.CACHE_POLICY metrics = settings.DATA_COLLECTORS scenario = "%s, %s, alpha: %s, netcache: %s" % (topology_name, strategy_name, str(alpha), str(network_cache)) logger.info('Experiment %d/%d | Preparing scenario: %s', curr_exp, n_exp, scenario) # Check parameters if topology_name not in topology_factory_register: logger.error('No topology factory implementation for %s was found.' % topology_name) return None if cache_policy not in cache_policy_register: logger.error('No implementation of cache policy %s was found.' % cache_policy) return None if strategy_name not in strategy_register: logger.error('No implementation of strategy %s was found.' % strategy_name) return None if any(m not in data_collector_register for m in metrics): logger.error('There are no implementations for at least one data collector specified') return None # Get user-defined seed, if any seed = settings.SEED if 'SEED' in settings else None # Get topology and event generator topology = topology_factory_register[topology_name](network_cache, n_contents, seed=seed) events = uniform_req_gen(topology, n_contents, alpha, rate=settings.NETWORK_REQUEST_RATE, n_warmup=settings.N_WARMUP_REQUESTS, n_measured=settings.N_MEASURED_REQUESTS, seed=seed) topology.graph['cache_policy'] = cache_policy collectors = [(m, {}) for m in metrics] strategy = (strategy_name, strategy_params) logger.info('Experiment %d/%d | Start simulation', curr_exp, n_exp) results = exec_experiment(topology, events, strategy, collectors) duration = time.time() - start_time logger.info('Experiment %d/%d | End simulation | Duration %s.', curr_exp, n_exp, timestr(duration, True)) return (params, results, duration) except KeyboardInterrupt: logger.error('Received keyboard interrupt. Terminating') sys.exit(-signal.SIGINT) except Exception as e: err_type = str(type(e)).split("'")[1].split(".")[1] err_message = e.message logger.error('Experiment %d/%d | Failed | %s: %s\n%s', curr_exp, n_exp, err_type, err_message, traceback.format_exc())
def run_scenario(settings, params, curr_exp, n_exp): """Run a single scenario experiment Parameters ---------- settings : Settings The simulator settings params : dict Dictionary of parameters curr_exp : int sequence number of the experiment n_exp : int Number of scheduled experiments Returns ------- results : 2-tuple A 2-tuple of dictionaries. The first dict stores all the attributes of the experiment. The second dict stores the results. """ try: start_time = time.time() proc_name = mp.current_process().name logger = logging.getLogger('runner-%s' % proc_name) alpha = params['alpha'] topology_name = params['topology_name'] network_cache = params['network_cache'] strategy_name = params['strategy_name'] n_contents = params['n_contents'] strategy_params = params['strategy_params'] cache_policy = params['cache_policy'] if 'cache_policy' in params \ and params['cache_policy'] is not None \ else settings.CACHE_POLICY metrics = settings.DATA_COLLECTORS ## MT add log_dir = settings.LOG_DIR scenario_id = 'T=%s-C=%s-A=%s-S=%s-Run=%s' % (topology_name, str(network_cache), str(alpha), strategy_name, str(n_exp)) scenario = "%s, %s, alpha: %s, netcache: %s" % (topology_name, strategy_name, str(alpha), str(network_cache)) logger.info('Experiment %d/%d | Preparing scenario: %s', curr_exp, n_exp, scenario) # Check parameters if topology_name not in topology_factory_register: logger.error('No topology factory implementation for %s was found.' % topology_name) return None if cache_policy not in cache_policy_register: logger.error('No implementation of cache policy %s was found.' % cache_policy) return None if strategy_name not in strategy_register: logger.error('No implementation of strategy %s was found.' % strategy_name) return None if any(m not in data_collector_register for m in metrics): logger.error('There are no implementations for at least one data collector specified') return None # Get topology and event generator topology = topology_factory_register[topology_name](network_cache, n_contents) events = uniform_req_gen(topology, n_contents, alpha, rate=settings.NETWORK_REQUEST_RATE, n_warmup=settings.N_WARMUP_REQUESTS, n_measured=settings.N_MEASURED_REQUESTS) topology.graph['cache_policy'] = cache_policy collectors = [(m, {}) for m in metrics] strategy = (strategy_name, strategy_params) logger.info('Experiment %d/%d | Start simulation', curr_exp, n_exp) results = exec_experiment(topology, events, strategy, collectors) duration = time.time() - start_time logger.info('Experiment %d/%d | End simulation | Duration %s.', curr_exp, n_exp, timestr(duration, True)) return (params, results, curr_exp, duration) except KeyboardInterrupt: logger.error('Received keyboard interrupt. Terminating') sys.exit(-signal.SIGINT)