Search in sources :

Example 51 with Statistics

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

the class StatisticsDistributedTest method testPubAndSubCustomStats.

@Test
public void testPubAndSubCustomStats() throws Exception {
    String regionName = "region_" + getName();
    VM[] pubs = new VM[NUM_PUBS];
    for (int pubVM = 0; pubVM < NUM_PUBS; pubVM++) {
        pubs[pubVM] = getHost(0).getVM(pubVM);
    }
    VM sub = getHost(0).getVM(NUM_PUBS);
    String subArchive = this.directory.getAbsolutePath() + File.separator + getName() + "_sub" + ".gfs";
    String[] pubArchives = new String[NUM_PUBS];
    for (int pubVM = 0; pubVM < NUM_PUBS; pubVM++) {
        pubArchives[pubVM] = this.directory.getAbsolutePath() + File.separator + getName() + "_pub-" + pubVM + ".gfs";
    }
    for (int i = 0; i < NUM_PUBS; i++) {
        final int pubVM = i;
        pubs[pubVM].invoke("pub-connect-and-create-data-" + pubVM, () -> {
            Properties props = new Properties();
            props.setProperty(STATISTIC_SAMPLING_ENABLED, "true");
            props.setProperty(STATISTIC_SAMPLE_RATE, "1000");
            props.setProperty(STATISTIC_ARCHIVE_FILE, pubArchives[pubVM]);
            InternalDistributedSystem system = getSystem(props);
            // assert that sampler is working as expected
            GemFireStatSampler sampler = system.getStatSampler();
            assertTrue(sampler.isSamplingEnabled());
            assertTrue(sampler.isAlive());
            assertEquals(new File(pubArchives[pubVM]), sampler.getArchiveFileName());
            await("awaiting SampleCollector to exist").atMost(30, SECONDS).until(() -> sampler.getSampleCollector() != null);
            SampleCollector sampleCollector = sampler.getSampleCollector();
            assertNotNull(sampleCollector);
            StatArchiveHandler archiveHandler = sampleCollector.getStatArchiveHandler();
            assertNotNull(archiveHandler);
            assertTrue(archiveHandler.isArchiving());
            // create cache and region
            Cache cache = getCache();
            RegionFactory<String, Number> factory = cache.createRegionFactory();
            factory.setScope(Scope.DISTRIBUTED_ACK);
            RegionMembershipListener rml = new RegionMembershipListener();
            rmlRef.set(rml);
            factory.addCacheListener(rml);
            Region<String, Number> region = factory.create(regionName);
            // create the keys
            if (region.getAttributes().getScope() == Scope.DISTRIBUTED_ACK) {
                for (int key = 0; key < NUM_KEYS; key++) {
                    region.create("KEY-" + key, null);
                }
            }
        });
    }
    DistributedMember subMember = sub.invoke("sub-connect-and-create-keys", () -> {
        Properties props = new Properties();
        props.setProperty(STATISTIC_SAMPLING_ENABLED, "true");
        props.setProperty(STATISTIC_SAMPLE_RATE, "1000");
        props.setProperty(STATISTIC_ARCHIVE_FILE, subArchive);
        InternalDistributedSystem system = getSystem(props);
        PubSubStats statistics = new PubSubStats(system, "sub-1", 1);
        subStatsRef.set(statistics);
        // assert that sampler is working as expected
        GemFireStatSampler sampler = system.getStatSampler();
        assertTrue(sampler.isSamplingEnabled());
        assertTrue(sampler.isAlive());
        assertEquals(new File(subArchive), sampler.getArchiveFileName());
        await("awaiting SampleCollector to exist").atMost(30, SECONDS).until(() -> sampler.getSampleCollector() != null);
        SampleCollector sampleCollector = sampler.getSampleCollector();
        assertNotNull(sampleCollector);
        StatArchiveHandler archiveHandler = sampleCollector.getStatArchiveHandler();
        assertNotNull(archiveHandler);
        assertTrue(archiveHandler.isArchiving());
        // create cache and region with UpdateListener
        Cache cache = getCache();
        RegionFactory<String, Number> factory = cache.createRegionFactory();
        factory.setScope(Scope.DISTRIBUTED_ACK);
        CacheListener<String, Number> cl = new UpdateListener(statistics);
        factory.addCacheListener(cl);
        Region<String, Number> region = factory.create(regionName);
        // create the keys
        if (region.getAttributes().getScope() == Scope.DISTRIBUTED_ACK) {
            for (int key = 0; key < NUM_KEYS; key++) {
                region.create("KEY-" + key, null);
            }
        }
        assertEquals(0, statistics.getUpdateEvents());
        return system.getDistributedMember();
    });
    for (int i = 0; i < NUM_PUBS; i++) {
        final int pubVM = i;
        AsyncInvocation[] publishers = new AsyncInvocation[NUM_PUB_THREADS];
        for (int j = 0; j < NUM_PUB_THREADS; j++) {
            final int pubThread = j;
            publishers[pubThread] = pubs[pubVM].invokeAsync("pub-connect-and-put-data-" + pubVM + "-thread-" + pubThread, () -> {
                PubSubStats statistics = new PubSubStats(basicGetSystem(), "pub-" + pubThread, pubVM);
                pubStatsRef.set(pubThread, statistics);
                RegionMembershipListener rml = rmlRef.get();
                Region<String, Number> region = getCache().getRegion(regionName);
                // assert that sub is in rml membership
                assertNotNull(rml);
                await("awaiting Membership to contain subMember").atMost(30, SECONDS).until(() -> rml.contains(subMember) && rml.size() == NUM_PUBS);
                // publish lots of puts cycling through the NUM_KEYS
                assertEquals(0, statistics.getPuts());
                // cycle through the keys randomly
                if (RANDOMIZE_PUTS) {
                    Random randomGenerator = new Random();
                    int key = 0;
                    for (int idx = 0; idx < MAX_PUTS; idx++) {
                        long start = statistics.startPut();
                        key = randomGenerator.nextInt(NUM_KEYS);
                        region.put("KEY-" + key, idx);
                        statistics.endPut(start);
                    }
                // cycle through he keys in order and wrapping back around
                } else {
                    int key = 0;
                    for (int idx = 0; idx < MAX_PUTS; idx++) {
                        long start = statistics.startPut();
                        region.put("KEY-" + key, idx);
                        // cycle through the keys...
                        key++;
                        if (key >= NUM_KEYS) {
                            key = 0;
                        }
                        statistics.endPut(start);
                    }
                }
                assertEquals(MAX_PUTS, statistics.getPuts());
                // wait for 2 samples to ensure all stats have been archived
                StatisticsType statSamplerType = getSystem().findType("StatSampler");
                Statistics[] statsArray = getSystem().findStatisticsByType(statSamplerType);
                assertEquals(1, statsArray.length);
                Statistics statSamplerStats = statsArray[0];
                int initialSampleCount = statSamplerStats.getInt(StatSamplerStats.SAMPLE_COUNT);
                await("awaiting sampleCount >= 2").atMost(30, SECONDS).until(() -> statSamplerStats.getInt(StatSamplerStats.SAMPLE_COUNT) >= initialSampleCount + 2);
            });
        }
        for (int pubThread = 0; pubThread < publishers.length; pubThread++) {
            publishers[pubThread].join();
            if (publishers[pubThread].exceptionOccurred()) {
                fail("Test failed", publishers[pubThread].getException());
            }
        }
    }
    sub.invoke("sub-wait-for-samples", () -> {
        // wait for 2 samples to ensure all stats have been archived
        StatisticsType statSamplerType = getSystem().findType("StatSampler");
        Statistics[] statsArray = getSystem().findStatisticsByType(statSamplerType);
        assertEquals(1, statsArray.length);
        Statistics statSamplerStats = statsArray[0];
        int initialSampleCount = statSamplerStats.getInt(StatSamplerStats.SAMPLE_COUNT);
        await("awaiting sampleCount >= 2").atMost(30, SECONDS).until(() -> statSamplerStats.getInt(StatSamplerStats.SAMPLE_COUNT) >= initialSampleCount + 2);
        // now post total updateEvents to static
        PubSubStats statistics = subStatsRef.get();
        assertNotNull(statistics);
        updateEvents.set(statistics.getUpdateEvents());
    });
    // validate pub values against sub values
    int totalUpdateEvents = sub.invoke(() -> getUpdateEvents());
    // validate pub values against pub statistics against pub archive
    for (int i = 0; i < NUM_PUBS; i++) {
        final int pubIdx = i;
        pubs[pubIdx].invoke("pub-validation", () -> {
            // add up all the puts
            assertEquals(NUM_PUB_THREADS, pubStatsRef.length());
            int totalPuts = 0;
            for (int pubThreadIdx = 0; pubThreadIdx < NUM_PUB_THREADS; pubThreadIdx++) {
                PubSubStats statistics = pubStatsRef.get(pubThreadIdx);
                assertNotNull(statistics);
                totalPuts += statistics.getPuts();
            }
            // assert that total puts adds up to max puts times num threads
            assertEquals(MAX_PUTS * NUM_PUB_THREADS, totalPuts);
            // assert that archive file contains same values as statistics
            File archive = new File(pubArchives[pubIdx]);
            assertTrue(archive.exists());
            StatArchiveReader reader = new StatArchiveReader(new File[] { archive }, null, false);
            double combinedPuts = 0;
            List resources = reader.getResourceInstList();
            assertNotNull(resources);
            assertFalse(resources.isEmpty());
            for (Iterator<ResourceInst> iter = resources.iterator(); iter.hasNext(); ) {
                ResourceInst ri = iter.next();
                if (!ri.getType().getName().equals(PubSubStats.TYPE_NAME)) {
                    continue;
                }
                StatValue[] statValues = ri.getStatValues();
                for (int idx = 0; idx < statValues.length; idx++) {
                    String statName = ri.getType().getStats()[idx].getName();
                    assertNotNull(statName);
                    if (statName.equals(PubSubStats.PUTS)) {
                        StatValue sv = statValues[idx];
                        sv.setFilter(StatValue.FILTER_NONE);
                        double mostRecent = sv.getSnapshotsMostRecent();
                        double min = sv.getSnapshotsMinimum();
                        double max = sv.getSnapshotsMaximum();
                        double maxMinusMin = sv.getSnapshotsMaximum() - sv.getSnapshotsMinimum();
                        double mean = sv.getSnapshotsAverage();
                        double stdDev = sv.getSnapshotsStandardDeviation();
                        assertEquals(mostRecent, max, 0f);
                        double summation = 0;
                        double[] rawSnapshots = sv.getRawSnapshots();
                        for (int j = 0; j < rawSnapshots.length; j++) {
                            summation += rawSnapshots[j];
                        }
                        assertEquals(mean, summation / sv.getSnapshotsSize(), 0);
                        combinedPuts += mostRecent;
                    }
                }
            }
            // assert that sum of mostRecent values for all puts equals totalPuts
            assertEquals((double) totalPuts, combinedPuts, 0);
            puts.getAndAdd(totalPuts);
        });
    }
    // validate pub values against sub values
    int totalCombinedPuts = 0;
    for (int i = 0; i < NUM_PUBS; i++) {
        int pubIdx = i;
        int totalPuts = pubs[pubIdx].invoke(() -> getPuts());
        assertEquals(MAX_PUTS * NUM_PUB_THREADS, totalPuts);
        totalCombinedPuts += totalPuts;
    }
    assertEquals(totalCombinedPuts, totalUpdateEvents);
    assertEquals(MAX_PUTS * NUM_PUB_THREADS * NUM_PUBS, totalCombinedPuts);
    // validate sub values against sub statistics against sub archive
    final int totalPuts = totalCombinedPuts;
    sub.invoke("sub-validation", () -> {
        PubSubStats statistics = subStatsRef.get();
        assertNotNull(statistics);
        int updateEvents = statistics.getUpdateEvents();
        assertEquals(totalPuts, updateEvents);
        assertEquals(totalUpdateEvents, updateEvents);
        assertEquals(MAX_PUTS * NUM_PUB_THREADS * NUM_PUBS, updateEvents);
        // assert that archive file contains same values as statistics
        File archive = new File(subArchive);
        assertTrue(archive.exists());
        StatArchiveReader reader = new StatArchiveReader(new File[] { archive }, null, false);
        double combinedUpdateEvents = 0;
        List resources = reader.getResourceInstList();
        for (Iterator<ResourceInst> iter = resources.iterator(); iter.hasNext(); ) {
            ResourceInst ri = iter.next();
            if (!ri.getType().getName().equals(PubSubStats.TYPE_NAME)) {
                continue;
            }
            StatValue[] statValues = ri.getStatValues();
            for (int i = 0; i < statValues.length; i++) {
                String statName = ri.getType().getStats()[i].getName();
                assertNotNull(statName);
                if (statName.equals(PubSubStats.UPDATE_EVENTS)) {
                    StatValue sv = statValues[i];
                    sv.setFilter(StatValue.FILTER_NONE);
                    double mostRecent = sv.getSnapshotsMostRecent();
                    double min = sv.getSnapshotsMinimum();
                    double max = sv.getSnapshotsMaximum();
                    double maxMinusMin = sv.getSnapshotsMaximum() - sv.getSnapshotsMinimum();
                    double mean = sv.getSnapshotsAverage();
                    double stdDev = sv.getSnapshotsStandardDeviation();
                    assertEquals(mostRecent, max, 0);
                    double summation = 0;
                    double[] rawSnapshots = sv.getRawSnapshots();
                    for (int j = 0; j < rawSnapshots.length; j++) {
                        summation += rawSnapshots[j];
                    }
                    assertEquals(mean, summation / sv.getSnapshotsSize(), 0);
                    combinedUpdateEvents += mostRecent;
                }
            }
        }
        assertEquals((double) totalUpdateEvents, combinedUpdateEvents, 0);
    });
    int updateEvents = sub.invoke(() -> readIntStat(new File(subArchive), "PubSubStats", "updateEvents"));
    assertTrue(updateEvents > 0);
    assertEquals(MAX_PUTS * NUM_PUB_THREADS * NUM_PUBS, updateEvents);
    int puts = 0;
    for (int pubVM = 0; pubVM < NUM_PUBS; pubVM++) {
        int currentPubVM = pubVM;
        int vmPuts = pubs[pubVM].invoke(() -> readIntStat(new File(pubArchives[currentPubVM]), "PubSubStats", "puts"));
        assertTrue(vmPuts > 0);
        assertEquals(MAX_PUTS * NUM_PUB_THREADS, vmPuts);
        puts += vmPuts;
    }
    assertTrue(puts > 0);
    assertEquals(MAX_PUTS * NUM_PUB_THREADS * NUM_PUBS, puts);
    // use regex "testPubAndSubCustomStats"
    MultipleArchiveReader reader = new MultipleArchiveReader(this.directory, ".*" + getTestMethodName() + ".*\\.gfs");
    int combinedUpdateEvents = reader.readIntStat(PubSubStats.TYPE_NAME, PubSubStats.UPDATE_EVENTS);
    assertTrue("Failed to read updateEvents stat values", combinedUpdateEvents > 0);
    int combinedPuts = reader.readIntStat(PubSubStats.TYPE_NAME, PubSubStats.PUTS);
    assertTrue("Failed to read puts stat values", combinedPuts > 0);
    assertTrue("updateEvents is " + combinedUpdateEvents + " but puts is " + combinedPuts, combinedUpdateEvents == combinedPuts);
}
Also used : ResourceInst(org.apache.geode.internal.statistics.StatArchiveReader.ResourceInst) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) StatValue(org.apache.geode.internal.statistics.StatArchiveReader.StatValue) Random(java.util.Random) List(java.util.List) ArrayList(java.util.ArrayList) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) StatisticsType(org.apache.geode.StatisticsType) Statistics(org.apache.geode.Statistics) VM(org.apache.geode.test.dunit.VM) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) File(java.io.File) Cache(org.apache.geode.cache.Cache) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 52 with Statistics

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

