Search in sources :

Example 11 with StatValue

use of org.apache.geode.internal.statistics.StatArchiveReader.StatValue 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 12 with StatValue

use of org.apache.geode.internal.statistics.StatArchiveReader.StatValue in project geode by apache.

the class StatArchiveWithConsecutiveResourceInstIntegrationTest method readingFourActiveCacheClientUpdaterStatsWithReaderMatchSpec.

@Test
public void readingFourActiveCacheClientUpdaterStatsWithReaderMatchSpec() throws Exception {
    StatArchiveReader reader = new StatArchiveReader(new File[] { this.archiveFile }, new StatSpec[] { this.statSpec }, true);
    Set<ResourceInst> resourceInstList = new HashSet<>();
    for (StatValue statValue : reader.matchSpec(this.statSpec)) {
        for (int i = 0; i < statValue.getResources().length; i++) {
            resourceInstList.add(statValue.getResources()[i]);
        }
    }
    assertThat(resourceInstList.size()).isEqualTo(2);
}
Also used : ResourceInst(org.apache.geode.internal.statistics.StatArchiveReader.ResourceInst) StatValue(org.apache.geode.internal.statistics.StatArchiveReader.StatValue) HashSet(java.util.HashSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 13 with StatValue

use of org.apache.geode.internal.statistics.StatArchiveReader.StatValue 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);
}
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 StatValue

use of org.apache.geode.internal.statistics.StatArchiveReader.StatValue in project geode by apache.

the class SystemAdmin method statistics.

/**
   * List the statistics of a running system.
   * 
   * @param directory the system directory of the system to list.
   * @param archiveNames the archive file(s) to read.
   * @param details if true the statistic descriptions will also be listed.
   * @param nofilter if true then printed stat values will all be raw unfiltered.
   * @param persec if true then printed stat values will all be the rate of change, per second, of
   *        the raw values.
   * @param persample if true then printed stat values will all be the rate of change, per sample,
   *        of the raw values.
   * @param prunezeros if true then stat values whose samples are all zero will not be printed.
   *
   * @throws UncreatedSystemException if the system <code>sysDir</code> does not exist, is not a
   *         directory, or does not contain a configuration file.
   * @throws NoSystemException if the system is not running or could not be connected to.
   * @throws IllegalArgumentException if a statSpec does not match a resource and/or statistic.
   * @throws GemFireIOException if the archive could not be read
   */
