Search in sources :

Example 11 with Statistics

use of org.apache.geode.Statistics in project geode by apache.

the class StatArchiveWriterReaderIntegrationTest method testDestroyClosedStatistics.

/**
   * Verifies fix for bug #45377.
   */
@Test
public void testDestroyClosedStatistics() throws Exception {
    final StatArchiveDescriptor archiveDescriptor = new StatArchiveDescriptor.Builder().setArchiveName(this.archiveFileName).setSystemId(1).setSystemStartTime(WRITER_INITIAL_DATE_MILLIS - 2000).setSystemDirectoryPath(this.testName.getMethodName()).setProductDescription(getClass().getSimpleName()).build();
    final StatArchiveWriter writer = new TestStatArchiveWriter(archiveDescriptor);
    final StatisticsType statsType = createDummyStatisticsType();
    final ResourceType rt = new ResourceType(0, statsType);
    final Statistics statistics = mock(Statistics.class);
    when(statistics.isClosed()).thenReturn(true);
    final ResourceInstance ri = new ResourceInstance(0, statistics, rt);
    // if bug #45377 still existed, this call would throw IllegalStateException
    writer.destroyedResourceInstance(ri);
}
Also used : TestStatArchiveWriter(org.apache.geode.internal.statistics.TestStatArchiveWriter) StatisticsType(org.apache.geode.StatisticsType) TestStatArchiveWriter(org.apache.geode.internal.statistics.TestStatArchiveWriter) Statistics(org.apache.geode.Statistics) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 12 with Statistics

use of org.apache.geode.Statistics in project geode by apache.

the class StatArchiveWriterReaderIntegrationTest method testWriteAfterSamplingBegins.

/**
   * Tests the stat archive file written by StatArchiveWriter.
   */