the class ValueMonitorIntegrationTest method testValueMonitorListener.

@Test
public void testValueMonitorListener() throws Exception {
    long startTime = System.currentTimeMillis();
    TestStatisticsManager manager = new TestStatisticsManager(1, "ValueMonitorIntegrationTest", startTime);
    StatisticsSampler sampler = new TestStatisticsSampler(manager);
    StatArchiveHandlerConfig mockStatArchiveHandlerConfig = this.mockContext.mock(StatArchiveHandlerConfig.class, testName.getMethodName() + "$StatArchiveHandlerConfig");
    this.mockContext.checking(new Expectations() {

        {
            allowing(mockStatArchiveHandlerConfig).getArchiveFileName();
            will(returnValue(new File("")));
            allowing(mockStatArchiveHandlerConfig).getArchiveFileSizeLimit();
            will(returnValue(0));
            allowing(mockStatArchiveHandlerConfig).getArchiveDiskSpaceLimit();
            will(returnValue(0));
            allowing(mockStatArchiveHandlerConfig).getSystemId();
            will(returnValue(1));
            allowing(mockStatArchiveHandlerConfig).getSystemStartTime();
            will(returnValue(startTime));
            allowing(mockStatArchiveHandlerConfig).getSystemDirectoryPath();
            will(returnValue(""));
            allowing(mockStatArchiveHandlerConfig).getProductDescription();
            will(returnValue("testFoo"));
        }
    });
    SampleCollector sampleCollector = new SampleCollector(sampler);
    sampleCollector.initialize(mockStatArchiveHandlerConfig, NanoTimer.getTime(), new MainWithChildrenRollingFileHandler());
    StatisticDescriptor[] statsST1 = new StatisticDescriptor[] { manager.createDoubleCounter("double_counter_1", "double_counter_1_desc", "double_counter_1_units"), manager.createIntCounter("int_counter_2", "int_counter_2_desc", "int_counter_2_units"), manager.createLongCounter("long_counter_3", "long_counter_3_desc", "long_counter_3_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", 2);
    st1_1.incDouble("double_counter_1", 1000.0001);
    st1_1.incInt("int_counter_2", 2);
    st1_1.incLong("long_counter_3", 3333333333L);
    st1_2.incDouble("double_counter_1", 2000.0002);
    st1_2.incInt("int_counter_2", 3);
    st1_2.incLong("long_counter_3", 4444444444L);
    List<StatisticsNotification> notifications = new ArrayList<>();
    StatisticsListener listener = (final StatisticsNotification notification) -> {
        notifications.add(notification);
    };
    ValueMonitor monitor = new ValueMonitor().addStatistics(st1_1);
    monitor.addListener(listener);
    assertTrue("Unexpected notifications: " + notifications, notifications.isEmpty());
    long timeStamp = NanoTimer.getTime();
    sampleCollector.sample(timeStamp);
    awaitAtLeastTimeoutOrUntilNotifications(notifications, 2 * 1000);
    assertEquals("Unexpected notifications: " + notifications, 1, notifications.size());
    StatisticsNotification notification = notifications.remove(0);
    assertEquals(StatisticsNotification.Type.VALUE_CHANGED, notification.getType());
    // validate 1 notification occurs with all 3 stats of st1_1
    st1_1.incDouble("double_counter_1", 1.1);
    st1_1.incInt("int_counter_2", 2);
    st1_1.incLong("long_counter_3", 3);
    timeStamp += NanoTimer.millisToNanos(1000);
    sampleCollector.sample(timeStamp);
    awaitAtLeastTimeoutOrUntilNotifications(notifications, 2 * 1000);
    assertEquals("Unexpected notifications: " + notifications, 1, notifications.size());
    notification = notifications.remove(0);
    assertEquals(StatisticsNotification.Type.VALUE_CHANGED, notification.getType());
    int statCount = 0;
    Map<String, Number> expectedValues = new HashMap<>();
    expectedValues.put("double_counter_1", 1001.1001);
    expectedValues.put("int_counter_2", 4);
    expectedValues.put("long_counter_3", 3333333336L);
    for (StatisticId statId : notification) {
        Number value = expectedValues.remove(statId.getStatisticDescriptor().getName());
        assertNotNull(value);
        assertEquals(value, notification.getValue(statId));
        statCount++;
    }
    assertEquals(3, statCount);
    // validate no notification occurs when no stats are updated
    timeStamp += NanoTimer.millisToNanos(1000);
    sampleCollector.sample(timeStamp);
    awaitAtLeastTimeoutOrUntilNotifications(notifications, 2 * 1000);
    assertTrue("Unexpected notifications: " + notifications, notifications.isEmpty());
    // validate no notification occurs when only other stats are updated
    st1_2.incDouble("double_counter_1", 3.3);
    st1_2.incInt("int_counter_2", 1);
    st1_2.incLong("long_counter_3", 2);
    timeStamp += NanoTimer.millisToNanos(1000);
    sampleCollector.sample(timeStamp);
    awaitAtLeastTimeoutOrUntilNotifications(notifications, 2 * 1000);
    assertTrue("Unexpected notifications: " + notifications, notifications.isEmpty());
    // validate notification only contains stats added to monitor
    st1_1.incInt("int_counter_2", 100);
    st1_2.incInt("int_counter_2", 200);
    assertEquals(2, sampleCollector.currentHandlersForTesting().size());
    timeStamp += NanoTimer.millisToNanos(1000);
    sampleCollector.sample(timeStamp);
    awaitAtLeastTimeoutOrUntilNotifications(notifications, 2 * 1000);
    assertEquals("Unexpected notifications: " + notifications, 1, notifications.size());
    notification = notifications.remove(0);
    assertEquals(StatisticsNotification.Type.VALUE_CHANGED, notification.getType());
    statCount = 0;
    expectedValues = new HashMap<>();
    expectedValues.put("int_counter_2", 104);
    for (StatisticId statId : notification) {
        Number value = expectedValues.remove(statId.getStatisticDescriptor().getName());
        assertNotNull(value);
        assertEquals(value, notification.getValue(statId));
        statCount++;
    }
    assertEquals(1, statCount);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Expectations(org.jmock.Expectations) StatisticsType(org.apache.geode.StatisticsType) MainWithChildrenRollingFileHandler(org.apache.geode.internal.io.MainWithChildrenRollingFileHandler) 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 53 with Statistics

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

the class ValueMonitorIntegrationTest method testAddRemoveListener.

@Test
public void testAddRemoveListener() throws Exception {
    long startTime = System.currentTimeMillis();
    List<Statistics> statsList = new ArrayList<Statistics>();
    StatisticsManager mockStatisticsManager = this.mockContext.mock(StatisticsManager.class, testName.getMethodName() + "$StatisticsManager");
    this.mockContext.checking(new Expectations() {

        {
            allowing(mockStatisticsManager).getName();
            will(returnValue("mockStatisticsManager"));
            allowing(mockStatisticsManager).getId();
            will(returnValue(1));
            allowing(mockStatisticsManager).getStartTime();
            will(returnValue(startTime));
            allowing(mockStatisticsManager).getStatListModCount();
            will(returnValue(0));
            allowing(mockStatisticsManager).getStatsList();
            will(returnValue(statsList));
        }
    });
    StatisticsSampler mockStatisticsSampler = this.mockContext.mock(StatisticsSampler.class, testName.getMethodName() + "$StatisticsSampler");
    this.mockContext.checking(new Expectations() {

        {
            allowing(mockStatisticsSampler).getStatisticsModCount();
            will(returnValue(0));
            allowing(mockStatisticsSampler).getStatistics();
            will(returnValue(new Statistics[] {}));
        }
    });
    StatArchiveHandlerConfig mockStatArchiveHandlerConfig = this.mockContext.mock(StatArchiveHandlerConfig.class, testName.getMethodName() + "$StatArchiveHandlerConfig");
    this.mockContext.checking(new Expectations() {

        {
            allowing(mockStatArchiveHandlerConfig).getArchiveFileName();
            will(returnValue(new File("")));
            allowing(mockStatArchiveHandlerConfig).getArchiveFileSizeLimit();
            will(returnValue(0));
            allowing(mockStatArchiveHandlerConfig).getArchiveDiskSpaceLimit();
            will(returnValue(0));
            allowing(mockStatArchiveHandlerConfig).getSystemId();
            will(returnValue(1));
            allowing(mockStatArchiveHandlerConfig).getSystemStartTime();
            will(returnValue(startTime));
            allowing(mockStatArchiveHandlerConfig).getSystemDirectoryPath();
            will(returnValue(""));
            allowing(mockStatArchiveHandlerConfig).getProductDescription();
            will(returnValue("testAddRemoveListener"));
        }
    });
    // need a real SampleCollector for this test or the monitor can't get the handler
    SampleCollector sampleCollector = new SampleCollector(mockStatisticsSampler);
    sampleCollector.initialize(mockStatArchiveHandlerConfig, NanoTimer.getTime(), new MainWithChildrenRollingFileHandler());
    List<StatisticsNotification> notifications = new ArrayList<>();
    StatisticsListener listener = (final StatisticsNotification notification) -> {
        notifications.add(notification);
    };
    ValueMonitor monitor = new ValueMonitor();
    long timeStamp = System.currentTimeMillis();
    Type type = Type.VALUE_CHANGED;
    Number value = 43;
    StatisticsNotification notification = createStatisticsNotification(timeStamp, type, value);
    monitor.notifyListeners(notification);
    assertTrue(notifications.isEmpty());
    monitor.addListener(listener);
    monitor.notifyListeners(notification);
    assertEquals(1, notifications.size());
    notification = notifications.remove(0);
    assertNotNull(notification);
    assertEquals(timeStamp, notification.getTimeStamp());
    assertEquals(type, notification.getType());
    StatisticId statId = createStatisticId(null, null);
    assertEquals(value, notification.getValue(statId));
    monitor.removeListener(listener);
    monitor.notifyListeners(notification);
    assertTrue(notifications.isEmpty());
}
Also used : Expectations(org.jmock.Expectations) ArrayList(java.util.ArrayList) MainWithChildrenRollingFileHandler(org.apache.geode.internal.io.MainWithChildrenRollingFileHandler) Statistics(org.apache.geode.Statistics) StatisticsType(org.apache.geode.StatisticsType) Type(org.apache.geode.internal.statistics.StatisticsNotification.Type) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 54 with Statistics

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

the class StatArchiveWithConsecutiveResourceInstGenerator method generateStatArchiveFile.

@Test
public void generateStatArchiveFile() throws Exception {
    long sampleTimeNanos = WRITER_PREVIOUS_TIMESTAMP_NANOS + NANOS_PER_MILLI * 1000;
    // 1) create statistics
    StatisticsType type = createStatisticsType(STATS_TYPE_NAME, "description of " + STATS_TYPE_NAME);
    Statistics statistics1 = createStatistics(type, STATS_TYPE_NAME + "1", 1);
    for (int i = 0; i < 100; i++) {
        incInt(statistics1, "stat", 1);
        this.sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    }
    // 3) close statistics
    statistics1.close();
    // 4) recreate statistics
    Statistics statistics2 = createStatistics(type, STATS_TYPE_NAME + "1", 1);
    for (int i = 0; i < 100; i++) {
        incInt(statistics2, "stat", 1);
        this.sampleCollector.sample(sampleTimeNanos += (1000 * NANOS_PER_MILLI));
    }
    // close the writer
    this.writer.close();
    // validate that stat archive file exists
    File actual = new File(this.archiveFileName);
    assertTrue(actual.exists());
    // validate content of stat archive file using StatArchiveReader
    StatArchiveReader reader = new StatArchiveReader(new File[] { actual }, null, false);
    for (final Iterator iter = reader.getResourceInstList().iterator(); iter.hasNext(); ) {
        StatArchiveReader.ResourceInst ri = (StatArchiveReader.ResourceInst) iter.next();
        String resourceName = ri.getName();
        assertNotNull(resourceName);
        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++) {
            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);
            double[] rawSnapshots = statValues[i].getRawSnapshots();
            assertEquals("Value " + i + " for " + statName + " is wrong: " + expectedStatValues, expectedStatValues.get(statName).doubleValue(), statValues[i].getSnapshotsMostRecent(), 0.01);
        }
    }
    validateArchiveFile();
}
Also used : StatisticsType(org.apache.geode.StatisticsType) ResourceInst(org.apache.geode.internal.statistics.StatArchiveReader.ResourceInst) ResourceInst(org.apache.geode.internal.statistics.StatArchiveReader.ResourceInst) Statistics(org.apache.geode.Statistics) StatValue(org.apache.geode.internal.statistics.StatArchiveReader.StatValue) Iterator(java.util.Iterator) File(java.io.File) Test(org.junit.Test)