public void statistics(File directory, List archiveNames, boolean details, boolean nofilter, boolean persec, boolean persample, boolean prunezeros, boolean monitor, long startTime, long endTime, List cmdLineSpecs) {
    if (persec && nofilter) {
        throw new IllegalArgumentException(LocalizedStrings.SystemAdmin_THE_NOFILTER_AND_PERSEC_OPTIONS_ARE_MUTUALLY_EXCLUSIVE.toLocalizedString());
    }
    if (persec && persample) {
        throw new IllegalArgumentException(LocalizedStrings.SystemAdmin_THE_PERSAMPLE_AND_PERSEC_OPTIONS_ARE_MUTUALLY_EXCLUSIVE.toLocalizedString());
    }
    if (nofilter && persample) {
        throw new IllegalArgumentException(LocalizedStrings.SystemAdmin_THE_PERSAMPLE_AND_NOFILTER_OPTIONS_ARE_MUTUALLY_EXCLUSIVE.toLocalizedString());
    }
    StatSpec[] specs = createSpecs(cmdLineSpecs);
    if (archiveOption != null) {
        if (directory != null) {
            throw new IllegalArgumentException(LocalizedStrings.SystemAdmin_THE_ARCHIVE_AND_DIR_OPTIONS_ARE_MUTUALLY_EXCLUSIVE.toLocalizedString());
        }
        StatArchiveReader reader = null;
        boolean interrupted = false;
        try {
            reader = new StatArchiveReader((File[]) archiveNames.toArray(new File[archiveNames.size()]), specs, !monitor);
            // (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
            if (specs.length == 0) {
                if (details) {
                    StatArchiveReader.StatArchiveFile[] archives = reader.getArchives();
                    for (int i = 0; i < archives.length; i++) {
                        System.out.println(archives[i].getArchiveInfo().toString());
                    }
                }
            }
            do {
                if (specs.length == 0) {
                    Iterator it = reader.getResourceInstList().iterator();
                    while (it.hasNext()) {
                        ResourceInst inst = (ResourceInst) it.next();
                        StatValue[] values = inst.getStatValues();
                        boolean firstTime = true;
                        for (int i = 0; i < values.length; i++) {
                            if (values[i] != null && values[i].hasValueChanged()) {
                                if (firstTime) {
                                    firstTime = false;
                                    System.out.println(inst.toString());
                                }
                                printStatValue(values[i], startTime, endTime, nofilter, persec, persample, prunezeros, details);
                            }
                        }
                    }
                } else {
                    Map<CombinedResources, List<StatValue>> allSpecsMap = new HashMap<CombinedResources, List<StatValue>>();
                    for (int i = 0; i < specs.length; i++) {
                        StatValue[] values = reader.matchSpec(specs[i]);
                        if (values.length == 0) {
                            if (!quiet) {
                                System.err.println(LocalizedStrings.SystemAdmin_WARNING_NO_STATS_MATCHED_0.toLocalizedString(specs[i].cmdLineSpec));
                            }
                        } else {
                            Map<CombinedResources, List<StatValue>> specMap = new HashMap<CombinedResources, List<StatValue>>();
                            for (StatValue v : values) {
                                CombinedResources key = new CombinedResources(v);
                                List<StatArchiveReader.StatValue> list = specMap.get(key);
                                if (list != null) {
                                    list.add(v);
                                } else {
                                    specMap.put(key, new ArrayList<StatValue>(Collections.singletonList(v)));
                                }
                            }
                            if (!quiet) {
                                System.out.println(LocalizedStrings.SystemAdmin_INFO_FOUND_0_MATCHES_FOR_1.toLocalizedString(new Object[] { Integer.valueOf(specMap.size()), specs[i].cmdLineSpec }));
                            }
                            for (Map.Entry<CombinedResources, List<StatValue>> me : specMap.entrySet()) {
                                List<StatArchiveReader.StatValue> list = allSpecsMap.get(me.getKey());
                                if (list != null) {
                                    list.addAll(me.getValue());
                                } else {
                                    allSpecsMap.put(me.getKey(), me.getValue());
                                }
                            }
                        }
                    }
                    for (Map.Entry<CombinedResources, List<StatValue>> me : allSpecsMap.entrySet()) {
                        System.out.println(me.getKey());
                        for (StatValue v : me.getValue()) {
                            printStatValue(v, startTime, endTime, nofilter, persec, persample, prunezeros, details);
                        }
                    }
                }
                if (monitor) {
                    while (!reader.update()) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ignore) {
                            interrupted = true;
                        }
                    }
                }
            } while (monitor && !interrupted);
        } catch (IOException ex) {
            throw new GemFireIOException(LocalizedStrings.SystemAdmin_FAILED_READING_0.toLocalizedString(archiveOption), ex);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ignore) {
                }
            }
            if (interrupted) {
                Thread.currentThread().interrupt();
            }
        }
    }
}
Also used : StatArchiveReader(org.apache.geode.internal.statistics.StatArchiveReader) ResourceInst(org.apache.geode.internal.statistics.StatArchiveReader.ResourceInst) StatValue(org.apache.geode.internal.statistics.StatArchiveReader.StatValue)

Aggregations

StatValue (org.apache.geode.internal.statistics.StatArchiveReader.StatValue)14 Test (org.junit.Test)12 File (java.io.File)11 Statistics (org.apache.geode.Statistics)11 StatisticsType (org.apache.geode.StatisticsType)11 List (java.util.List)10 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)10 StatisticDescriptor (org.apache.geode.StatisticDescriptor)9 TestStatArchiveWriter (org.apache.geode.internal.statistics.TestStatArchiveWriter)8 ResourceInst (org.apache.geode.internal.statistics.StatArchiveReader.ResourceInst)5 Iterator (java.util.Iterator)4 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 Random (java.util.Random)1 CancelCriterion (org.apache.geode.CancelCriterion)1 Cache (org.apache.geode.cache.Cache)1 Region (org.apache.geode.cache.Region)1 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)1 DistributedMember (org.apache.geode.distributed.DistributedMember)1