work 3 sla program

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

import random

import logging
import matplotlib.pyplot as plt
import pandas as pd

# Configure logging
logging.basicConfig(filename='resource_management_framework_multi_csp.log', level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')

# Embedded configuration
config = {
"csp_list": [
{
"name": "CSP1",
"num_requests": 10,
"ram_cost": 30,
"min_ram_size_mb": 1024,
"max_ram_size_mb": 2048,
"min_days": 15,
"max_days": 45,
"min_sla_percentage": 10,
"max_sla_percentage": 30,
"min_vms": 2,
"max_vms": 6,
"energy_cost_per_vm": 5
},
{
"name": "CSP2",
"num_requests": 10,
"ram_cost": 22,
"min_ram_size_mb": 512,
"max_ram_size_mb": 2048,
"min_days": 20,
"max_days": 50,
"min_sla_percentage": 15,
"max_sla_percentage": 25,
"min_vms": 2,
"max_vms": 8,
"energy_cost_per_vm": 3 # Reduced energy cost for CSP2
},
{
"name": "CSP3",
"num_requests": 10,
"ram_cost": 25,
"min_ram_size_mb": 512,
"max_ram_size_mb": 1024,
"min_days": 10,
"max_days": 30,
"min_sla_percentage": 5,
"max_sla_percentage": 20,
"min_vms": 1,
"max_vms": 5,
"energy_cost_per_vm": 6
},
{
"name": "CSP4",
"num_requests": 10,
"ram_cost": 40,
"min_ram_size_mb": 2048,
"max_ram_size_mb": 4096,
"min_days": 25,
"max_days": 70,
"min_sla_percentage": 20,
"max_sla_percentage": 35,
"min_vms": 3,
"max_vms": 12,
"energy_cost_per_vm": 7
},
{
"name": "CSP5",
"num_requests": 10,
"ram_cost": 28,
"min_ram_size_mb": 1024,
"max_ram_size_mb": 2048,
"min_days": 30,
"max_days": 60,
"min_sla_percentage": 10,
"max_sla_percentage": 30,
"min_vms": 1,
"max_vms": 6,
"energy_cost_per_vm": 4
},
{
"name": "CSP6",
"num_requests": 10,
"ram_cost": 32,
"min_ram_size_mb": 512,
"max_ram_size_mb": 1024,
"min_days": 15,
"max_days": 45,
"min_sla_percentage": 10,
"max_sla_percentage": 25,
"min_vms": 2,
"max_vms": 7,
"energy_cost_per_vm": 5
}
]
}

def calculate_vm_cost(req_ram_size_mb, ram_cost_per_mb, num_vms, num_days):


ram_size_mb = 512 # 1 RAM unit is 512 MB
ram_needed = req_ram_size_mb / ram_size_mb
total_ram_cost = num_days * ram_cost_per_mb * ram_needed
total_vm_cost = total_ram_cost * num_vms
return total_vm_cost

def calculate_penalty_cost(sla_percentage, total_vm_cost):


return total_vm_cost * (sla_percentage / 100)

def calculate_extended_service_days(penalty_cost, daily_vm_cost):


return penalty_cost / daily_vm_cost

def calculate_energy_consumption(allocated_vms, energy_cost_per_vm):


return allocated_vms * energy_cost_per_vm
def optimize_resource_allocation(csp, num_vms, requested_ram_mb):
allocated_ram_mb = min(csp['max_ram_size_mb'], requested_ram_mb)
allocated_vms = min(csp['max_vms'], num_vms)

energy_consumption = calculate_energy_consumption(allocated_vms, csp['energy_cost_per_vm'])


logging.info(f"Optimized Resource Allocation for CSP {csp['name']}: Allocated {allocated_vms} VMs
with {allocated_ram_mb} MB RAM and energy consumption {energy_consumption} units.")

return allocated_vms, allocated_ram_mb, energy_consumption

def negotiate_and_adjust(csp, request):


allocated_vms, allocated_ram_mb, energy_consumption = optimize_resource_allocation(csp,
request['num_vms'], request['req_ram_size_mb'])

total_vm_cost = calculate_vm_cost(request['req_ram_size_mb'], csp['ram_cost'], allocated_vms,


request['num_days'])

sla_percentage = random.uniform(csp['min_sla_percentage'], csp['max_sla_percentage'])


penalty_cost = calculate_penalty_cost(sla_percentage, total_vm_cost)

daily_vm_cost = total_vm_cost / (request['num_days'] * allocated_vms)


extended_days = calculate_extended_service_days(penalty_cost, daily_vm_cost)

request['extended_service_days'] = round(extended_days, 2)
request['penalty_cost'] = round(penalty_cost, 2)
request['allocated_vms'] = allocated_vms
request['allocated_ram_mb'] = allocated_ram_mb
request['energy_consumption'] = round(energy_consumption, 2)
request['total_vm_cost'] = round(total_vm_cost, 2)

return request

def evaluate_csp_efficiency(csp, request):


updated_request = negotiate_and_adjust(csp, request)
return {
'csp_name': csp['name'],
'total_vm_cost': updated_request['total_vm_cost'],
'penalty_cost': updated_request['penalty_cost'],
'extended_service_days': updated_request['extended_service_days'],
'allocated_vms': updated_request['allocated_vms'],
'allocated_ram_mb': updated_request['allocated_ram_mb'],
'energy_consumption': updated_request['energy_consumption'],
'energy_efficiency': updated_request['allocated_vms'] /
updated_request['energy_consumption'], # Higher is better
'total_cost': updated_request['total_vm_cost'] + updated_request['penalty_cost'] +
updated_request['energy_consumption']
}

def simulate_resource_management(config):
evaluations = []

# Define a sample request


sample_request = {
'req_ram_size_mb': random.randint(1024, 2048),
'num_vms': random.randint(2, 6),
'num_days': random.randint(15, 45),
'single_vm_cost': random.uniform(20, 50), # Assumed single VM cost for penalty calculation
'min_penalty_cost': random.uniform(10, 30) # Assumed minimum penalty cost
}

logging.info(f"Simulating request: {sample_request}")

for csp in config['csp_list']:


evaluation = evaluate_csp_efficiency(csp, sample_request)
evaluations.append(evaluation)
logging.info(f"Evaluation for CSP {csp['name']}: {evaluation}")

return evaluations

def display_results(results):
df = pd.DataFrame(results)

print("CSP Evaluation Results:")


print(df)

# Find the best CSP


best_csp = df.loc[df['total_cost'].idxmin()]

print(f"\nBest CSP is {best_csp['csp_name']} with the lowest total cost of $


{best_csp['total_cost']:.2f}.")
print(f"Reduced penalty cost of ${best_csp['penalty_cost']:.2f}.")
print(f"Reduced energy consumption of {best_csp['energy_consumption']:.2f} units.")
print(f"Increased energy efficiency of {best_csp['energy_efficiency']:.2f}.")

# Visualization
fig, ax1 = plt.subplots(figsize=(14, 10))

# Plot total cost and penalty cost on the primary y-axis


ax1.bar(df['csp_name'], df['total_cost'], color='skyblue', label='Total Cost')
ax1.bar(df['csp_name'], df['penalty_cost'], color='salmon', bottom=df['total_cost'], label='Penalty
Cost')
ax1.set_xlabel('CSP Name')
ax1.set_ylabel('Cost ($)')
ax1.set_title('CSP Evaluation: Total Cost, Penalty Cost, and Extended Service Days')
ax1.legend(loc='upper left')

# Create a secondary y-axis for extended service days


ax2 = ax1.twinx()
ax2.plot(df['csp_name'], df['extended_service_days'], color='green', marker='o', linestyle='-',
label='Extended Service Days')
ax2.set_ylabel('Extended Service Days (Days)')
ax2.legend(loc='upper right')

# Create a third y-axis for energy efficiency


ax3 = ax1.twinx()
ax3.spines['right'].set_position(('outward', 60))
ax3.plot(df['csp_name'], df['energy_efficiency'], color='orange', marker='x', linestyle='--',
label='Energy Efficiency')
ax3.set_ylabel('Energy Efficiency (VMs per Unit)')
ax3.legend(loc='upper center')

fig.tight_layout()
plt.savefig('csp_evaluation_plot.png', format='png', dpi=300, bbox_inches='tight')
plt.show()

# Main execution
if __name__ == "__main__":
# Run simulation
results = simulate_resource_management(config)

# Display results
display_results(results)

You might also like