Search in sources :

Example 1 with CpuMeter

use of org.apache.servicecomb.metrics.core.meter.os.CpuMeter 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());
}
Also used : Expectations(mockit.Expectations) NetMeter(org.apache.servicecomb.metrics.core.meter.os.NetMeter) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) MockUp(mockit.MockUp) OsMeter(org.apache.servicecomb.metrics.core.meter.os.OsMeter) File(java.io.File) CpuMeter(org.apache.servicecomb.metrics.core.meter.os.CpuMeter) InterfaceUsage(org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage) Test(org.junit.Test)

Aggregations

File (java.io.File)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 Expectations (mockit.Expectations)1 MockUp (mockit.MockUp)1 CpuMeter (org.apache.servicecomb.metrics.core.meter.os.CpuMeter)1 NetMeter (org.apache.servicecomb.metrics.core.meter.os.NetMeter)1 OsMeter (org.apache.servicecomb.metrics.core.meter.os.OsMeter)1 InterfaceUsage (org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage)1 Test (org.junit.Test)1