@Test
public void testWriteAfterSamplingBegins() throws Exception {
    final TestStatisticsManager manager = new TestStatisticsManager(1, getUniqueName(), WRITER_INITIAL_DATE_MILLIS);
    final TestStatisticsSampler sampler = new TestStatisticsSampler(manager);
    final SampleCollector sampleCollector = new SampleCollector(sampler);
    final StatArchiveDescriptor archiveDescriptor = new StatArchiveDescriptor.Builder().setArchiveName(this.archiveFileName).setSystemId(1).setSystemStartTime(WRITER_INITIAL_DATE_MILLIS - 2000).setSystemDirectoryPath(this.testName.getMethodName()).setProductDescription(getClass().getSimpleName()).build();
    final StatArchiveWriter writer = new TestStatArchiveWriter(archiveDescriptor);
    sampleCollector.addSampleHandler(writer);
    long sampleTimeNanos = WRITER_PREVIOUS_TIMESTAMP_NANOS + NANOS_PER_MILLI * 1000;
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // 1) create ST1 and st1_1
    final StatisticDescriptor[] statsST1 = new StatisticDescriptor[] { manager.createDoubleCounter("double_counter_1", "d1", "u1"), manager.createDoubleCounter("double_counter_2", "d2", "u2", true), manager.createDoubleGauge("double_gauge_3", "d3", "u3"), manager.createDoubleGauge("double_gauge_4", "d4", "u4", false), manager.createIntCounter("int_counter_5", "d5", "u5"), manager.createIntCounter("int_counter_6", "d6", "u6", true), manager.createIntGauge("int_gauge_7", "d7", "u7"), manager.createIntGauge("int_gauge_8", "d8", "u8", false), manager.createLongCounter("long_counter_9", "d9", "u9"), manager.createLongCounter("long_counter_10", "d10", "u10", true), manager.createLongGauge("long_gauge_11", "d11", "u11"), manager.createLongGauge("long_gauge_12", "d12", "u12", false) };
    final StatisticsType ST1 = manager.createType("ST1", "ST1", statsST1);
    final Statistics st1_1 = manager.createAtomicStatistics(ST1, "st1_1", 1);
    // 2) create st1_2
    final Statistics st1_2 = manager.createAtomicStatistics(ST1, "st1_2", 2);
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // 3) some new values
    incDouble(st1_1, "double_counter_1", 18347.94880);
    incDouble(st1_1, "double_gauge_4", 24885.02346);
    incInt(st1_1, "int_counter_5", 3);
    incInt(st1_1, "int_gauge_8", 4);
    incLong(st1_1, "long_counter_9", 1073741824);
    incLong(st1_1, "long_gauge_12", 154367897);
    incDouble(st1_2, "double_counter_2", 346.95);
    incDouble(st1_2, "double_gauge_3", 9865.23008);
    incInt(st1_2, "int_counter_6", 4);
    incInt(st1_2, "int_gauge_7", 3);
    incLong(st1_2, "long_counter_10", 3497536);
    incLong(st1_2, "long_gauge_11", 103909646);
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // 4) all new values
    incDouble(st1_1, "double_counter_1", 1.098367);
    incDouble(st1_1, "double_counter_2", 50247.0983254);
    incDouble(st1_1, "double_gauge_3", 987654.2344);
    incDouble(st1_1, "double_gauge_4", 23.097);
    incInt(st1_1, "int_counter_5", 3);
    incInt(st1_1, "int_counter_6", 4);
    incInt(st1_1, "int_gauge_7", 3);
    incInt(st1_1, "int_gauge_8", 4);
    incLong(st1_1, "long_counter_9", 5);
    incLong(st1_1, "long_counter_10", 465793);
    incLong(st1_1, "long_gauge_11", -203050);
    incLong(st1_1, "long_gauge_12", 6);
    incDouble(st1_2, "double_counter_1", 0.000846643);
    incDouble(st1_2, "double_counter_2", 4.0);
    incDouble(st1_2, "double_gauge_3", -4.0);
    incDouble(st1_2, "double_gauge_4", 19276.0346624);
    incInt(st1_2, "int_counter_5", 1);
    incInt(st1_2, "int_counter_6", 2);
    incInt(st1_2, "int_gauge_7", -1);
    incInt(st1_2, "int_gauge_8", -2);
    incLong(st1_2, "long_counter_9", 309876);
    incLong(st1_2, "long_counter_10", 4);
    incLong(st1_2, "long_gauge_11", -4);
    incLong(st1_2, "long_gauge_12", 1098764);
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // 5) no new values
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // 6) some new values
    incDouble(st1_1, "double_counter_2", 10.255);
    incDouble(st1_1, "double_gauge_3", -4123.05);
    incInt(st1_1, "int_counter_6", 2);
    incInt(st1_1, "int_gauge_7", 3);
    incLong(st1_1, "long_counter_10", 4);
    incLong(st1_1, "long_gauge_11", -2);
    incDouble(st1_2, "double_counter_1", 5.00007634);
    incDouble(st1_2, "double_gauge_4", 16904.06524);
    incInt(st1_2, "int_counter_5", 4);
    incInt(st1_2, "int_gauge_8", 1);
    incLong(st1_2, "long_counter_9", 8);
    incLong(st1_2, "long_gauge_12", 10);
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // 7) all new values
    incDouble(st1_1, "double_counter_1", 4065.340);
    incDouble(st1_1, "double_counter_2", 2.01342568);
    incDouble(st1_1, "double_gauge_3", 1.367890);
    incDouble(st1_1, "double_gauge_4", 8.0549003);
    incInt(st1_1, "int_counter_5", 2);
    incInt(st1_1, "int_counter_6", 9);
    incInt(st1_1, "int_gauge_7", 1);
    incInt(st1_1, "int_gauge_8", 2);
    incLong(st1_1, "long_counter_9", 6);
    incLong(st1_1, "long_counter_10", 2);
    incLong(st1_1, "long_gauge_11", -10);
    incLong(st1_1, "long_gauge_12", 8);
    incDouble(st1_2, "double_counter_1", 128.2450);
    incDouble(st1_2, "double_counter_2", 113.550);
    incDouble(st1_2, "double_gauge_3", 21.0676);
    incDouble(st1_2, "double_gauge_4", 2.01346);
    incInt(st1_2, "int_counter_5", 3);
    incInt(st1_2, "int_counter_6", 4);
    incInt(st1_2, "int_gauge_7", 4);
    incInt(st1_2, "int_gauge_8", 2);
    incLong(st1_2, "long_counter_9", 1);
    incLong(st1_2, "long_counter_10", 2);
    incLong(st1_2, "long_gauge_11", 3);
    incLong(st1_2, "long_gauge_12", -2);
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // 8) create ST2 and ST3 and st2_1 and st3_1 and st3_2
    final StatisticDescriptor[] statsST2 = new StatisticDescriptor[] { manager.createIntGauge("int_gauge_7", "d7", "u7"), manager.createIntGauge("int_gauge_8", "d8", "u8", false), manager.createLongCounter("long_counter_9", "d9", "u9"), manager.createLongCounter("long_counter_10", "d10", "u10", true), manager.createLongGauge("long_gauge_11", "d11", "u11"), manager.createLongGauge("long_gauge_12", "d12", "u12", false) };
    final StatisticsType ST2 = manager.createType("ST2", "ST2", statsST2);
    final Statistics st2_1 = manager.createAtomicStatistics(ST2, "st2_1", 1);
    final StatisticDescriptor[] statsST3 = new StatisticDescriptor[] { manager.createDoubleCounter("double_counter_1", "d1", "u1"), manager.createDoubleCounter("double_counter_2", "d2", "u2", true), manager.createDoubleGauge("double_gauge_3", "d3", "u3"), manager.createDoubleGauge("double_gauge_4", "d4", "u4", false), manager.createIntCounter("int_counter_5", "d5", "u5"), manager.createIntCounter("int_counter_6", "d6", "u6", true) };
    final StatisticsType ST3 = manager.createType("ST3", "ST3", statsST3);
    final Statistics st3_1 = manager.createAtomicStatistics(ST3, "st3_1", 1);
    final Statistics st3_2 = manager.createAtomicStatistics(ST3, "st3_2", 2);
    // 9) all new values
    incDouble(st1_1, "double_counter_1", 9499.10);
    incDouble(st1_1, "double_counter_2", 83.0);
    incDouble(st1_1, "double_gauge_3", -7.05678);
    incDouble(st1_1, "double_gauge_4", 5111.031);
    incInt(st1_1, "int_counter_5", 1);
    incInt(st1_1, "int_counter_6", 3);
    incInt(st1_1, "int_gauge_7", 9);
    incInt(st1_1, "int_gauge_8", -3);
    incLong(st1_1, "long_counter_9", 3);
    incLong(st1_1, "long_counter_10", 8);
    incLong(st1_1, "long_gauge_11", 5);
    incLong(st1_1, "long_gauge_12", 4);
    incDouble(st1_2, "double_counter_1", 2509.0235);
    incDouble(st1_2, "double_counter_2", 409.10063);
    incDouble(st1_2, "double_gauge_3", -42.66904);
    incDouble(st1_2, "double_gauge_4", 21.0098);
    incInt(st1_2, "int_counter_5", 8);
    incInt(st1_2, "int_counter_6", 9);
    incInt(st1_2, "int_gauge_7", -2);
    incInt(st1_2, "int_gauge_8", 6);
    incLong(st1_2, "long_counter_9", 4);
    incLong(st1_2, "long_counter_10", 5);
    incLong(st1_2, "long_gauge_11", 5);
    incLong(st1_2, "long_gauge_12", -1);
    incInt(st2_1, "int_gauge_7", 2);
    incInt(st2_1, "int_gauge_8", -1);
    incLong(st2_1, "long_counter_9", 1002948);
    incLong(st2_1, "long_counter_10", 29038856);
    incLong(st2_1, "long_gauge_11", -2947465);
    incLong(st2_1, "long_gauge_12", 4934745);
    incDouble(st3_1, "double_counter_1", 562.0458);
    incDouble(st3_1, "double_counter_2", 14.0086);
    incDouble(st3_1, "double_gauge_3", -2.0);
    incDouble(st3_1, "double_gauge_4", 1.0);
    incInt(st3_1, "int_counter_5", 2);
    incInt(st3_1, "int_counter_6", 1);
    incDouble(st3_2, "double_counter_1", 33.087);
    incDouble(st3_2, "double_counter_2", 2.02);
    incDouble(st3_2, "double_gauge_3", 1.06);
    incDouble(st3_2, "double_gauge_4", 3.021);
    incInt(st3_2, "int_counter_5", 1);
    incInt(st3_2, "int_counter_6", 4);
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // 10) some new values
    incDouble(st1_1, "double_counter_1", 3.014);
    incDouble(st1_1, "double_gauge_3", 57.003);
    incInt(st1_1, "int_counter_5", 3);
    incInt(st1_1, "int_gauge_7", 5);
    incLong(st1_1, "long_counter_9", 1);
    incLong(st1_1, "long_gauge_11", 1);
    incDouble(st1_2, "double_counter_2", 20.107);
    incDouble(st1_2, "double_gauge_4", 1.5078);
    incInt(st1_2, "int_counter_6", 1);
    incInt(st1_2, "int_gauge_8", -1);
    incLong(st1_2, "long_counter_10", 1073741824);
    incLong(st1_2, "long_gauge_12", 5);
    incInt(st2_1, "int_gauge_7", 2);
    incLong(st2_1, "long_counter_9", 2);
    incLong(st2_1, "long_gauge_11", -2);
    incDouble(st3_1, "double_counter_1", 24.80097);
    incDouble(st3_1, "double_gauge_3", -22.09834);
    incInt(st3_1, "int_counter_5", 2);
    incDouble(st3_2, "double_counter_2", 21.0483);
    incDouble(st3_2, "double_gauge_4", 36310.012);
    incInt(st3_2, "int_counter_6", 4);
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // 11) remove ST2 and st2_1
    manager.destroyStatistics(st2_1);
    // 12) some new values
    incDouble(st1_1, "double_counter_1", 339.0803);
    incDouble(st1_1, "double_counter_2", 21.06);
    incDouble(st1_1, "double_gauge_3", 12.056);
    incDouble(st1_1, "double_gauge_4", 27.108);
    incInt(st1_1, "int_counter_5", 2);
    incInt(st1_1, "int_counter_6", 4);
    incInt(st1_2, "int_gauge_7", 4);
    incInt(st1_2, "int_gauge_8", 7);
    incLong(st1_2, "long_counter_9", 8);
    incLong(st1_2, "long_counter_10", 4);
    incLong(st1_2, "long_gauge_11", 2);
    incLong(st1_2, "long_gauge_12", 1);
    incDouble(st3_1, "double_counter_1", 41.103);
    incDouble(st3_1, "double_counter_2", 2.0333);
    incDouble(st3_1, "double_gauge_3", -14.0);
    incDouble(st3_2, "double_gauge_4", 26.01);
    incInt(st3_2, "int_counter_5", 3);
    incInt(st3_2, "int_counter_6", 1);
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // 13) no new values
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // 14) remove st1_2
    manager.destroyStatistics(st1_2);
    // 15) all new values
    incDouble(st1_1, "double_counter_1", 62.1350);
    incDouble(st1_1, "double_counter_2", 33.306);
    incDouble(st1_1, "double_gauge_3", 41.1340);
    incDouble(st1_1, "double_gauge_4", -1.04321);
    incInt(st1_1, "int_counter_5", 2);
    incInt(st1_1, "int_counter_6", 2);
    incInt(st1_1, "int_gauge_7", 1);
    incInt(st1_1, "int_gauge_8", 9);
    incLong(st1_1, "long_counter_9", 2);
    incLong(st1_1, "long_counter_10", 5);
    incLong(st1_1, "long_gauge_11", 3);
    incLong(st1_1, "long_gauge_12", -2);
    incDouble(st3_1, "double_counter_1", 3461.0153);
    incDouble(st3_1, "double_counter_2", 5.03167);
    incDouble(st3_1, "double_gauge_3", -1.31051);
    incDouble(st3_1, "double_gauge_4", 71.031);
    incInt(st3_1, "int_counter_5", 4);
    incInt(st3_1, "int_counter_6", 2);
    incDouble(st3_2, "double_counter_1", 531.5608);
    incDouble(st3_2, "double_counter_2", 55.0532);
    incDouble(st3_2, "double_gauge_3", 8.40956);
    incDouble(st3_2, "double_gauge_4", 23230.0462);
    incInt(st3_2, "int_counter_5", 9);
    incInt(st3_2, "int_counter_6", 5);
    sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    // close the writer
    writer.close();
    if (false) {
        StatisticDescriptor[] sds = ST1.getStatistics();
        for (int i = 0; i < sds.length; i++) {
            logger.info("testWriteAfterSamplingBegins#st1_1#" + sds[i].getName() + "=" + st1_1.get(sds[i].getName()));
        }
        for (int i = 0; i < sds.length; i++) {
            logger.info("testWriteAfterSamplingBegins#st1_2#" + sds[i].getName() + "=" + st1_2.get(sds[i].getName()));
        }
        sds = ST2.getStatistics();
        for (int i = 0; i < sds.length; i++) {
            logger.info("testWriteAfterSamplingBegins#st2_1#" + sds[i].getName() + "=" + st2_1.get(sds[i].getName()));
        }
        sds = ST3.getStatistics();
        for (int i = 0; i < sds.length; i++) {
            logger.info("testWriteAfterSamplingBegins#st3_1#" + sds[i].getName() + "=" + st3_1.get(sds[i].getName()));
        }
        for (int i = 0; i < sds.length; i++) {
            logger.info("testWriteAfterSamplingBegins#st3_2#" + sds[i].getName() + "=" + st3_2.get(sds[i].getName()));
        }
    }
    // validate that stat archive file exists
    final File actual = new File(this.archiveFileName);
    assertTrue(actual.exists());
    // validate content of stat archive file using StatArchiveReader
    final StatArchiveReader reader = new StatArchiveReader(new File[] { actual }, null, false);
    // compare all resourceInst values against what was printed above
    final List resources = reader.getResourceInstList();
    for (final Iterator iter = resources.iterator(); iter.hasNext(); ) {
        final StatArchiveReader.ResourceInst ri = (StatArchiveReader.ResourceInst) iter.next();
        final String resourceName = ri.getName();
        assertNotNull(resourceName);
        final String expectedStatsType = this.statisticTypes.get(resourceName);
        assertNotNull(expectedStatsType);
        assertEquals(expectedStatsType, ri.getType().getName());
        final Map<String, Number> expectedStatValues = this.allStatistics.get(resourceName);
        assertNotNull(expectedStatValues);
        final StatValue[] statValues = ri.getStatValues();
        for (int i = 0; i < statValues.length; i++) {
            final String statName = ri.getType().getStats()[i].getName();
            assertNotNull(statName);
            assertNotNull(expectedStatValues.get(statName));
            assertEquals(statName, statValues[i].getDescriptor().getName());
            statValues[i].setFilter(StatValue.FILTER_NONE);
            final double[] rawSnapshots = statValues[i].getRawSnapshots();
            // for (int j = 0; j < rawSnapshots.length; j++) {
            // logger.info("DEBUG " + ri.getName() + " " + statName + " rawSnapshots[" + j + "] = " +
            // rawSnapshots[j]);
            // }
            assertEquals("Value " + i + " for " + statName + " is wrong: " + expectedStatValues, expectedStatValues.get(statName).doubleValue(), statValues[i].getSnapshotsMostRecent(), 0.01);
        }
    }
    // validate byte content of stat archive file against saved expected file
    final File expected = new File(TestUtil.getResourcePath(getClass(), "StatArchiveWriterReaderJUnitTest_" + this.testName.getMethodName() + "_expected.gfs"));
    assertTrue(expected + " does not exist!", expected.exists());
    assertEquals(expected.length(), actual.length());
    assertTrue("Actual stat archive file bytes differ from expected stat archive file bytes!", Arrays.equals(readBytes(expected), readBytes(actual)));
}
Also used : TestStatArchiveWriter(org.apache.geode.internal.statistics.TestStatArchiveWriter) StatValue(org.apache.geode.internal.statistics.StatArchiveReader.StatValue) TestStatArchiveWriter(org.apache.geode.internal.statistics.TestStatArchiveWriter) Iterator(java.util.Iterator) List(java.util.List) StatisticsType(org.apache.geode.StatisticsType) Statistics(org.apache.geode.Statistics) StatisticDescriptor(org.apache.geode.StatisticDescriptor) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 13 with Statistics

