mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
fix long cast to double
- the result of dividing long with long resulted in loss of precision both for network and IO - unit tests included Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
This commit is contained in:
parent
ded8d6592b
commit
b2f06aae68
@ -4776,7 +4776,7 @@ ServerResource {
|
||||
Calendar _timestamp;
|
||||
}
|
||||
|
||||
private VmStatsEntry getVmStat(Connect conn, String vmName)
|
||||
VmStatsEntry getVmStat(Connect conn, String vmName)
|
||||
throws LibvirtException {
|
||||
Domain dm = null;
|
||||
try {
|
||||
@ -4821,10 +4821,10 @@ ServerResource {
|
||||
}
|
||||
|
||||
if (oldStats != null) {
|
||||
long deltarx = rx - oldStats._rx;
|
||||
double deltarx = rx - oldStats._rx;
|
||||
if (deltarx > 0)
|
||||
stats.setNetworkReadKBs(deltarx / 1024);
|
||||
long deltatx = tx - oldStats._tx;
|
||||
double deltatx = tx - oldStats._tx;
|
||||
if (deltatx > 0)
|
||||
stats.setNetworkWriteKBs(deltatx / 1024);
|
||||
}
|
||||
@ -4850,10 +4850,10 @@ ServerResource {
|
||||
long deltaiowr = io_wr - oldStats._io_wr;
|
||||
if (deltaiowr > 0)
|
||||
stats.setDiskWriteIOs(deltaiowr);
|
||||
long deltabytesrd = bytes_rd - oldStats._bytes_rd;
|
||||
double deltabytesrd = bytes_rd - oldStats._bytes_rd;
|
||||
if (deltabytesrd > 0)
|
||||
stats.setDiskReadKBs(deltabytesrd / 1024);
|
||||
long deltabyteswr = bytes_wr - oldStats._bytes_wr;
|
||||
double deltabyteswr = bytes_wr - oldStats._bytes_wr;
|
||||
if (deltabyteswr > 0)
|
||||
stats.setDiskWriteKBs(deltabyteswr / 1024);
|
||||
}
|
||||
|
||||
@ -19,21 +19,37 @@
|
||||
|
||||
package com.cloud.hypervisor.kvm.resource;
|
||||
|
||||
import com.cloud.agent.api.to.VirtualMachineTO;
|
||||
import com.cloud.template.VirtualMachineTemplate.BootloaderType;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.vm.VirtualMachine;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Test;
|
||||
import org.libvirt.Connect;
|
||||
import org.libvirt.Domain;
|
||||
import org.libvirt.DomainBlockStats;
|
||||
import org.libvirt.DomainInfo;
|
||||
import org.libvirt.DomainInterfaceStats;
|
||||
import org.libvirt.LibvirtException;
|
||||
import org.libvirt.NodeInfo;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import com.cloud.agent.api.VmStatsEntry;
|
||||
import com.cloud.agent.api.to.VirtualMachineTO;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef;
|
||||
import com.cloud.template.VirtualMachineTemplate.BootloaderType;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.vm.VirtualMachine;
|
||||
|
||||
public class LibvirtComputingResourceTest {
|
||||
|
||||
@ -210,4 +226,79 @@ public class LibvirtComputingResourceTest {
|
||||
uuid = lcr.getUuid(uuid);
|
||||
Assert.assertTrue(uuid.equals(oldUuid));
|
||||
}
|
||||
|
||||
private static final String VMNAME = "test";
|
||||
|
||||
@Test
|
||||
public void testGetVmStat() throws LibvirtException {
|
||||
Connect connect = Mockito.mock(Connect.class);
|
||||
Domain domain = Mockito.mock(Domain.class);
|
||||
DomainInfo domainInfo = new DomainInfo();
|
||||
Mockito.when(domain.getInfo()).thenReturn(domainInfo);
|
||||
Mockito.when(connect.domainLookupByName(VMNAME)).thenReturn(domain);
|
||||
NodeInfo nodeInfo = new NodeInfo();
|
||||
nodeInfo.cpus = 8;
|
||||
nodeInfo.memory = 8 * 1024 * 1024;
|
||||
nodeInfo.sockets = 2;
|
||||
nodeInfo.threads = 2;
|
||||
nodeInfo.model = "Foo processor";
|
||||
Mockito.when(connect.nodeInfo()).thenReturn(nodeInfo);
|
||||
// this is testing the interface stats, returns an increasing number of sent and received bytes
|
||||
Mockito.when(domain.interfaceStats(Mockito.anyString())).thenAnswer(new Answer<DomainInterfaceStats>() {
|
||||
// increment with less than a KB, so this should be less than 1 KB
|
||||
final static int increment = 1000;
|
||||
int rx_bytes = 1000;
|
||||
int tx_bytes = 1000;
|
||||
|
||||
@Override
|
||||
public DomainInterfaceStats answer(InvocationOnMock invocation) throws Throwable {
|
||||
DomainInterfaceStats domainInterfaceStats = new DomainInterfaceStats();
|
||||
domainInterfaceStats.rx_bytes = (this.rx_bytes += increment);
|
||||
domainInterfaceStats.tx_bytes = (this.tx_bytes += increment);
|
||||
return domainInterfaceStats;
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Mockito.when(domain.blockStats(Mockito.anyString())).thenAnswer(new Answer<DomainBlockStats>() {
|
||||
// a little less than a KB
|
||||
final static int increment = 1000;
|
||||
|
||||
int rd_bytes = 0;
|
||||
int wr_bytes = 1024;
|
||||
@Override
|
||||
public DomainBlockStats answer(InvocationOnMock invocation) throws Throwable {
|
||||
DomainBlockStats domainBlockStats = new DomainBlockStats();
|
||||
|
||||
domainBlockStats.rd_bytes = (rd_bytes += increment);
|
||||
domainBlockStats.wr_bytes = (wr_bytes += increment);
|
||||
return domainBlockStats;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource() {
|
||||
@Override
|
||||
protected List<InterfaceDef> getInterfaces(Connect conn, String vmName) {
|
||||
InterfaceDef interfaceDef = new InterfaceDef();
|
||||
return Arrays.asList(interfaceDef);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DiskDef> getDisks(Connect conn, String vmName) {
|
||||
DiskDef diskDef = new DiskDef();
|
||||
return Arrays.asList(diskDef);
|
||||
}
|
||||
|
||||
};
|
||||
libvirtComputingResource.getVmStat(connect, VMNAME);
|
||||
VmStatsEntry vmStat = libvirtComputingResource.getVmStat(connect, VMNAME);
|
||||
// network traffic as generated by the logic above, must be greater than zero
|
||||
Assert.assertTrue(vmStat.getNetworkReadKBs() > 0);
|
||||
Assert.assertTrue(vmStat.getNetworkWriteKBs() > 0);
|
||||
// IO traffic as generated by the logic above, must be greater than zero
|
||||
Assert.assertTrue(vmStat.getDiskReadKBs() > 0);
|
||||
Assert.assertTrue(vmStat.getDiskWriteKBs() > 0);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user