Example 55 with Statistics

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

the class SampleCollectorTest method testAddHandlerBeforeSample.

@Test
public void testAddHandlerBeforeSample() {
    TestSampleHandler handler = new TestSampleHandler();
    this.sampleCollector.addSampleHandler(handler);
    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);
    this.sampleCollector.sample(NanoTimer.getTime());
    assertEquals(3, handler.getNotificationCount());
    List<Info> notifications = handler.getNotifications();
    // validate the allocatedResourceType notification
    assertTrue(notifications.get(0) instanceof ResourceTypeInfo);
    ResourceTypeInfo allocatedResourceTypeInfo = (ResourceTypeInfo) notifications.get(0);
    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
    assertTrue(notifications.get(1) instanceof ResourceInstanceInfo);
    ResourceInstanceInfo allocatedResourceInstanceInfo = (ResourceInstanceInfo) notifications.get(1);
    assertNotNull(allocatedResourceInstanceInfo);
    assertEquals("allocatedResourceInstance", allocatedResourceInstanceInfo.getName());
    ResourceInstance resourceInstance = allocatedResourceInstanceInfo.getResourceInstance();
    assertNotNull(resourceInstance);
    assertEquals(0, resourceInstance.getId());
    assertEquals(1, 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 sampled notification
    assertTrue(notifications.get(2) instanceof SampledInfo);
    SampledInfo sampledInfo = (SampledInfo) notifications.get(2);
    assertNotNull(sampledInfo);
    assertEquals("sampled", sampledInfo.getName());
    assertEquals(1, sampledInfo.getResourceCount());
}
Also used : ResourceInstanceInfo(org.apache.geode.internal.statistics.TestSampleHandler.ResourceInstanceInfo) StatisticsType(org.apache.geode.StatisticsType) SampledInfo(org.apache.geode.internal.statistics.TestSampleHandler.SampledInfo) ResourceTypeInfo(org.apache.geode.internal.statistics.TestSampleHandler.ResourceTypeInfo) Info(org.apache.geode.internal.statistics.TestSampleHandler.Info) ResourceInstanceInfo(org.apache.geode.internal.statistics.TestSampleHandler.ResourceInstanceInfo) SampledInfo(org.apache.geode.internal.statistics.TestSampleHandler.SampledInfo) ResourceTypeInfo(org.apache.geode.internal.statistics.TestSampleHandler.ResourceTypeInfo) Statistics(org.apache.geode.Statistics) StatisticDescriptor(org.apache.geode.StatisticDescriptor) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

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