use of org.apache.geode.Statistics in project geode by apache.

the class StatArchiveWriterReaderIntegrationTest method testIntCounterOneSample.

@Test
public void testIntCounterOneSample() throws Exception {
    final TestStatisticsManager manager = new TestStatisticsManager(1, getUniqueName(), WRITER_INITIAL_DATE_MILLIS);
    final TestStatisticsSampler sampler = new TestStatisticsSampler(manager);
    final SampleCollector sampleCollector = new SampleCollector(sampler);
    final StatArchiveDescriptor archiveDescriptor = new StatArchiveDescriptor.Builder().setArchiveName(this.archiveFileName).setSystemId(1).setSystemStartTime(WRITER_INITIAL_DATE_MILLIS).setSystemDirectoryPath(this.testName.getMethodName()).setProductDescription(getClass().getSimpleName()).build();
    final StatArchiveWriter writer = new TestStatArchiveWriter(archiveDescriptor);
    sampleCollector.addSampleHandler(writer);
    final StatisticDescriptor[] statsST1 = new StatisticDescriptor[] { manager.createIntCounter("int_counter_1", "d1", "u1") };
    final StatisticsType ST1 = manager.createType("ST1", "ST1", statsST1);
    final Statistics st1_1 = manager.createAtomicStatistics(ST1, "st1_1", 1);
    final int value = 5;
    incInt(st1_1, "int_counter_1", value);
    final long sampleIncNanos = NANOS_PER_MILLI * 1000;
    final long sampleTimeNanos = WRITER_PREVIOUS_TIMESTAMP_NANOS + sampleIncNanos;
    sampleCollector.sample(sampleTimeNanos);
    writer.close();
    final StatisticDescriptor[] sds = ST1.getStatistics();
    for (int i = 0; i < sds.length; i++) {
        assertEquals(value, st1_1.get(sds[i].getName()));
    }
    final StatArchiveReader reader = new StatArchiveReader(new File[] { new File(this.archiveFileName) }, null, false);
    // compare all resourceInst values against what was printed above
    final List resources = reader.getResourceInstList();
    assertNotNull(resources);
    assertEquals(1, resources.size());
    final StatArchiveReader.ResourceInst ri = (StatArchiveReader.ResourceInst) resources.get(0);
    assertNotNull(ri);
    final String statsName = ri.getName();
    assertNotNull(statsName);
    assertEquals("st1_1", statsName);
    assertEquals("ST1", ri.getType().getName());
    final StatValue[] statValues = ri.getStatValues();
    assertNotNull(statValues);
    assertEquals(1, statValues.length);
    final String statName = ri.getType().getStats()[0].getName();
    assertNotNull(statName);
    assertEquals("int_counter_1", statName);
    assertEquals(statName, statValues[0].getDescriptor().getName());
    assertEquals(1, statValues[0].getSnapshotsSize());
    assertEquals((double) value, statValues[0].getSnapshotsMostRecent(), 0.01);
    final long[] timeStampsMillis = statValues[0].getRawAbsoluteTimeStamps();
    assertNotNull(timeStampsMillis);
    assertEquals(1, timeStampsMillis.length);
    final long initPreviousTimeStampMillis = NanoTimer.nanosToMillis(WRITER_PREVIOUS_TIMESTAMP_NANOS);
    final long timeStampMillis = NanoTimer.nanosToMillis(sampleTimeNanos);
    final long deltaMillis = timeStampMillis - initPreviousTimeStampMillis;
    assertEquals(NanoTimer.nanosToMillis(sampleIncNanos), deltaMillis);
    final long expectedTimeStampMillis = deltaMillis + WRITER_INITIAL_DATE_MILLIS;
    assertEquals(expectedTimeStampMillis, timeStampsMillis[0]);
    final double[] snapshots = statValues[0].getRawSnapshots();
    assertNotNull(snapshots);
    assertEquals(1, snapshots.length);
    assertEquals((double) value, snapshots[0], 0.01);
}
Also used : StatisticsType(org.apache.geode.StatisticsType) TestStatArchiveWriter(org.apache.geode.internal.statistics.TestStatArchiveWriter) Statistics(org.apache.geode.Statistics) StatValue(org.apache.geode.internal.statistics.StatArchiveReader.StatValue) StatisticDescriptor(org.apache.geode.StatisticDescriptor) TestStatArchiveWriter(org.apache.geode.internal.statistics.TestStatArchiveWriter) List(java.util.List) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 14 with Statistics

