use of org.apache.servicecomb.metrics.core.meter.os.OsMeter in project java-chassis by ServiceComb.
the class OsMetersInitializer method init.
@Override
public void init(GlobalRegistry globalRegistry, EventBus eventBus, MetricsBootstrapConfig config) {
if (!isOsLinux) {
LOGGER.info("only support linux os to collect cpu and net info");
return;
}
Registry registry = globalRegistry.getDefaultRegistry();
osMeter = new OsMeter(registry);
SpectatorUtils.registerMeter(registry, osMeter);
}
use of org.apache.servicecomb.metrics.core.meter.os.OsMeter in project java-chassis by ServiceComb.
the class TestOsMeterInitializer method init.
@Test
public void init(@Mocked Runtime runtime, @Mocked RuntimeMXBean mxBean) {
List<String> list = new ArrayList<>();
list.add("13 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1");
list.add("useless");
list.add("eth0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0");
new MockUp<Files>() {
// Files.readFirstLine
@Mock
public String readFirstLine(File file, Charset encoding) {
return list.get(0);
}
};
new MockUp<FileUtils>() {
@Mock
public List<String> readLines(File file, Charset encoding) {
return list;
}
};
new MockUp<CpuUtils>() {
@Mock
public int calcHertz() {
return 100;
}
};
new MockUp<ManagementFactory>() {
@Mock
RuntimeMXBean getRuntimeMXBean() {
return mxBean;
}
};
new MockUp<Runtime>() {
@Mock
public Runtime getRuntime() {
return runtime;
}
};
new Expectations() {
{
runtime.availableProcessors();
result = 2;
}
};
globalRegistry.add(registry);
OsMetersInitializer osMetersInitializer = new OsMetersInitializer();
osMetersInitializer.setOsLinux(true);
osMetersInitializer.init(globalRegistry, eventBus, null);
OsMeter osMeter = osMetersInitializer.getOsMeter();
Assert.assertNotNull(osMeter);
Assert.assertNotNull(osMeter.getCpuMeter());
Assert.assertNotNull(osMeter.getNetMeter());
CpuMeter cpuMeter = osMeter.getCpuMeter();
NetMeter netMeter = osMeter.getNetMeter();
Assert.assertEquals(0.0, cpuMeter.getProcessCpuUsage().getUsage(), 0.0);
Assert.assertEquals(0.0, cpuMeter.getAllCpuUsage().getUsage(), 0.0);
Map<String, InterfaceUsage> interfaceInfoMap = netMeter.getInterfaceUsageMap();
Assert.assertEquals(1, interfaceInfoMap.size());
InterfaceUsage eth0 = interfaceInfoMap.get("eth0");
Assert.assertEquals(4, eth0.getNetStats().size());
// recv Bps
Assert.assertEquals(0L, eth0.getNetStats().get(0).getLastValue());
Assert.assertEquals(0, eth0.getNetStats().get(0).getRate(), 0.0);
Assert.assertEquals(0, eth0.getNetStats().get(0).getIndex());
// send Bps
Assert.assertEquals(0L, eth0.getNetStats().get(1).getLastValue());
Assert.assertEquals(0, eth0.getNetStats().get(1).getRate(), 0.0);
Assert.assertEquals(8, eth0.getNetStats().get(1).getIndex());
// recv pps
Assert.assertEquals(0L, eth0.getNetStats().get(2).getLastValue());
Assert.assertEquals(0, eth0.getNetStats().get(2).getRate(), 0.0);
Assert.assertEquals(1, eth0.getNetStats().get(2).getIndex());
// send pps
Assert.assertEquals(0L, eth0.getNetStats().get(3).getLastValue());
Assert.assertEquals(0, eth0.getNetStats().get(3).getRate(), 0.0);
Assert.assertEquals(9, eth0.getNetStats().get(3).getIndex());
}
Aggregations