Java Code Examples for org.hibernate.stat.Statistics#getEntityNames()

The following examples show how to use org.hibernate.stat.Statistics#getEntityNames() . 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: HibernateStatsReporter.java    From keycloak with Apache License 2.0 6 votes vote down vote up
protected void logEntities(StringBuilder builder, String lineSep, Statistics stats) {
    builder.append("Important entities statistics: ").append(lineSep);
    for (String entity : stats.getEntityNames()) {
        EntityStatistics entityStats = stats.getEntityStatistics(entity);
        if (entityStats.getInsertCount() > LIMIT || entityStats.getDeleteCount() > LIMIT || entityStats.getUpdateCount() > LIMIT || entityStats.getLoadCount() > LIMIT || entityStats.getFetchCount() > LIMIT) {
            builder.append(entity).append(" - ")
                    .append("inserted: ").append(entityStats.getInsertCount())
                    .append(", updated: ").append(entityStats.getUpdateCount())
                    .append(", removed: ").append(entityStats.getDeleteCount())
                    .append(", loaded: ").append(entityStats.getLoadCount())
                    .append(", fetched: ").append(entityStats.getFetchCount())
                    .append(lineSep);
        }
    }
    builder.append(lineSep);
}
 
Example 2
Source File: HibernateStatsUtils.java    From pnc with Apache License 2.0 4 votes vote down vote up
/**
 * Get all the Hibernate Entities statistics aggregated in a sorted Map
 * 
 * @param statistics
 * @return a sorted map containing all the Hibernate entities stats
 */
public static SortedMap<String, Map<String, HibernateMetric>> getSecondLevelCacheEntitiesStats(
        Statistics statistics) {

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

    if (statistics.isStatisticsEnabled()) {
        String[] entityNames = statistics.getEntityNames();
        Stream.of(entityNames).forEach(eN -> {
            EntityStatistics entityStat = statistics.getEntityStatistics(eN);
            SortedMap<String, HibernateMetric> entityStatMap = new TreeMap<String, HibernateMetric>();

            // Entity cache stats
            entityStatMap.put(
                    "cache.region.name",
                    createHibernateMetricItem(
                            "cacheRegionName",
                            "The name of the region where this data is cached.",
                            entityStat.getCacheRegionName()));
            entityStatMap.put(
                    "cache.hit.count",
                    createHibernateMetricItem(
                            "cacheHitCount",
                            "The number of successful cache look-ups for this data from its configured cache region since the last Statistics clearing.",
                            entityStat.getCacheHitCount()));
            entityStatMap.put(
                    "cache.miss.count",
                    createHibernateMetricItem(
                            "cacheMissCount",
                            "The number of unsuccessful cache look-ups for this data from its configured cache region since the last Statistics clearing.",
                            entityStat.getCacheMissCount()));
            entityStatMap.put(
                    "cache.put.count",
                    createHibernateMetricItem(
                            "cachePutCount",
                            "The number of times this data has been into its configured cache region since the last Statistics clearing.",
                            entityStat.getCachePutCount()));
            double hitsRatio = (entityStat.getCacheHitCount() + entityStat.getCacheMissCount()) != 0
                    ? ((double) entityStat.getCacheHitCount()
                            / (entityStat.getCacheHitCount() + entityStat.getCacheMissCount()) * 100)
                    : -1;
            entityStatMap.put(
                    "cache.hit.ratio",
                    createHibernateMetricItem(
                            "cacheHitRatio",
                            "The ratio of successful cache look-ups for this data from its configured cache region since the last Statistics clearing.",
                            df2.format(hitsRatio)));

            // Entity stats
            entityStatMap.put(
                    "fetch.count",
                    createHibernateMetricItem(
                            "fetchCount",
                            "Number of times (since last Statistics clearing) this entity has been fetched.",
                            entityStat.getFetchCount()));
            entityStatMap.put(
                    "insert.count",
                    createHibernateMetricItem(
                            "insertCount",
                            "Number of times (since last Statistics clearing) this entity has been inserted.",
                            entityStat.getInsertCount()));
            entityStatMap.put(
                    "delete.count",
                    createHibernateMetricItem(
                            "deleteCount",
                            "Number of times (since last Statistics clearing) this entity has been deleted.",
                            entityStat.getDeleteCount()));
            entityStatMap.put(
                    "load.count",
                    createHibernateMetricItem(
                            "loadCount",
                            "Number of times (since last Statistics clearing) this entity has been loaded.",
                            entityStat.getLoadCount()));
            entityStatMap.put(
                    "optimistic.failure.count",
                    createHibernateMetricItem(
                            "optimisticFailureCount",
                            "Number of times (since last Statistics clearing) this entity has experienced an optimistic lock failure.",
                            entityStat.getOptimisticFailureCount()));
            entityStatMap.put(
                    "update.count",
                    createHibernateMetricItem(
                            "updateCount",
                            "Number of times (since last Statistics clearing) this entity has been updated.",
                            entityStat.getUpdateCount()));

            entitiesStatMap.put(ENTITY_STATS_PREFIX + eN, entityStatMap);
        });
    }

    return entitiesStatMap;
}
 