use of org.apache.geode.Statistics in project geode by apache.

the class StatSamplerIntegrationTest method testStatSampler.

@Test
public void testStatSampler() throws Exception {
    StatArchiveWriter.setTraceFilter("st1_1", "ST1");
    File folder = temporaryFolder.newFolder();
    String archiveFileName = folder.getAbsolutePath() + File.separator + getName() + ".gfs";
    System.setProperty("stats.log-level", "config");
    System.setProperty("stats.disable", "false");
    System.setProperty("stats.name", getName());
    System.setProperty("stats.archive-file", archiveFileName);
    System.setProperty("stats.file-size-limit", "0");
    System.setProperty("stats.disk-space-limit", "0");
    System.setProperty("stats.sample-rate", "100");
    final CancelCriterion stopper = new CancelCriterion() {

        public String cancelInProgress() {
            return null;
        }

        public RuntimeException generateCancelledException(Throwable e) {
            return null;
        }
    };
    final LocalStatisticsFactory factory = new LocalStatisticsFactory(stopper);
    final StatisticDescriptor[] statsST1 = new StatisticDescriptor[] { factory.createDoubleCounter("double_counter_1", "d1", "u1"), factory.createDoubleCounter("double_counter_2", "d2", "u2", true), factory.createDoubleGauge("double_gauge_3", "d3", "u3"), factory.createDoubleGauge("double_gauge_4", "d4", "u4", false), factory.createIntCounter("int_counter_5", "d5", "u5"), factory.createIntCounter("int_counter_6", "d6", "u6", true), factory.createIntGauge("int_gauge_7", "d7", "u7"), factory.createIntGauge("int_gauge_8", "d8", "u8", false), factory.createLongCounter("long_counter_9", "d9", "u9"), factory.createLongCounter("long_counter_10", "d10", "u10", true), factory.createLongGauge("long_gauge_11", "d11", "u11"), factory.createLongGauge("long_gauge_12", "d12", "u12", false), factory.createLongGauge("sampled_long", "d13", "u13", false), factory.createIntGauge("sampled_int", "d14", "u14", false), factory.createDoubleGauge("sampled_double", "d15", "u15", false) };
    final StatisticsType ST1 = factory.createType("ST1", "ST1", statsST1);
    final Statistics st1_1 = factory.createAtomicStatistics(ST1, "st1_1", 1);
    st1_1.setIntSupplier("sampled_int", () -> 5);
    getOrCreateExpectedValueMap(st1_1).put("sampled_int", 5);
    st1_1.setLongSupplier("sampled_long", () -> 6);
    getOrCreateExpectedValueMap(st1_1).put("sampled_long", 6);
    st1_1.setDoubleSupplier("sampled_double", () -> 7.0);
    getOrCreateExpectedValueMap(st1_1).put("sampled_double", 7.0);
    await("awaiting StatSampler readiness").atMost(30, SECONDS).until(() -> hasSamplerStatsInstances(factory));
    Statistics[] samplerStatsInstances = factory.findStatisticsByTextId("statSampler");
    assertNotNull(samplerStatsInstances);
    assertEquals(1, samplerStatsInstances.length);
    final Statistics samplerStats = samplerStatsInstances[0];
    incDouble(st1_1, "double_counter_1", 1);
    incDouble(st1_1, "double_gauge_3", 3);
    incInt(st1_1, "int_counter_5", 5);
    incInt(st1_1, "int_gauge_7", 7);
    incLong(st1_1, "long_counter_9", 9);
    incLong(st1_1, "long_gauge_11", 11);
    awaitStatSample(samplerStats);
    incDouble(st1_1, "double_counter_1", 1);
    incDouble(st1_1, "double_counter_2", 1);
    incDouble(st1_1, "double_gauge_3", 1);
    incDouble(st1_1, "double_gauge_4", 1);
    incInt(st1_1, "int_counter_5", 1);
    incInt(st1_1, "int_counter_6", 1);
    awaitStatSample(samplerStats);
    incDouble(st1_1, "double_counter_1", 1);
    incDouble(st1_1, "double_counter_2", 1);
    incDouble(st1_1, "double_gauge_3", 1);
    incDouble(st1_1, "double_gauge_4", 1);
    incInt(st1_1, "int_counter_5", 1);
    incInt(st1_1, "int_counter_6", 1);
    incInt(st1_1, "int_gauge_7", 1);
    incInt(st1_1, "int_gauge_8", 1);
    incLong(st1_1, "long_counter_9", 1);
    incLong(st1_1, "long_counter_10", 1);
    incLong(st1_1, "long_gauge_11", 1);
    incLong(st1_1, "long_gauge_12", 1);
    awaitStatSample(samplerStats);
    incDouble(st1_1, "double_counter_1", 1);
    incDouble(st1_1, "double_counter_2", 1);
    incDouble(st1_1, "double_gauge_3", -1);
    incDouble(st1_1, "double_gauge_4", 1);
    incInt(st1_1, "int_counter_5", 1);
    incInt(st1_1, "int_counter_6", 1);
    incInt(st1_1, "int_gauge_7", -1);
    incInt(st1_1, "int_gauge_8", 1);
    incLong(st1_1, "long_counter_9", 1);
    incLong(st1_1, "long_counter_10", 1);
    incLong(st1_1, "long_gauge_11", -1);
    incLong(st1_1, "long_gauge_12", 1);
    awaitStatSample(samplerStats);
    incDouble(st1_1, "double_counter_1", 1);
    incDouble(st1_1, "double_counter_2", 1);
    incDouble(st1_1, "double_gauge_3", 1);
    incDouble(st1_1, "double_gauge_4", 1);
    incInt(st1_1, "int_counter_5", 1);
    incInt(st1_1, "int_counter_6", 1);
    incInt(st1_1, "int_gauge_7", 1);
    incInt(st1_1, "int_gauge_8", 1);
    incLong(st1_1, "long_counter_9", 1);
    incLong(st1_1, "long_counter_10", 1);
    incLong(st1_1, "long_gauge_11", 1);
    incLong(st1_1, "long_gauge_12", 1);
    awaitStatSample(samplerStats);
    awaitStatSample(samplerStats);
    awaitStatSample(samplerStats);
    incDouble(st1_1, "double_counter_1", 1);
    incDouble(st1_1, "double_gauge_3", 3);
    incInt(st1_1, "int_counter_5", 5);
    incInt(st1_1, "int_gauge_7", 7);
    incLong(st1_1, "long_counter_9", 9);
    incLong(st1_1, "long_gauge_11", 11);
    awaitStatSample(samplerStats);
    incDouble(st1_1, "double_counter_1", 1);
    incDouble(st1_1, "double_counter_2", 1);
    incDouble(st1_1, "double_gauge_3", 1);
    incDouble(st1_1, "double_gauge_4", 1);
    incInt(st1_1, "int_counter_5", 1);
    incInt(st1_1, "int_counter_6", 1);
    awaitStatSample(samplerStats);
    incDouble(st1_1, "double_counter_1", 1);
    incDouble(st1_1, "double_counter_2", 1);
    incDouble(st1_1, "double_gauge_3", 1);
    incDouble(st1_1, "double_gauge_4", 1);
    incInt(st1_1, "int_counter_5", 1);
    incInt(st1_1, "int_counter_6", 1);
    incInt(st1_1, "int_gauge_7", 1);
    incInt(st1_1, "int_gauge_8", 1);
    incLong(st1_1, "long_counter_9", 1);
    incLong(st1_1, "long_counter_10", 1);
    incLong(st1_1, "long_gauge_11", 1);
    incLong(st1_1, "long_gauge_12", 1);
    awaitStatSample(samplerStats);
    incDouble(st1_1, "double_counter_1", 1);
    incDouble(st1_1, "double_counter_2", 1);
    incDouble(st1_1, "double_gauge_3", -1);
    incDouble(st1_1, "double_gauge_4", 1);
    incInt(st1_1, "int_counter_5", 1);
    incInt(st1_1, "int_counter_6", 1);
    incInt(st1_1, "int_gauge_7", -1);
    incInt(st1_1, "int_gauge_8", 1);
    incLong(st1_1, "long_counter_9", 1);
    incLong(st1_1, "long_counter_10", 1);
    incLong(st1_1, "long_gauge_11", -1);
    incLong(st1_1, "long_gauge_12", 1);
    awaitStatSample(samplerStats);
    incDouble(st1_1, "double_counter_1", 1);
    incDouble(st1_1, "double_counter_2", 1);
    incDouble(st1_1, "double_gauge_3", 1);
    incDouble(st1_1, "double_gauge_4", 1);
    incInt(st1_1, "int_counter_5", 1);
    incInt(st1_1, "int_counter_6", 1);
    incInt(st1_1, "int_gauge_7", 1);
    incInt(st1_1, "int_gauge_8", 1);
    incLong(st1_1, "long_counter_9", 1);
    incLong(st1_1, "long_counter_10", 1);
    incLong(st1_1, "long_gauge_11", 1);
    incLong(st1_1, "long_gauge_12", 1);
    awaitStatSample(samplerStats);
    factory.close();
    final File archiveFile = new File(System.getProperty("stats.archive-file"));
    assertTrue(archiveFile.exists());
    final StatArchiveReader reader = new StatArchiveReader(new File[] { archiveFile }, null, false);
    List resources = reader.getResourceInstList();
    for (Iterator iter = resources.iterator(); iter.hasNext(); ) {
        StatArchiveReader.ResourceInst ri = (StatArchiveReader.ResourceInst) iter.next();
        String resourceName = ri.getName();
        assertNotNull(resourceName);
        if (!resourceName.equals("st1_1")) {
            logger.info("testStatSampler skipping {}", resourceName);
            continue;
        }
        String expectedStatsType = this.statisticTypes.get(resourceName);
        assertNotNull(expectedStatsType);
        assertEquals(expectedStatsType, ri.getType().getName());
        Map<String, Number> expectedStatValues = this.allStatistics.get(resourceName);
        assertNotNull(expectedStatValues);
        StatValue[] statValues = ri.getStatValues();
        for (int i = 0; i < statValues.length; i++) {
            String statName = ri.getType().getStats()[i].getName();
            assertNotNull(statName);
            assertNotNull(expectedStatValues.get(statName));
            assertEquals(statName, statValues[i].getDescriptor().getName());
            statValues[i].setFilter(StatValue.FILTER_NONE);
            // double[] rawSnapshots = statValues[i].getRawSnapshots();
            assertEquals("Value " + i + " for " + statName + " is wrong: " + expectedStatValues, expectedStatValues.get(statName).doubleValue(), statValues[i].getSnapshotsMostRecent(), 0);
        }
    }
}
Also used : StatisticsType(org.apache.geode.StatisticsType) CancelCriterion(org.apache.geode.CancelCriterion) Statistics(org.apache.geode.Statistics) StatValue(org.apache.geode.internal.statistics.StatArchiveReader.StatValue) StatisticDescriptor(org.apache.geode.StatisticDescriptor) Iterator(java.util.Iterator) List(java.util.List) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 15 with Statistics

