use of org.apache.geode.internal.statistics.TestStatArchiveWriter in project geode by apache.
the class StatArchiveWriterReaderIntegrationTest method testLongCounterTwoSamples.
@Test
public void testLongCounterTwoSamples() 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.createLongCounter("long_counter_1", "d1", "u1") };
final StatisticsType ST1 = manager.createType("ST1", "ST1", statsST1);
final Statistics st1_1 = manager.createAtomicStatistics(ST1, "st1_1", 1);
final long value1 = 5;
incLong(st1_1, "long_counter_1", value1);
final long sampleIncNanos = NANOS_PER_MILLI * 1000;
long sampleTimeNanos = WRITER_PREVIOUS_TIMESTAMP_NANOS + sampleIncNanos;
sampleCollector.sample(sampleTimeNanos);
final long value2 = 15;
incLong(st1_1, "long_counter_1", value2);
sampleTimeNanos += sampleIncNanos;
sampleCollector.sample(sampleTimeNanos);
writer.close();
final StatisticDescriptor[] sds = ST1.getStatistics();
for (int i = 0; i < sds.length; i++) {
assertEquals(value1 + value2, 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);
statValues[0].setFilter(StatValue.FILTER_NONE);
final String statName = ri.getType().getStats()[0].getName();
assertNotNull(statName);
assertEquals("long_counter_1", statName);
assertEquals(statName, statValues[0].getDescriptor().getName());
assertEquals(2, statValues[0].getSnapshotsSize());
assertEquals((double) (value1 + value2), statValues[0].getSnapshotsMostRecent(), 0.01);
final long[] timeStampsMillis = statValues[0].getRawAbsoluteTimeStamps();
assertNotNull(timeStampsMillis);
assertEquals(2, timeStampsMillis.length);
final long initialPreviousTimeStampMillis = NanoTimer.nanosToMillis(WRITER_PREVIOUS_TIMESTAMP_NANOS);
final long timeStampMillis = NanoTimer.nanosToMillis(sampleTimeNanos);
final long deltaMillis = timeStampMillis - initialPreviousTimeStampMillis;
assertEquals(NanoTimer.nanosToMillis(sampleIncNanos * 2), deltaMillis);
long expectedTimeStampMillis = WRITER_INITIAL_DATE_MILLIS;
for (int i = 0; i < timeStampsMillis.length; i++) {
expectedTimeStampMillis += 1000;
assertEquals("expectedTimeStampMillis for " + i + " is wrong", expectedTimeStampMillis, timeStampsMillis[i]);
}
final double[] snapshots = statValues[0].getRawSnapshots();
assertNotNull(snapshots);
assertEquals(2, snapshots.length);
assertEquals((double) value1, snapshots[0], 0.01);
assertEquals((double) (value1 + value2), snapshots[1], 0.01);
}
use of org.apache.geode.internal.statistics.TestStatArchiveWriter in project geode by apache.
the class StatArchiveWriterReaderIntegrationTest method testNegativeSampleTimeStamp.
/**
* Validates that IllegalArgumentException is thrown if sample time stamp is before the previous
* time stamp. Tests the fix for cause of bug #45268.
*/
@Test
public void testNegativeSampleTimeStamp() throws Exception {
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);
final long sampleIncNanos = NANOS_PER_MILLI * 1000;
try {
writer.sampled(WRITER_PREVIOUS_TIMESTAMP_NANOS - sampleIncNanos, Collections.emptyList());
fail("Expected IllegalArgumentException to be thrown from StatArchiveWriter#writeTimeStamp(long)");
} catch (IllegalArgumentException expected) {
// test passed
} finally {
writer.close();
}
}
use of org.apache.geode.internal.statistics.TestStatArchiveWriter in project geode by apache.
the class StatArchiveWriterReaderIntegrationTest method testIntGaugeOneSample.
@Test
public void testIntGaugeOneSample() 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.createIntGauge("int_gauge_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_gauge_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_gauge_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 initialPreviousTimeStampMillis = NanoTimer.nanosToMillis(WRITER_PREVIOUS_TIMESTAMP_NANOS);
final long timeStampMillis = NanoTimer.nanosToMillis(sampleTimeNanos);
final long deltaMillis = timeStampMillis - initialPreviousTimeStampMillis;
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);
}
use of org.apache.geode.internal.statistics.TestStatArchiveWriter in project geode by apache.
the class StatArchiveWriterReaderIntegrationTest method testPreviousSampleTimeStamp.
/**
* Validates that IllegalArgumentException is thrown if sample time stamp is same as the previous
* time stamp. Tests the fix for cause of bug #45268.
*/
@Test
public void testPreviousSampleTimeStamp() throws Exception {
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);
try {
writer.sampled(WRITER_PREVIOUS_TIMESTAMP_NANOS, Collections.emptyList());
fail("Expected IllegalArgumentException to be thrown from StatArchiveWriter#writeTimeStamp(long)");
} catch (IllegalArgumentException expected) {
// test passed
} finally {
writer.close();
}
writer.close();
}
use of org.apache.geode.internal.statistics.TestStatArchiveWriter 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);
}
Aggregations