use of org.apache.geode.Statistics in project geode by apache.
the class GemFireStatSamplerIntegrationTest method testLocalStatListener.
/**
* Adds a LocalStatListener for an individual stat. Validates that it receives notifications.
* Removes the listener and validates that it was in fact removed and no longer receives
* notifications.
*/
@Test
public void testLocalStatListener() throws Exception {
connect(createGemFireProperties());
GemFireStatSampler statSampler = getGemFireStatSampler();
assertTrue(statSampler.waitForInitialization(5000));
Method getLocalListeners = getGemFireStatSampler().getClass().getMethod("getLocalListeners");
assertNotNull(getLocalListeners);
Method addLocalStatListener = getGemFireStatSampler().getClass().getMethod("addLocalStatListener", LocalStatListener.class, Statistics.class, String.class);
assertNotNull(addLocalStatListener);
Method removeLocalStatListener = getGemFireStatSampler().getClass().getMethod("removeLocalStatListener", LocalStatListener.class);
assertNotNull(removeLocalStatListener);
// validate that there are no listeners
assertTrue(statSampler.getLocalListeners().isEmpty());
// add a listener for sampleCount stat in StatSampler statistics
StatisticsType statSamplerType = getStatisticsManager().findType("StatSampler");
Statistics[] statsArray = getStatisticsManager().findStatisticsByType(statSamplerType);
assertEquals(1, statsArray.length);
final Statistics statSamplerStats = statsArray[0];
final String statName = "sampleCount";
final AtomicInteger sampleCountValue = new AtomicInteger(0);
final AtomicInteger sampleCountChanged = new AtomicInteger(0);
LocalStatListener listener = new LocalStatListener() {
public void statValueChanged(double value) {
sampleCountValue.set((int) value);
sampleCountChanged.incrementAndGet();
}
};
statSampler.addLocalStatListener(listener, statSamplerStats, statName);
assertTrue(statSampler.getLocalListeners().size() == 1);
// there's a level of indirection here and some protected member fields
LocalStatListenerImpl lsli = (LocalStatListenerImpl) statSampler.getLocalListeners().iterator().next();
assertEquals("sampleCount", lsli.stat.getName());
// wait for the listener to update 4 times
final int expectedChanges = 4;
boolean done = false;
try {
for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < 5000; done = (sampleCountChanged.get() >= expectedChanges)) {
Thread.sleep(10);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
assertTrue("Waiting for sampleCountChanged >= " + expectedChanges, done);
// validate that the listener fired and updated the value
assertTrue(sampleCountValue.get() > 0);
assertTrue(sampleCountChanged.get() >= expectedChanges);
// remove the listener
statSampler.removeLocalStatListener(listener);
final int expectedSampleCountValue = sampleCountValue.get();
final int expectedSampleCountChanged = sampleCountChanged.get();
// validate that there are no listeners now
assertTrue(statSampler.getLocalListeners().isEmpty());
// wait for 2 stat samples to occur
waitForStatSample(statSamplerStats, expectedSampleCountValue, 5000, 10);
// validate that the listener did not fire
assertEquals(expectedSampleCountValue, sampleCountValue.get());
assertEquals(expectedSampleCountChanged, sampleCountChanged.get());
}
use of org.apache.geode.Statistics in project geode by apache.
the class DistributedSystemStatisticsIntegrationTest method testDoubleStatistics.
/**
* Tests {@code double} statistics
*/
@Test
public void testDoubleStatistics() {
Statistics stats = setUpDoubleStatistics(3);
// Set/get some random double values
for (int i = 0; i < 100; i++) {
for (int j = 0; j < this.statNames.length; j++) {
String statName = this.statNames[j];
double value = this.random.nextDouble();
stats.setDouble(statName, value);
assertThat(stats.getDouble(statName)).isEqualTo(value);
}
}
// Increment by some random values
for (int i = 0; i < 100; i++) {
for (int j = 0; j < this.statNames.length; j++) {
String statName = this.statNames[j];
double inc = this.random.nextDouble();
double before = stats.getDouble(statName);
stats.incDouble(statName, inc);
assertThat(stats.getDouble(statName)).isEqualTo(before + inc);
}
}
}
use of org.apache.geode.Statistics in project geode by apache.
the class GemFireStatSamplerIntegrationTest method testLocalStatListenerRegistration.
@Test
public void testLocalStatListenerRegistration() throws Exception {
connect(createGemFireProperties());
final GemFireStatSampler statSampler = getGemFireStatSampler();
statSampler.waitForInitialization(5000);
final AtomicBoolean flag = new AtomicBoolean(false);
final LocalStatListener listener = new LocalStatListener() {
public void statValueChanged(double value) {
flag.set(true);
}
};
final String tenuredPoolName = HeapMemoryMonitor.getTenuredMemoryPoolMXBean().getName();
logger.info("TenuredPoolName: {}", tenuredPoolName);
final List<Statistics> list = ((StatisticsManager) this.system).getStatsList();
assertFalse(list.isEmpty());
boolean done = false;
try {
for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < 5000; ) {
Thread.sleep(10);
int i = 0;
synchronized (list) {
for (Object obj : list) {
++i;
logger.info("List:{}:{}", i, obj);
if (obj instanceof StatisticsImpl) {
StatisticsImpl si = (StatisticsImpl) obj;
logger.info("stat:{}", si.getTextId());
if (si.getTextId().contains(tenuredPoolName)) {
statSampler.addLocalStatListener(listener, si, "currentUsedMemory");
done = true;
}
}
}
}
// done = false;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
assertTrue("Waiting for " + tenuredPoolName + " statistics to be added to create listener for", done);
assertTrue("expected at least one stat listener, found " + statSampler.getLocalListeners().size(), statSampler.getLocalListeners().size() > 0);
long maxTenuredMemory = HeapMemoryMonitor.getTenuredMemoryPoolMXBean().getUsage().getMax();
// byte[] bytes = new byte[1024 * 1024 * 10];
byte[] bytes = new byte[(int) (maxTenuredMemory * 0.01)];
Arrays.fill(bytes, Byte.MAX_VALUE);
done = false;
try {
for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < 5000; done = (flag.get())) {
Thread.sleep(10);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
assertTrue("Waiting for listener to set flag to true", done);
}
use of org.apache.geode.Statistics 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.Statistics 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);
}
Aggregations