use of org.apache.geode.Statistics in project geode by apache.

the class StatTypesAreRolledOverRegressionTest method setUp.

@Before
public void setUp() throws Exception {
    this.dir = this.temporaryFolder.getRoot();
    this.archiveFileName = new File(this.dir, this.testName.getMethodName() + ".gfs").getAbsolutePath();
    this.factory = new LocalStatisticsFactory(null);
    this.statisticDescriptors = new StatisticDescriptor[] { this.factory.createIntCounter("stat1", "description of stat1", "units", true) };
    this.statisticsType = factory.createType("statisticsType1", "statisticsType1", this.statisticDescriptors);
    this.statistics = factory.createAtomicStatistics(this.statisticsType, "statistics1", 1);
    Answer<Statistics[]> statisticsAnswer = invocation -> factory.getStatistics();
    Answer<Integer> modCountAnswer = invocation -> factory.getStatListModCount();
    StatisticsSampler sampler = mock(StatisticsSampler.class);
    when(sampler.getStatistics()).thenAnswer(statisticsAnswer);
    when(sampler.getStatisticsModCount()).thenAnswer(modCountAnswer);
    StatArchiveHandlerConfig config = mock(StatArchiveHandlerConfig.class);
    when(config.getArchiveFileName()).thenReturn(new File(this.archiveFileName));
    when(config.getArchiveFileSizeLimit()).thenReturn(FILE_SIZE_LIMIT);
    when(config.getSystemId()).thenReturn(1L);
    when(config.getSystemStartTime()).thenReturn(System.currentTimeMillis());
    when(config.getSystemDirectoryPath()).thenReturn(this.temporaryFolder.getRoot().getAbsolutePath());
    when(config.getProductDescription()).thenReturn(this.testName.getMethodName());
    when(config.getArchiveDiskSpaceLimit()).thenReturn(0L);
    this.sampleCollector = new SampleCollector(sampler);
    this.sampleCollector.initialize(config, this.timer.getTime(), new MainWithChildrenRollingFileHandler());
    this.timer.reset();
    this.nanosTimeStamp = this.timer.getLastResetTime() - getNanoRate();
}
Also used : NanoTimer(org.apache.geode.internal.NanoTimer) TimeoutException(java.util.concurrent.TimeoutException) Statistics(org.apache.geode.Statistics) StatisticsType(org.apache.geode.StatisticsType) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestName(org.junit.rules.TestName) After(org.junit.After) Assertions(org.assertj.core.api.Assertions) MainWithChildrenRollingFileHandler(org.apache.geode.internal.io.MainWithChildrenRollingFileHandler) Before(org.junit.Before) StatisticDescriptor(org.apache.geode.StatisticDescriptor) IOException(java.io.IOException) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) List(java.util.List) Rule(org.junit.Rule) TEST_FILE_SIZE_LIMIT_IN_KB_PROPERTY(org.apache.geode.internal.statistics.HostStatSampler.TEST_FILE_SIZE_LIMIT_IN_KB_PROPERTY) ResourceInst(org.apache.geode.internal.statistics.StatArchiveReader.ResourceInst) RestoreSystemProperties(org.junit.contrib.java.lang.system.RestoreSystemProperties) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest) TemporaryFolder(org.junit.rules.TemporaryFolder) MainWithChildrenRollingFileHandler(org.apache.geode.internal.io.MainWithChildrenRollingFileHandler) File(java.io.File) Before(org.junit.Before)

Aggregations

Statistics (org.apache.geode.Statistics)74 StatisticsType (org.apache.geode.StatisticsType)36 Test (org.junit.Test)34 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)29 StatisticDescriptor (org.apache.geode.StatisticDescriptor)18 File (java.io.File)17 ArrayList (java.util.ArrayList)12 List (java.util.List)12 StatValue (org.apache.geode.internal.statistics.StatArchiveReader.StatValue)11 TestStatArchiveWriter (org.apache.geode.internal.statistics.TestStatArchiveWriter)10 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)6 LRUStatistics (org.apache.geode.internal.cache.lru.LRUStatistics)6 HashMap (java.util.HashMap)5 LinuxProcFsStatistics (org.apache.geode.internal.statistics.platform.LinuxProcFsStatistics)5 Iterator (java.util.Iterator)4 Map (java.util.Map)4 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)4 MainWithChildrenRollingFileHandler (org.apache.geode.internal.io.MainWithChildrenRollingFileHandler)3 Before (org.junit.Before)3 IOException (java.io.IOException)2