def _generate_components(self, variables, hosts, filter_properties): num_hosts = len(hosts) num_instances = filter_properties.get('num_instances') instance_type = filter_properties.get('instance_type') or {} requested_ram = instance_type.get('memory_mb', 0) if 'memory_mb' not in instance_type: LOG.warn(_("No information about requested instances\' RAM size " "was found, default value (0) is used.")) var_matrix = variables.host_instance_matrix self.variables = [var_matrix[i][j] for i in range(num_hosts) for j in range(num_instances)] #self.variables = var_matrix if requested_ram == 0: coeff_matrix = [ [(-hosts[i].free_ram_mb) for j in range(num_instances)] for i in range(num_hosts)] else: # we use int approximation here to avoid scaling problems after # normalization, in the case that the free ram in all hosts are # of very small values coeff_matrix = [ [-int(hosts[i].free_ram_mb / requested_ram) + j for j in range(num_instances)] for i in range(num_hosts)] coeff_matrix = utils.normalize_cost_matrix(coeff_matrix) self.coefficients = [coeff_matrix[i][j] for i in range(num_hosts) for j in range(num_instances)]
def test_normalize_cost_matrix_first_column_all_zero(self): test_matrix = [ [0, 1, 2, 3], [0, -1, -2, -3], [0, 0.2, 0.4, 0.6]] expected_result = [ [0, 1, 2, 3], [0, -1, -2, -3], [0, 0.2, 0.4, 0.6]] result = utils.normalize_cost_matrix(test_matrix) round_values = lambda x: [round(v, 4) for v in x] expected_result = map(round_values, expected_result) result = map(round_values, result) self.assertEqual(expected_result, result)
def test_normalize_cost_matrix(self): test_matrix = [ [1, 2, 3, 4], [5, 7, 9, 10], [-2, -1, 0, 2]] expected_result = [ [0.2, 0.4, 0.6, 0.8], [1.0, 1.4, 1.8, 2.0], [-0.4, -0.2, 0.0, 0.4]] result = utils.normalize_cost_matrix(test_matrix) round_values = lambda x: [round(v, 4) for v in x] expected_result = map(round_values, expected_result) result = map(round_values, result) self.assertEqual(expected_result, result)
def _generate_components(self, variables, hosts, filter_properties): num_hosts = len(hosts) num_instances = filter_properties.get('num_instances') host_weights = [] numeric_values = [] for host in hosts: metric_sum = 0.0 for (name, ratio) in self.setting: metric = host.metrics.get(name, None) if metric: metric_sum += metric.value * ratio else: metric_sum = None break host_weights.append(metric_sum) if metric_sum: numeric_values.append(metric_sum) if numeric_values: minval = min(numeric_values) maxval = max(numeric_values) weight_of_unavailable = (minval + (maxval - minval) * CONF.metrics.weight_multiplier_of_unavailable) for i in range(num_hosts): if host_weights[i] is None: host_weights[i] = weight_of_unavailable else: host_weights = [0 for i in range(num_hosts)] var_matrix = variables.host_instance_matrix self.variables = [var_matrix[i][j] for i in range(num_hosts) for j in range(num_instances)] #self.variables = var_matrix coeff_matrix = [[(-host_weights[i]) for j in range(num_instances)] for i in range(num_hosts)] coeff_matrix = cost_utils.normalize_cost_matrix(coeff_matrix) self.coefficients = [coeff_matrix[i][j] for i in range(num_hosts) for j in range(num_instances)]
def test_normalize_cost_matrix_empty_input(self): test_matrix = [] expected_result = [] result = utils.normalize_cost_matrix(test_matrix) self.assertEqual(expected_result, result)