Commit 190bf4ce authored by Eduardo Falcão's avatar Eduardo Falcão
Browse files

created test for sorting hosts and vms

parent e3f7f60e
......@@ -74,7 +74,7 @@ public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
if(currentFragmentation.isEmpty()){
for(Host h : getDatacenter().getHostList()){
double hostFragmentation = getHostFragmentation(h);
double hostFragmentation = getFragmentation(h);
currentFragmentation.put(h, hostFragmentation);
previousFragmentation.put(h, hostFragmentation);
}
......@@ -95,7 +95,7 @@ public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
//update current fragmentation
for(Host h : getDatacenter().getHostList()){
double hostFragmentation = getHostFragmentation(h);
double hostFragmentation = getFragmentation(h);
currentFragmentation.put(h, hostFragmentation);
}
......@@ -133,7 +133,7 @@ public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
* @param list a list of Machines (vms or hosts)
*/
protected void sort(List<? extends Machine> list){
if(list.isEmpty())
if(list == null || list.isEmpty())
return;
Comparator<Machine> comparator = null;
......@@ -143,8 +143,8 @@ public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
public int compare(Machine m1, Machine m2) {
Vm vm1 = (Vm) m1;
Vm vm2 = (Vm) m2;
double fragmentationVm1 = getVmFragmentation(vm1);
double fragmentationVm2 = getVmFragmentation(vm2);
double fragmentationVm1 = getFragmentation(vm1);
double fragmentationVm2 = getFragmentation(vm2);
if(fragmentationVm1 == fragmentationVm2)
return 0;
else if(fragmentationVm1 < fragmentationVm2)
......@@ -160,8 +160,8 @@ public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
public int compare(Machine m1, Machine m2) {
Host h1 = (Host) m1;
Host h2 = (Host) m2;
double fragmentationH1 = getHostFragmentation(h1);
double fragmentationH2 = getHostFragmentation(h2);
double fragmentationH1 = getFragmentation(h1);
double fragmentationH2 = getFragmentation(h2);
if(fragmentationH1 == fragmentationH2)
return 0;
else if(fragmentationH1 < fragmentationH2)
......@@ -196,12 +196,14 @@ public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
return candidateVmsToMigrate;
}
protected double getVmFragmentation(Vm vm){
return vm.getIops().getAvailableResource();
}
protected double getHostFragmentation(Host h){
return h.getIops().getAvailableResource();
/**
* Returns the absolute fragmentation (amount of idle resources)
* of a machine, be it a VM or a Host.
* @param m the machine
* @return the fragmentation
*/
protected double getFragmentation(Machine m){
return m.getIops().getAvailableResource();
}
/**
......@@ -250,7 +252,7 @@ public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
* @param policyType type scenario of the host SlaExplotation
* @return a set of hosts
*/
protected List<Host> getHostsFromSlaExploitationPolicy(Datacenter dc, String policyType) {
private List<Host> getHostsFromSlaExploitationPolicy(Datacenter dc, String policyType) {
List<Host> hosts = new ArrayList<Host>();
for (Host host : dc.getHostList()) {
if (policyType.toUpperCase().equals(LOOSE) &&
......
......@@ -5,7 +5,6 @@ import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.NotImplementedException;
import org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicy;
import org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.datacenters.Datacenter;
......@@ -16,12 +15,9 @@ import org.cloudbus.cloudsim.provisioners.ResourceProvisionerSimple;
import org.cloudbus.cloudsim.resources.Bandwidth;
import org.cloudbus.cloudsim.resources.Iops;
import org.cloudbus.cloudsim.resources.Ram;
import org.cloudbus.cloudsim.resources.ResourceManageable;
import org.cloudbus.cloudsim.vms.Vm;
import org.cloudbus.cloudsim.vms.VmSimple;
import org.easymock.EasyMock;
import org.easymock.internal.matchers.InstanceOf;
import org.hamcrest.core.AnyOf;
import org.junit.Before;
import org.junit.Test;
......@@ -38,6 +34,8 @@ public class VmAllocationPolicyMigrationSlaTest {
public static final int MIGRATION_TIME = 60;
public static final double IMPRECISION = 0.0001;
private VmAllocationPolicyMigrationSla policy;
@Before
......@@ -45,123 +43,186 @@ public class VmAllocationPolicyMigrationSlaTest {
VmAllocationPolicy ap = new VmAllocationPolicySimple();
policy = new VmAllocationPolicyMigrationSla(ap, MIGRATION_TIME);
}
private Datacenter createMockedDatacenter(List<Host> hosts) {
Datacenter datacenter = EasyMock.createMock(Datacenter.class);
@Test
public void getVmFragmentationTest() {
Vm vm1 = createMockedVmWithIops(1, 0.1, 0.1, 0.1, 0.5, 900, 1000);
Vm vm2 = createMockedVmWithIops(2, 0.1, 0.1, 0.5, 0.5, 500, 1000);
Vm vm3 = createMockedVmWithIops(3, 0.1, 0.1, 1.0, 0.5, 0, 1000);
Vm vm4 = createMockedVmWithIops(4, 0.1, 0.1, 0.0, 0.5, 1000, 1000);
double vmFragmentation1 = policy.getVmFragmentation(vm1);
double vmFragmentation2 = policy.getVmFragmentation(vm2);
double vmFragmentation3 = policy.getVmFragmentation(vm3);
double vmFragmentation4 = policy.getVmFragmentation(vm4);
assertEquals(vm1.getIops().getCapacity() - (vm1.getIopsPercentUsage() * vm1.getIops().getCapacity()), vmFragmentation1, 1);
assertEquals(vm2.getIops().getCapacity() - (vm2.getIopsPercentUsage() * vm2.getIops().getCapacity()), vmFragmentation2, 1);
assertEquals(vm3.getIops().getCapacity() - (vm3.getIopsPercentUsage() * vm3.getIops().getCapacity()), vmFragmentation3, 1);
assertEquals(vm4.getIops().getCapacity() - (vm4.getIopsPercentUsage() * vm4.getIops().getCapacity()), vmFragmentation4, 1);
}
@Test
public void getHostFragmentation() {
Vm vm1 = createMockedVmWithIops(1, 0.1, 0.1, 0.1, 0.5, 900, 1000);
Vm vm2 = createMockedVmWithIops(2, 0.1, 0.1, 0.5, 0.5, 500, 1000);
Vm vm3 = createMockedVmWithIops(3, 0.1, 0.1, 1.0, 0.5, 0, 1000);
Vm vm4 = createMockedVmWithIops(4, 0.1, 0.1, 0.0, 0.5, 1000, 1000);
Host h1 = createMockedHostWithIops(1, 2, vm1, 900, 1000, 0.1, null);
Host h2 = createMockedHostWithIops(2, 2, vm2, 500, 1000, 0.5, null);
Host h3 = createMockedHostWithIops(3, 2, vm3, 0, 1000, 1.0, null);
Host h4 = createMockedHostWithIops(4, 2, vm4, 1000, 1000, 0.0, null);
double hostFragmentation1 = policy.getHostFragmentation(h1);
double hostFragmentation2 = policy.getHostFragmentation(h2);
double hostFragmentation3 = policy.getHostFragmentation(h3);
double hostFragmentation4 = policy.getHostFragmentation(h4);
assertEquals(h1.getIops().getCapacity() - (h1.getIops().getPercentUtilization() * h1.getIops().getCapacity()), hostFragmentation1, 1);
assertEquals(h2.getIops().getCapacity() - (h2.getIops().getPercentUtilization() * h2.getIops().getCapacity()), hostFragmentation2, 1);
assertEquals(h3.getIops().getCapacity() - (h3.getIops().getPercentUtilization() * h3.getIops().getCapacity()), hostFragmentation3, 1);
assertEquals(h4.getIops().getCapacity() - (h4.getIops().getPercentUtilization() * h4.getIops().getCapacity()), hostFragmentation4, 1);
}
private Vm createMockedVmWithIops(int id, double cpuUsage, double ramUsage,
double iopsUsage, double bwUsage,
long availableResource, long iopsCapacity) {
VmSimple vm = EasyMock.createMock(VmSimple.class);
Iops iops = EasyMock.createMock(Iops.class);
EasyMock.expect(vm.getId()).andReturn(id).anyTimes();
EasyMock.expect(vm.getIopsPercentUsage()).andReturn(iopsUsage).anyTimes();
EasyMock.expect(vm.getCurrentAllocatedIops()).andReturn((long) (HOST_IOPS / 2)).anyTimes();
EasyMock.expect(iops.getAvailableResource()).andReturn(availableResource).anyTimes();
EasyMock.expect(iops.getCapacity()).andReturn(iopsCapacity).anyTimes();
EasyMock.expect(vm.getIops()).andReturn(iops).anyTimes();
EasyMock.replay(iops);
EasyMock.replay(vm);
EasyMock.expect(datacenter.getHostList()).andReturn(hosts).anyTimes();
EasyMock.replay(datacenter);
return vm;
return datacenter;
}
private Host createMockedHostWithIops(int id, int pesNumber, Vm vm,
long availableResource, long iopsCapacity,
double iopsUsage, ResourceProvisioner provisioner) {
long availableResource, long iopsCapacity,
double iopsUsage, ResourceProvisioner provisioner) {
Host host = EasyMock.createMock(Host.class);
Iops iops = EasyMock.createMock(Iops.class);
EasyMock.expect(host.getId()).andReturn(id).anyTimes();
EasyMock.expect(host.getNumberOfPes()).andReturn((long) pesNumber).anyTimes();
EasyMock.expect(host.getMips()).andReturn((double) HOST_MIPS).anyTimes();
EasyMock.expect(host.getRam()).andReturn(new Ram(HOST_RAM)).anyTimes();
EasyMock.expect(iops.getAvailableResource()).andReturn(availableResource).anyTimes();
EasyMock.expect(iops.getCapacity()).andReturn(iopsCapacity).anyTimes();
EasyMock.expect(iops.getPercentUtilization()).andReturn(iopsUsage).anyTimes();
EasyMock.expect(host.getIops()).andReturn(iops).anyTimes();
EasyMock.expect(host.getBw()).andReturn(new Bandwidth(HOST_BW)).anyTimes();
EasyMock.expect(host.getNumberOfWorkingPes()).andReturn((long) 2).anyTimes();
EasyMock.expect(host.getIopsProvisioner()).andReturn(provisioner).anyTimes();
List<Vm> vms = new ArrayList<Vm>();
vms.add(vm);
EasyMock.expect(host.getVmList()).andReturn(vms);
EasyMock.replay(iops);
EasyMock.replay(host);
return host;
}
private Vm createMockedVmWithIops(int id, double cpuUsage, double ramUsage,
double iopsUsage, double bwUsage,
long availableResource, long iopsCapacity) {
private Datacenter createMockedDatacenter(List<Host> hosts) {
Datacenter datacenter = EasyMock.createMock(Datacenter.class);
VmSimple vm = EasyMock.createMock(VmSimple.class);
Iops iops = EasyMock.createMock(Iops.class);
EasyMock.expect(vm.getId()).andReturn(id).anyTimes();
EasyMock.expect(vm.getIopsPercentUsage()).andReturn(iopsUsage).anyTimes();
EasyMock.expect(vm.getCurrentAllocatedIops()).andReturn((long) (HOST_IOPS / 2)).anyTimes();
EasyMock.expect(iops.getAvailableResource()).andReturn(availableResource).anyTimes();
EasyMock.expect(iops.getCapacity()).andReturn(iopsCapacity).anyTimes();
EasyMock.expect(vm.getIops()).andReturn(iops).anyTimes();
EasyMock.replay(iops);
EasyMock.replay(vm);
return vm;
}
@Test
public void testSortVmDescending() {
Vm vm1 = createMockedVmWithIops(1, 0.0, 0.0, 0.0, 0.0, 900, 0); //available = 900
Vm vm2 = createMockedVmWithIops(2, 0.0, 0.0, 0.0, 0.0, 500, 0);
Vm vm3 = createMockedVmWithIops(3, 0.0, 0.0, 0.0, 0.0, 0, 0);
Vm vm4 = createMockedVmWithIops(4, 0.0, 0.0, 0.0, 0.0, 1000, 0);
List<Vm> vmsList = new ArrayList<Vm>();
vmsList.add(vm1);
vmsList.add(vm2);
vmsList.add(vm3);
vmsList.add(vm4);
assertEquals(vm1, vmsList.get(0));
assertEquals(vm2, vmsList.get(1));
assertEquals(vm3, vmsList.get(2));
assertEquals(vm4, vmsList.get(3));
policy.sort(vmsList);
assertEquals(vm4, vmsList.get(0));
assertEquals(vm1, vmsList.get(1));
assertEquals(vm2, vmsList.get(2));
assertEquals(vm3, vmsList.get(3));
}
@Test
public void testSortVmOrHostNull() {
List<Vm> vmsList = new ArrayList<Vm>();
policy.sort(vmsList);
vmsList = null;
policy.sort(vmsList);
List<Host> hostsList = new ArrayList<Host>();
policy.sort(hostsList);
hostsList = null;
policy.sort(hostsList);
}
@Test
public void testSortHostAscending() {
Host h1 = createMockedHostWithIops(1, 0, null, 500, 0, 0.0, null);
Host h2 = createMockedHostWithIops(2, 0, null, 200, 0, 0.0, null);
Host h3 = createMockedHostWithIops(3, 0, null, 1200, 0, 0.0, null);
Host h4 = createMockedHostWithIops(4, 0, null, 400, 0, 0.0, null);
List<Host> hostsList = new ArrayList<Host>();
hostsList.add(h1);
hostsList.add(h2);
hostsList.add(h3);
hostsList.add(h4);
assertEquals(h1, hostsList.get(0));
assertEquals(h2, hostsList.get(1));
assertEquals(h3, hostsList.get(2));
assertEquals(h4, hostsList.get(3));
policy.sort(hostsList);
assertEquals(h2, hostsList.get(0));
assertEquals(h4, hostsList.get(1));
assertEquals(h1, hostsList.get(2));
assertEquals(h3, hostsList.get(3));
}
EasyMock.expect(datacenter.getHostList()).andReturn(hosts).anyTimes();
EasyMock.replay(datacenter);
@Test
public void testGetVmFragmentation() {
Vm vm1 = createMockedVmWithIops(1, 0.1, 0.1, 0.1, 0.5, 900, 1000);
Vm vm2 = createMockedVmWithIops(2, 0.1, 0.1, 0.5, 0.5, 500, 1000);
Vm vm3 = createMockedVmWithIops(3, 0.1, 0.1, 1.0, 0.5, 0, 1000);
Vm vm4 = createMockedVmWithIops(4, 0.1, 0.1, 0.0, 0.5, 1000, 1000);
return datacenter;
double vmFragmentation1 = policy.getFragmentation(vm1);
double vmFragmentation2 = policy.getFragmentation(vm2);
double vmFragmentation3 = policy.getFragmentation(vm3);
double vmFragmentation4 = policy.getFragmentation(vm4);
assertEquals(vm1.getIops().getCapacity() - (vm1.getIopsPercentUsage() * vm1.getIops().getCapacity()), vmFragmentation1, IMPRECISION);
assertEquals(vm2.getIops().getCapacity() - (vm2.getIopsPercentUsage() * vm2.getIops().getCapacity()), vmFragmentation2, IMPRECISION);
assertEquals(vm3.getIops().getCapacity() - (vm3.getIopsPercentUsage() * vm3.getIops().getCapacity()), vmFragmentation3, IMPRECISION);
assertEquals(vm4.getIops().getCapacity() - (vm4.getIopsPercentUsage() * vm4.getIops().getCapacity()), vmFragmentation4, IMPRECISION);
}
@Test
public void testGetHostFragmentation() {
Vm vm1 = createMockedVmWithIops(1, 0.1, 0.1, 0.1, 0.5, 900, 1000);
Vm vm2 = createMockedVmWithIops(2, 0.1, 0.1, 0.5, 0.5, 500, 1000);
Vm vm3 = createMockedVmWithIops(3, 0.1, 0.1, 1.0, 0.5, 0, 1000);
Vm vm4 = createMockedVmWithIops(4, 0.1, 0.1, 0.0, 0.5, 1000, 1000);
Host h1 = createMockedHostWithIops(1, 2, vm1, 900, 1000, 0.1, null);
Host h2 = createMockedHostWithIops(2, 2, vm2, 500, 1000, 0.5, null);
Host h3 = createMockedHostWithIops(3, 2, vm3, 0, 1000, 1.0, null);
Host h4 = createMockedHostWithIops(4, 2, vm4, 1000, 1000, 0.0, null);
double hostFragmentation1 = policy.getFragmentation(h1);
double hostFragmentation2 = policy.getFragmentation(h2);
double hostFragmentation3 = policy.getFragmentation(h3);
double hostFragmentation4 = policy.getFragmentation(h4);
assertEquals(h1.getIops().getCapacity() - (h1.getIops().getPercentUtilization() * h1.getIops().getCapacity()), hostFragmentation1, IMPRECISION);
assertEquals(h2.getIops().getCapacity() - (h2.getIops().getPercentUtilization() * h2.getIops().getCapacity()), hostFragmentation2, IMPRECISION);
assertEquals(h3.getIops().getCapacity() - (h3.getIops().getPercentUtilization() * h3.getIops().getCapacity()), hostFragmentation3, IMPRECISION);
assertEquals(h4.getIops().getCapacity() - (h4.getIops().getPercentUtilization() * h4.getIops().getCapacity()), hostFragmentation4, IMPRECISION);
}
@Test
public void getHostsWithTightSla_test(){
public void testGetHostsWithTightAndWithLooseSla(){
List<Host> hosts = new ArrayList<Host>();
final int NUM_HEAVEN_HOSTS = 50;
for(int i = 0; i < NUM_HEAVEN_HOSTS; i++){
hosts.add(createMockedHostWithIops(i, 0, null, 0, 0, 0.0, new ResourceProvisionerSimple()));
System.out.println("#"+i);
}
final int NUM_HELL_HOSTS = 150;
for(int i = NUM_HEAVEN_HOSTS; i < NUM_HEAVEN_HOSTS+NUM_HELL_HOSTS; i++){
hosts.add(createMockedHostWithIops(i, 0, null, 0, 0, 0.0, new ResourceProvisionerShared()));
System.out.println("*"+i);
}
Datacenter dc = createMockedDatacenter(hosts);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment