下面列出了org.hibernate.stat.Statistics#getCollectionRoleNames ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void logCollections(StringBuilder builder, String lineSep, Statistics stats) {
builder.append("Important collections statistics: ").append(lineSep);
for (String col : stats.getCollectionRoleNames()) {
CollectionStatistics collectionStats = stats.getCollectionStatistics(col);
if (collectionStats.getRecreateCount() > LIMIT || collectionStats.getUpdateCount() > LIMIT || collectionStats.getRemoveCount() > LIMIT ||
collectionStats.getLoadCount() > LIMIT || collectionStats.getFetchCount() > LIMIT) {
builder.append(col).append(" - ")
.append("recreated: ").append(collectionStats.getRecreateCount())
.append(", updated: ").append(collectionStats.getUpdateCount())
.append(", removed: ").append(collectionStats.getRemoveCount())
.append(", loaded: ").append(collectionStats.getLoadCount())
.append(", fetched: ").append(collectionStats.getFetchCount())
.append(lineSep);
}
}
builder.append(lineSep);
}
/**
* Get all the Hibernate collections statistics aggregated in a sorted Map
*
* @param statistics
* @return a sorted map containing all the Hibernate collections statistics
*/
public static SortedMap<String, Map<String, HibernateMetric>> getSecondLevelCacheCollectionsStats(
Statistics statistics) {
SortedMap<String, Map<String, HibernateMetric>> collectionsStatMap = new TreeMap<String, Map<String, HibernateMetric>>();
if (statistics.isStatisticsEnabled()) {
String[] collectionRoleNames = statistics.getCollectionRoleNames();
Stream.of(collectionRoleNames).forEach(crN -> {
CollectionStatistics cStats = statistics.getCollectionStatistics(crN);
SortedMap<String, HibernateMetric> cStatMap = new TreeMap<String, HibernateMetric>();
// Collection cache stats
cStatMap.put(
"cache.region.name",
createHibernateMetricItem(
"cacheRegionName",
"The name of the region where this data is cached.",
cStats.getCacheRegionName()));
cStatMap.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.",
cStats.getCacheHitCount()));
cStatMap.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.",
cStats.getCacheMissCount()));
double secondLvlCacheHitsRatio = (cStats.getCacheHitCount() + cStats.getCacheMissCount()) != 0
? ((double) cStats.getCacheHitCount() / (cStats.getCacheHitCount() + cStats.getCacheMissCount())
* 100)
: -1;
cStatMap.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(secondLvlCacheHitsRatio)));
cStatMap.put(
"cache.put.count",
createHibernateMetricItem(
"cachePutCount",
"The number of times this data has been into its configured cache region since the last Statistics clearing.",
cStats.getCachePutCount()));
// Collection stats
cStatMap.put(
"fetch.count",
createHibernateMetricItem(
"fetchCount",
"Number of times (since last Statistics clearing) this collection has been fetched.",
cStats.getFetchCount()));
cStatMap.put(
"recreate.count",
createHibernateMetricItem(
"recreateCount",
"Number of times (since last Statistics clearing) this collection has been recreated (rows potentially deleted and then rows (re-)inserted).",
cStats.getRecreateCount()));
cStatMap.put(
"remove.count",
createHibernateMetricItem(
"removeCount",
"Number of times (since last Statistics clearing) this collection has been removed.",
cStats.getRemoveCount()));
cStatMap.put(
"load.count",
createHibernateMetricItem(
"loadCount",
"Number of times (since last Statistics clearing) this collection has been loaded.",
cStats.getLoadCount()));
cStatMap.put(
"update.count",
createHibernateMetricItem(
"updateCount",
"Number of times (since last Statistics clearing) this collection has been updated.",
cStats.getUpdateCount()));
collectionsStatMap.put(COLLECTION_STATS_PREFIX + crN, cStatMap);
});
}
return collectionsStatMap;
}
@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;
}