org.hibernate.stat.CacheRegionStatistics Java Examples

The following examples show how to use org.hibernate.stat.CacheRegionStatistics. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HibernateL2CacheSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param sesFactory Session factory.
 * @param idToChildCnt Number of children per entity.
 * @param expHit Expected cache hits.
 * @param expMiss Expected cache misses.
 */
@SuppressWarnings("unchecked")
private void assertCollectionCache(SessionFactory sesFactory, Map<Integer, Integer> idToChildCnt, int expHit,
    int expMiss) {
    sesFactory.getStatistics().clear();

    Session ses = sesFactory.openSession();

    try {
        for (Map.Entry<Integer, Integer> e : idToChildCnt.entrySet()) {
            Entity entity = (Entity)ses.load(Entity.class, e.getKey());

            assertEquals((int)e.getValue(), entity.getChildren().size());
        }
    }
    finally {
        ses.close();
    }

    CacheRegionStatistics stats = sesFactory.getStatistics().getCacheRegionStatistics(CHILD_COLLECTION_REGION);

    assertEquals(expHit, stats.getHitCount());

    assertEquals(expMiss, stats.getMissCount());
}
 
Example #2
Source File: HibernateL2CacheSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param sesFactory Session factory.
 * @param nameToId Name-ID mapping.
 * @param absentNames Absent entities' names.
 */
private void assertNaturalIdCache(SessionFactory sesFactory, Map<String, Integer> nameToId, String... absentNames) {
    sesFactory.getStatistics().clear();

    CacheRegionStatistics stats = sesFactory.getStatistics().getCacheRegionStatistics(NATURAL_ID_REGION);

    long hitBefore = stats.getHitCount();

    long missBefore = stats.getMissCount();

    final Session ses = sesFactory.openSession();

    try {
        for (Map.Entry<String, Integer> e : nameToId.entrySet())
            assertEquals((int) e.getValue(), ((Entity) ses.bySimpleNaturalId(Entity.class).load(e.getKey())).getId());

        for (String name : absentNames)
            assertNull((ses.bySimpleNaturalId(Entity.class).load(name)));

        assertEquals(nameToId.size() + hitBefore, stats.getHitCount());

        assertEquals(absentNames.length + missBefore, stats.getMissCount());
    }
    finally {
        ses.close();
    }
}
 
Example #3
Source File: InfinispanCacheJPAFunctionalityTestEndpoint.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private static Counts statsToCounts(String region, Statistics stats) {
    final CacheRegionStatistics cacheStats = stats.getDomainDataRegionStatistics(region);
    return new Counts(
            cacheStats.getPutCount(), cacheStats.getHitCount(), cacheStats.getMissCount(),
            cacheStats.getElementCountInMemory());
}
 
Example #4
Source File: HibernateStatsUtils.java    From pnc with Apache License 2.0 4 votes vote down vote up
/**
 * Get all the Hibernate Entities second level cache statistics aggregated in a sorted Map
 * 
 * @param statistics
 * @return a sorted map containing all the Hibernate second level cache statistics
 */
public static SortedMap<String, Map<String, HibernateMetric>> getSecondLevelCacheRegionsStats(
        Statistics statistics) {

    SortedMap<String, Map<String, HibernateMetric>> secondLevelCachesStatMap = new TreeMap<String, Map<String, HibernateMetric>>();

    if (statistics.isStatisticsEnabled()) {
        String[] cacheRegionNames = statistics.getSecondLevelCacheRegionNames();
        Stream.of(cacheRegionNames).forEach(crN -> {
            try {
                CacheRegionStatistics sLCStats = statistics.getDomainDataRegionStatistics(crN);
                SortedMap<String, HibernateMetric> sLCStatMap = new TreeMap<String, HibernateMetric>();

                sLCStatMap.put(
                        "second-level-cache.cache.region.name",
                        createHibernateMetricItem(
                                "regionName",
                                "The name of the region where this data is cached.",
                                sLCStats.getRegionName()));

                sLCStatMap.put(
                        "second-level-cache.element.count.in.memory",
                        createHibernateMetricItem(
                                "elementCountInMemory",
                                "The number of elements currently in memory within the cache provider.",
                                sLCStats.getElementCountInMemory() != CacheRegionStatistics.NO_EXTENDED_STAT_SUPPORT_RETURN
                                        ? sLCStats.getElementCountInMemory()
                                        : -1));
                sLCStatMap.put(
                        "second-level-cache.element.count.on.disk",
                        createHibernateMetricItem(
                                "elementCountOnDisk",
                                "The number of elements currently stored to disk within the cache provider.",
                                sLCStats.getElementCountOnDisk() != CacheRegionStatistics.NO_EXTENDED_STAT_SUPPORT_RETURN
                                        ? sLCStats.getElementCountOnDisk()
                                        : -1));
                sLCStatMap.put(
                        "second-level-cache.size.in.memory",
                        createHibernateMetricItem(
                                "sizeInMemory",
                                "The size that the in-memory elements take up within the cache provider.",
                                sLCStats.getSizeInMemory() != CacheRegionStatistics.NO_EXTENDED_STAT_SUPPORT_RETURN
                                        ? sLCStats.getSizeInMemory()
                                        : -1));

                sLCStatMap.put(
                        "second-level-cache.hit.count",
                        createHibernateMetricItem(
                                "hitCount",
                                "The number of successful cache look-ups against the region since the last Statistics clearing.",
                                sLCStats.getHitCount()));
                sLCStatMap.put(
                        "second-level-cache.miss.count",
                        createHibernateMetricItem(
                                "missCount",
                                "The number of unsuccessful cache look-ups against the region since the last Statistics clearing.",
                                sLCStats.getMissCount()));
                double secondLvlCacheHitsRatio = (sLCStats.getHitCount() + sLCStats.getMissCount()) != 0
                        ? ((double) sLCStats.getHitCount() / (sLCStats.getHitCount() + sLCStats.getMissCount())
                                * 100)
                        : -1;
                sLCStatMap.put(
                        "second-level-cache.hit.ratio",
                        createHibernateMetricItem(
                                "hitRatio",
                                "The ratio of successful cache look-ups against the region since the last Statistics clearing.",
                                df2.format(secondLvlCacheHitsRatio)));
                sLCStatMap.put(
                        "second-level-cache.put.count",
                        createHibernateMetricItem(
                                "putCount",
                                "The number of cache puts into the region since the last Statistics clearing.",
                                sLCStats.getPutCount()));

                secondLevelCachesStatMap.put(REGION_STATS_PREFIX + crN, sLCStatMap);
            } catch (IllegalArgumentException e) {
                // logger.error("The region name could not be resolved: {}", e);
            }
        });
    }

    return secondLevelCachesStatMap;
}