Example 3
Source File: HibernateInformationProvider.java    From gocd with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Object> asJson() {
    LinkedHashMap<String, Object> json = new LinkedHashMap<>();
    Statistics statistics = sessionFactory.getStatistics();
    if (!statistics.isStatisticsEnabled()){
        return json;
    }
    json.put("EntityDeleteCount", statistics.getEntityDeleteCount());
    json.put("EntityInsertCount", statistics.getEntityInsertCount());
    json.put("EntityLoadCount", statistics.getEntityLoadCount());
    json.put("EntityFetchCount", statistics.getEntityFetchCount());
    json.put("EntityUpdateCount", statistics.getEntityUpdateCount());
    json.put("QueryExecutionCount", statistics.getQueryExecutionCount());
    json.put("QueryExecutionMaxTime", statistics.getQueryExecutionMaxTime());
    json.put("QueryExecutionMaxTimeQueryString", statistics.getQueryExecutionMaxTimeQueryString());
    json.put("QueryCacheHitCount", statistics.getQueryCacheHitCount());
    json.put("QueryCacheMissCount", statistics.getQueryCacheMissCount());
    json.put("QueryCachePutCount", statistics.getQueryCachePutCount());
    json.put("FlushCount", statistics.getFlushCount());
    json.put("ConnectCount", statistics.getConnectCount());
    json.put("SecondLevelCacheHitCount", statistics.getSecondLevelCacheHitCount());
    json.put("SecondLevelCacheMissCount", statistics.getSecondLevelCacheMissCount());
    json.put("SecondLevelCachePutCount", statistics.getSecondLevelCachePutCount());
    json.put("SessionCloseCount", statistics.getSessionCloseCount());
    json.put("SessionOpenCount", statistics.getSessionOpenCount());
    json.put("CollectionLoadCount", statistics.getCollectionLoadCount());
    json.put("CollectionFetchCount", statistics.getCollectionFetchCount());
    json.put("CollectionUpdateCount", statistics.getCollectionUpdateCount());
    json.put("CollectionRemoveCount", statistics.getCollectionRemoveCount());
    json.put("CollectionRecreateCount", statistics.getCollectionRecreateCount());
    json.put("StartTime", statistics.getStartTime());
    json.put("SecondLevelCacheRegionNames", statistics.getSecondLevelCacheRegionNames());
    json.put("SuccessfulTransactionCount", statistics.getSuccessfulTransactionCount());
    json.put("TransactionCount", statistics.getTransactionCount());
    json.put("PrepareStatementCount", statistics.getPrepareStatementCount());
    json.put("CloseStatementCount", statistics.getCloseStatementCount());
    json.put("OptimisticFailureCount", statistics.getOptimisticFailureCount());

    LinkedHashMap<String, Object> queryStats = new LinkedHashMap<>();
    json.put("Queries", queryStats);

    String[] queries = statistics.getQueries();
    for (String query : queries) {
        queryStats.put(query, statistics.getQueryStatistics(query));
    }

    LinkedHashMap<String, Object> entityStatistics = new LinkedHashMap<>();
    json.put("EntityStatistics", entityStatistics);

    String[] entityNames = statistics.getEntityNames();
    for (String entityName : entityNames) {
        entityStatistics.put(entityName, statistics.getEntityStatistics(entityName));
    }

    LinkedHashMap<String, Object> roleStatistics = new LinkedHashMap<>();
    json.put("RoleStatistics", roleStatistics);

    String[] roleNames = statistics.getCollectionRoleNames();
    for (String roleName : roleNames) {
        roleStatistics.put(roleName, statistics.getCollectionStatistics(roleName));
    }

    return json;
}