use of org.apache.geode.Statistics in project geode by apache.
the class SampleCollectorTest method testAddHandlerAfterSamples.
@Test
public void testAddHandlerAfterSamples() {
StatisticDescriptor[] statsST1 = new StatisticDescriptor[] { manager.createIntCounter("ST1_1_name", "ST1_1_desc", "ST1_1_units") };
StatisticsType ST1 = manager.createType("ST1_name", "ST1_desc", statsST1);
Statistics st1_1 = manager.createAtomicStatistics(ST1, "st1_1_text", 1);
Statistics st1_2 = manager.createAtomicStatistics(ST1, "st1_2_text", 1);
StatisticDescriptor[] statsST2 = new StatisticDescriptor[] { manager.createIntCounter("ST2_1_name", "ST2_1_desc", "ST2_1_units") };
StatisticsType ST2 = manager.createType("ST2_name", "ST2_desc", statsST2);
Statistics st2_1 = manager.createAtomicStatistics(ST2, "st2_1_text", 1);
st1_1.incInt("ST1_1_name", 1);
st1_2.incInt("ST1_1_name", 1);
st2_1.incInt("ST2_1_name", 1);
long sampleTime = NanoTimer.getTime();
this.sampleCollector.sample(sampleTime);
st1_1.incInt("ST1_1_name", 2);
st2_1.incInt("ST2_1_name", 1);
sampleTime += NanoTimer.millisToNanos(1000);
this.sampleCollector.sample(sampleTime);
st1_1.incInt("ST1_1_name", 1);
st1_1.incInt("ST1_1_name", 2);
sampleTime += NanoTimer.millisToNanos(1000);
this.sampleCollector.sample(sampleTime);
TestSampleHandler handler = new TestSampleHandler();
this.sampleCollector.addSampleHandler(handler);
assertEquals("TestSampleHandler = " + handler, 0, handler.getNotificationCount());
st1_2.incInt("ST1_1_name", 1);
st2_1.incInt("ST2_1_name", 1);
sampleTime += NanoTimer.millisToNanos(1000);
this.sampleCollector.sample(sampleTime);
assertEquals(6, handler.getNotificationCount());
List<Info> notifications = handler.getNotifications();
// validate the allocatedResourceType notification for ST1
int notificationIdx = 0;
assertTrue(notifications.get(notificationIdx) instanceof ResourceTypeInfo);
ResourceTypeInfo allocatedResourceTypeInfo = (ResourceTypeInfo) notifications.get(notificationIdx);
assertNotNull(allocatedResourceTypeInfo);
assertEquals("allocatedResourceType", allocatedResourceTypeInfo.getName());
ResourceType resourceType = allocatedResourceTypeInfo.getResourceType();
assertNotNull(resourceType);
assertEquals(0, resourceType.getId());
assertEquals(1, resourceType.getStatisticDescriptors().length);
StatisticsType statisticsType = resourceType.getStatisticsType();
assertNotNull(statisticsType);
assertTrue(statisticsType == ST1);
assertEquals("ST1_name", statisticsType.getName());
assertEquals("ST1_desc", statisticsType.getDescription());
assertEquals(1, statisticsType.getStatistics().length);
// validate the allocatedResourceInstance notification for st1_1
notificationIdx++;
assertTrue(notifications.get(notificationIdx) instanceof ResourceInstanceInfo);
ResourceInstanceInfo allocatedResourceInstanceInfo = (ResourceInstanceInfo) notifications.get(notificationIdx);
assertNotNull(allocatedResourceInstanceInfo);
assertEquals("allocatedResourceInstance", allocatedResourceInstanceInfo.getName());
ResourceInstance resourceInstance = allocatedResourceInstanceInfo.getResourceInstance();
assertNotNull(resourceInstance);
assertEquals(0, resourceInstance.getId());
assertEquals(0, resourceInstance.getUpdatedStats().length);
// TODO: is this correct?
assertEquals(1, resourceInstance.getLatestStatValues().length);
Statistics statistics = resourceInstance.getStatistics();
assertNotNull(statistics);
assertTrue(statistics == st1_1);
assertEquals(1, statistics.getUniqueId());
assertEquals(1, statistics.getNumericId());
assertEquals("st1_1_text", statistics.getTextId());
assertEquals("ST1_name", statistics.getType().getName());
assertTrue(resourceType == resourceInstance.getResourceType());
// validate the allocatedResourceInstance notification for st1_2
notificationIdx++;
assertTrue(notifications.get(notificationIdx) instanceof ResourceInstanceInfo);
allocatedResourceInstanceInfo = (ResourceInstanceInfo) notifications.get(notificationIdx);
assertNotNull(allocatedResourceInstanceInfo);
assertEquals("allocatedResourceInstance", allocatedResourceInstanceInfo.getName());
resourceInstance = allocatedResourceInstanceInfo.getResourceInstance();
assertNotNull(resourceInstance);
assertEquals(1, resourceInstance.getId());
assertEquals(1, resourceInstance.getUpdatedStats().length);
// TODO: is this correct?
assertEquals(1, resourceInstance.getLatestStatValues().length);
statistics = resourceInstance.getStatistics();
assertNotNull(statistics);
assertTrue(statistics == st1_2);
assertEquals(2, statistics.getUniqueId());
assertEquals(1, statistics.getNumericId());
assertEquals("st1_2_text", statistics.getTextId());
assertEquals("ST1_name", statistics.getType().getName());
assertTrue(resourceType == resourceInstance.getResourceType());
// validate the allocatedResourceType notification for ST2
notificationIdx++;
assertTrue(notifications.get(notificationIdx) instanceof ResourceTypeInfo);
allocatedResourceTypeInfo = (ResourceTypeInfo) notifications.get(notificationIdx);
assertNotNull(allocatedResourceTypeInfo);
assertEquals("allocatedResourceType", allocatedResourceTypeInfo.getName());
resourceType = allocatedResourceTypeInfo.getResourceType();
assertNotNull(resourceType);
assertEquals(1, resourceType.getId());
assertEquals(1, resourceType.getStatisticDescriptors().length);
statisticsType = resourceType.getStatisticsType();
assertNotNull(statisticsType);
assertTrue(statisticsType == ST2);
assertEquals("ST2_name", statisticsType.getName());
assertEquals("ST2_desc", statisticsType.getDescription());
assertEquals(1, statisticsType.getStatistics().length);
// validate the allocatedResourceInstance notification for st2_1
notificationIdx++;
assertTrue(notifications.get(notificationIdx) instanceof ResourceInstanceInfo);
allocatedResourceInstanceInfo = (ResourceInstanceInfo) notifications.get(notificationIdx);
assertNotNull(allocatedResourceInstanceInfo);
assertEquals("allocatedResourceInstance", allocatedResourceInstanceInfo.getName());
resourceInstance = allocatedResourceInstanceInfo.getResourceInstance();
assertNotNull(resourceInstance);
assertEquals(2, resourceInstance.getId());
assertEquals(1, resourceInstance.getUpdatedStats().length);
// TODO: is this correct?
assertEquals(1, resourceInstance.getLatestStatValues().length);
statistics = resourceInstance.getStatistics();
assertNotNull(statistics);
assertTrue(statistics == st2_1);
assertEquals(3, statistics.getUniqueId());
assertEquals(1, statistics.getNumericId());
assertEquals("st2_1_text", statistics.getTextId());
assertEquals("ST2_name", statistics.getType().getName());
assertTrue(resourceType == resourceInstance.getResourceType());
// validate the sampled notification
notificationIdx++;
assertTrue(notifications.get(notificationIdx) instanceof SampledInfo);
SampledInfo sampledInfo = (SampledInfo) notifications.get(notificationIdx);
assertNotNull(sampledInfo);
assertEquals("sampled", sampledInfo.getName());
assertEquals(3, sampledInfo.getResourceCount());
}
use of org.apache.geode.Statistics in project geode by apache.
the class SimpleStatSamplerIntegrationTest method testStop.
/**
* Invokes stop() and then validates that the sampler did in fact stop.
*/
@Test
public void testStop() throws Exception {
initStatisticsFactory();
SimpleStatSampler statSampler = getSimpleStatSampler();
assertTrue(statSampler.waitForInitialization(5000));
// validate the stat sampler is running
StatisticsType statSamplerType = getStatisticsManager().findType("StatSampler");
Statistics[] statsArray = getStatisticsManager().findStatisticsByType(statSamplerType);
assertEquals(1, statsArray.length);
final Statistics statSamplerStats = statsArray[0];
final int initialSampleCount = statSamplerStats.getInt("sampleCount");
final int expectedSampleCount = initialSampleCount + 2;
waitForStatSample(statSamplerStats, expectedSampleCount, 20000, 10);
// stop the stat sampler
statSampler.stop();
// validate the stat sampler has stopped
final int stoppedSampleCount = statSamplerStats.getInt("sampleCount");
// the following should timeout without completing
assertStatValueDoesNotChange(statSamplerStats, "sampleCount", stoppedSampleCount, 5000, 10);
assertEquals(stoppedSampleCount, statSamplerStats.getInt("sampleCount"));
}
use of org.apache.geode.Statistics in project geode by apache.
the class SimpleStatSamplerIntegrationTest method testSampleRate.
/**
* Tests the statistics sample rate within an acceptable margin of error.
*/
@Test
public void testSampleRate() throws Exception {
initStatisticsFactory();
SimpleStatSampler statSampler = getSimpleStatSampler();
assertTrue(statSampler.waitForInitialization(5000));
assertEquals(SimpleStatSampler.DEFAULT_SAMPLE_RATE, statSampler.getSampleRate());
assertTrue(getStatisticsManager().getStatListModCount() > 0);
List<Statistics> statistics = getStatisticsManager().getStatsList();
assertNotNull(statistics);
assertTrue(statistics.size() > 0);
StatisticsType statSamplerType = getStatisticsManager().findType("StatSampler");
Statistics[] statsArray = getStatisticsManager().findStatisticsByType(statSamplerType);
assertEquals(1, statsArray.length);
final Statistics statSamplerStats = statsArray[0];
final int initialSampleCount = statSamplerStats.getInt("sampleCount");
final int expectedSampleCount = initialSampleCount + 2;
waitForStatSample(statSamplerStats, expectedSampleCount, 20000, 10);
}
use of org.apache.geode.Statistics in project geode by apache.
the class StatArchiveWriterReaderIntegrationTest method testDestroyUnallocatedStatistics.
/**
* Control which helps verify fix for bug #45377.
*/
@Test
public void testDestroyUnallocatedStatistics() 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);
final ResourceInstance ri = new ResourceInstance(0, statistics, rt);
writer.sampled(WRITER_INITIAL_DATE_MILLIS + 1000, Collections.singletonList(ri));
writer.destroyedResourceInstance(ri);
writer.close();
// Verify StatArchiveReader.update returns cleanly, without throwing an exception
final StatArchiveReader reader = new StatArchiveReader(new File[] { new File(this.archiveFileName) }, null, false);
reader.update();
reader.close();
}
use of org.apache.geode.Statistics in project geode by apache.
the class StatArchiveWriterReaderIntegrationTest method testDoubleCounterOneSample.
@Test
public void testDoubleCounterOneSample() 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.createDoubleCounter("long_double_1", "d1", "u1") };
final StatisticsType ST1 = manager.createType("ST1", "ST1", statsST1);
final Statistics st1_1 = manager.createAtomicStatistics(ST1, "st1_1", 1);
final double value = 32317.716467;
incDouble(st1_1, "long_double_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("long_double_1", statName);
assertEquals(statName, statValues[0].getDescriptor().getName());
assertEquals(1, statValues[0].getSnapshotsSize());
assertEquals(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(value, snapshots[0], 0.01);
}
Aggregations