Example usage for org.hibernate.stat QueryStatistics getCacheHitCount

List of usage examples for org.hibernate.stat QueryStatistics getCacheHitCount

Introduction

In this page you can find the example usage for org.hibernate.stat QueryStatistics getCacheHitCount.

Prototype

long getCacheHitCount();

Source Link

Document

The number of cache hits for this query.

Usage

From source file:com.francetelecom.clara.cloud.scalability.helper.StatisticsHelper.java

License:Apache License

private static void queryStats(Statistics stats, String q) {
    logger.info("Query statistics for " + q);

    QueryStatistics qStats = stats.getQueryStatistics(q);

    logger.info("  Execution ct : " + qStats.getExecutionCount());
    logger.info("  Cache hits : " + qStats.getCacheHitCount());
    logger.info("  Cache puts : " + qStats.getCachePutCount());
    logger.info("  Cache misses : " + qStats.getCacheMissCount());
    logger.info("  Execution row ct : " + qStats.getExecutionRowCount());
    logger.info("  Execution avg millis : " + qStats.getExecutionAvgTime());
    logger.info("  Execution max millis : " + qStats.getExecutionMaxTime());
    logger.info("  Execution min millis : " + qStats.getExecutionMinTime());
}

From source file:com.hibernateinstrumentator.jboss.StatisticsService.java

License:Apache License

@Override
public Map<String, Object> getQueryStatisticsMap(String queryString) {

    Map<String, Object> m = null;
    QueryStatistics qs = super.getQueryStatistics(queryString);
    if (qs != null) {
        m = new HashMap<String, Object>();
        m.put("cacheHitCount", qs.getCacheHitCount());
        m.put("cacheMissCount", qs.getCacheMissCount());
        m.put("cachePutCount", qs.getCachePutCount());
        m.put("executionAvgTime", qs.getExecutionAvgTime());
        m.put("executionTime", qs.getExecutionCount());
        m.put("executionMaxTime", qs.getExecutionMaxTime());
        m.put("executionMinTime", qs.getExecutionMinTime());
        m.put("executionRowCount", qs.getExecutionRowCount());
    }/*  ww  w  .j  a v a 2 s  .  c o m*/

    return m;
}

From source file:com.openkm.servlet.admin.HibernateStatsServlet.java

License:Open Source License

/**
 * View log/*from   w  ww  .  j a  v  a2 s.  c  o  m*/
 */
private void view(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    log.debug("view({}, {})", request, response);
    refresh();

    // Query Statistics
    List<Map<String, String>> qStats = new ArrayList<Map<String, String>>();
    for (String query : queryStatistics.keySet()) {
        QueryStatistics queryStats = queryStatistics.get(query);
        Map<String, String> stat = new HashMap<String, String>();
        stat.put("query", query);
        //stat.put("tquery", HibernateUtil.toSql(query));
        stat.put("executionCount", Long.toString(queryStats.getExecutionCount()));
        stat.put("executionRowCount", Long.toString(queryStats.getExecutionRowCount()));
        stat.put("executionMaxTime", Long.toString(queryStats.getExecutionMaxTime()));
        stat.put("executionMinTime", Long.toString(queryStats.getExecutionMinTime()));
        stat.put("executionAvgTime", Long.toString(queryStats.getExecutionAvgTime()));
        stat.put("executionTotalTime",
                Long.toString(queryStats.getExecutionAvgTime() * queryStats.getExecutionCount()));
        stat.put("cacheHitCount", Long.toString(queryStats.getCacheHitCount()));
        stat.put("cacheMissCount", Long.toString(queryStats.getCacheMissCount()));
        stat.put("cachePutCount", Long.toString(queryStats.getCachePutCount()));
        qStats.add(stat);
    }

    // Entity Statistics
    List<Map<String, String>> eStats = new ArrayList<Map<String, String>>();
    for (String entity : entityStatistics.keySet()) {
        EntityStatistics entityStats = entityStatistics.get(entity);
        Map<String, String> stat = new HashMap<String, String>();
        stat.put("entity", entity);
        stat.put("loadCount", Long.toString(entityStats.getLoadCount()));
        stat.put("fetchCount", Long.toString(entityStats.getFetchCount()));
        stat.put("insertCount", Long.toString(entityStats.getInsertCount()));
        stat.put("updateCount", Long.toString(entityStats.getUpdateCount()));
        stat.put("deleteCount", Long.toString(entityStats.getDeleteCount()));
        stat.put("optimisticFailureCount", Long.toString(entityStats.getOptimisticFailureCount()));
        eStats.add(stat);
    }

    // Collection Statistics
    List<Map<String, String>> cStats = new ArrayList<Map<String, String>>();
    for (String collection : collectionStatistics.keySet()) {
        CollectionStatistics collectionStats = collectionStatistics.get(collection);
        Map<String, String> stat = new HashMap<String, String>();
        stat.put("collection", collection);
        stat.put("loadCount", Long.toString(collectionStats.getLoadCount()));
        stat.put("fetchCount", Long.toString(collectionStats.getFetchCount()));
        stat.put("updateCount", Long.toString(collectionStats.getUpdateCount()));
        stat.put("recreateCount", Long.toString(collectionStats.getRecreateCount()));
        stat.put("removeCount", Long.toString(collectionStats.getRemoveCount()));
        cStats.add(stat);
    }

    // 2nd Level Cache Statistics
    long totalSizeInMemory = 0;
    List<Map<String, String>> slcStats = new ArrayList<Map<String, String>>();
    for (String cache : secondLevelCacheStatistics.keySet()) {
        SecondLevelCacheStatistics cacheStats = secondLevelCacheStatistics.get(cache);
        totalSizeInMemory += cacheStats.getSizeInMemory();
        Map<String, String> stat = new HashMap<String, String>();
        stat.put("cache", cache);
        stat.put("putCount", Long.toString(cacheStats.getPutCount()));
        stat.put("hitCount", Long.toString(cacheStats.getHitCount()));
        stat.put("missCount", Long.toString(cacheStats.getMissCount()));
        stat.put("elementCountInMemory", Long.toString(cacheStats.getElementCountInMemory()));
        stat.put("sizeInMemory", Long.toString(cacheStats.getSizeInMemory()));
        stat.put("elementCountOnDisk", Long.toString(cacheStats.getElementCountOnDisk()));
        slcStats.add(stat);
    }

    ServletContext sc = getServletContext();
    sc.setAttribute("generalStats", generalStatistics);
    sc.setAttribute("queryStats", qStats);
    sc.setAttribute("entityStats", eStats);
    sc.setAttribute("collectionStats", cStats);
    sc.setAttribute("secondLevelCacheStats", slcStats);
    sc.setAttribute("totalSizeInMemory", totalSizeInMemory);
    sc.setAttribute("statsEnabled", HibernateUtil.getSessionFactory().getStatistics().isStatisticsEnabled());
    sc.getRequestDispatcher("/admin/hibernate_stats.jsp").forward(request, response);

    // Activity log
    UserActivity.log(request.getRemoteUser(), "ADMIN_HIBERNATE_STATS", null, null, null);

    log.debug("view: void");
}

From source file:org.bedework.calcore.hibernate.DbStatistics.java

License:Apache License

private static void queryStats(Collection<StatsEntry> c, Statistics dbStats, String q) {
    c.add(new StatsEntry("Query statistics for " + q));

    QueryStatistics qStats = dbStats.getQueryStatistics(q);

    c.add(new StatsEntry("Execution ct", qStats.getExecutionCount()));
    c.add(new StatsEntry("Cache hits", qStats.getCacheHitCount()));
    c.add(new StatsEntry("Cache puts", qStats.getCachePutCount()));
    c.add(new StatsEntry("Cache misses", qStats.getCacheMissCount()));
    c.add(new StatsEntry("Execution row ct", qStats.getExecutionRowCount()));
    c.add(new StatsEntry("Execution avg millis", qStats.getExecutionAvgTime()));
    c.add(new StatsEntry("Execution max millis", qStats.getExecutionMaxTime()));
    c.add(new StatsEntry("Execution min millis", qStats.getExecutionMinTime()));
}

From source file:org.jboss.as.jpa.hibernate4.management.QueryMetricsHandler.java

License:Open Source License

static final QueryMetricsHandler getCacheHitCount(
        final PersistenceUnitServiceRegistry persistenceUnitRegistry) {
    return new QueryMetricsHandler(persistenceUnitRegistry) {
        @Override/*w w  w.j  a  v  a  2 s .c  o  m*/
        protected void handle(QueryStatistics statistics, OperationContext context, String attributeName,
                String originalQueryName) {
            long count = statistics.getCacheHitCount();
            context.getResult().set(count);
        }
    };
}

From source file:org.jboss.as.test.integration.jpa.secondlevelcache.SFSB2LC.java

License:Open Source License

/**
 * Performs 2 query calls, first call put query in the cache and second should hit the cache
 *
 * @param id Employee's id in the query// ww  w. j av a  2  s.  com
 */
public String queryCacheCheck(String id) {

    // the nextTimestamp from infinispan is "return System.currentTimeMillis()"
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
        return e.getMessage();
    }

    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();

    try {
        String queryString = "from Employee e where e.id > " + id;
        QueryStatistics queryStats = stats.getQueryStatistics(queryString);
        Query query = em.createQuery(queryString);
        query.setHint("org.hibernate.cacheable", true);

        // query - this call should fill the cache
        query.getResultList();
        assertEquals("Expected 1 miss in cache" + generateQueryCacheStats(queryStats), 1,
                queryStats.getCacheMissCount());
        assertEquals("Expected 1 put in cache" + generateQueryCacheStats(queryStats), 1,
                queryStats.getCachePutCount());
        assertEquals("Expected no hits in cache" + generateQueryCacheStats(queryStats), 0,
                queryStats.getCacheHitCount());

        // query - second call should hit cache
        query.getResultList();
        assertEquals("Expected 1 hit in cache" + generateQueryCacheStats(queryStats), 1,
                queryStats.getCacheHitCount());

    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}

From source file:org.jboss.as.test.integration.jpa.secondlevelcache.SFSB2LC.java

License:Open Source License

/**
 * Checking if query cache is empty/*from  ww w  .  j a v a 2s .  com*/
 *
 * @param id Employee's id in the query
 */
public String queryCacheCheckIfEmpty(String id) {

    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();

    try {
        // the nextTimestamp from infinispan is "return System.currentTimeMillis() / 100;"
        Thread.sleep(1000);

        String queryString = "from Employee e where e.id > " + id;
        QueryStatistics queryStats = stats.getQueryStatistics(queryString);
        Query query = em.createQuery(queryString);
        query.setHint("org.hibernate.cacheable", true);

        // query - this call shouldn't hit the cache -> query cache is empty
        query.getResultList();
        assertEquals("Expected 1 miss in cache" + generateQueryCacheStats(queryStats), 1,
                queryStats.getCacheMissCount());
        assertEquals("Expected 1 put in cache" + generateQueryCacheStats(queryStats), 1,
                queryStats.getCachePutCount());
        assertEquals("Expected no hits in cache" + generateQueryCacheStats(queryStats), 0,
                queryStats.getCacheHitCount());

    } catch (AssertionError e) {
        return e.getMessage();
    } catch (InterruptedException e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}

From source file:org.jboss.as.test.integration.jpa.secondlevelcache.SFSB2LC.java

License:Open Source License

/**
 * Generate query cache statistics for put, hit and miss count as one String
 *//*  w w  w . ja v a  2s.c  om*/
public String generateQueryCacheStats(QueryStatistics stats) {
    String result = "(hitCount=" + stats.getCacheHitCount() + ", missCount=" + stats.getCacheMissCount()
            + ", putCount=" + stats.getCachePutCount() + ").";

    return result;
}

From source file:org.openmrs.api.db.hibernate.HibernateContextDAO.java

License:Mozilla Public License

/**
 * Convenience method to print out the hibernate cache usage stats to the log
 *///www.  jav a  2s .c  o  m
private void showUsageStatistics() {
    if (sessionFactory.getStatistics().isStatisticsEnabled()) {
        log.debug("Getting query statistics: ");
        Statistics stats = sessionFactory.getStatistics();
        for (String query : stats.getQueries()) {
            log.info("QUERY: " + query);
            QueryStatistics qstats = stats.getQueryStatistics(query);
            log.info("Cache Hit Count : " + qstats.getCacheHitCount());
            log.info("Cache Miss Count: " + qstats.getCacheMissCount());
            log.info("Cache Put Count : " + qstats.getCachePutCount());
            log.info("Execution Count : " + qstats.getExecutionCount());
            log.info("Average time    : " + qstats.getExecutionAvgTime());
            log.info("Row Count       : " + qstats.getExecutionRowCount());
        }
    }
}

From source file:org.unitime.commons.hibernate.stats.StatsProvider.java

License:Open Source License

/**
 * Format statistics in HTML//from  w w w.  jav a  2  s  . c om
 * @param sessionFactory Hibernate Session Factory
 * @param summaryOnly true - Display only summary info
 * @return HTML String
 */
public String getStatsHtml(SessionFactory sessionFactory, boolean summaryOnly) {

    StringBuffer hibStats = new StringBuffer();

    try {
        // Get statistics
        Statistics stats = sessionFactory.getStatistics();

        // Checks statistics enabled
        if (!stats.isStatisticsEnabled()) {
            return "<font color='red'><b>Hibernate statistics is not enabled.</b></font>";
        }

        // Row Color for even numbered rows
        String evenRowColor = "#FAFAFA";

        // Generate HTML table
        Table table = new Table();
        table.setWidth("100%");
        table.setBorder(0);
        table.setCellSpacing(0);
        table.setCellPadding(3);

        // Links
        StringBuffer links = new StringBuffer("");

        links.append("<A class=\"l7\" href=\"#Entity\">Entity</A>");
        if (!summaryOnly)
            links.append(" - <A class=\"l7\" href=\"#EntityDetail\">Detail</A>");

        links.append(" | <A class=\"l7\" href=\"#Collection\">Collection</A>");
        if (!summaryOnly)
            links.append(" - <A class=\"l7\" href=\"#CollectionDetail\">Detail</A>");

        links.append(" | <A class=\"l7\" href=\"#SecondLevelCache\">Second Level Cache</A>");
        if (!summaryOnly)
            links.append(" - <A class=\"l7\" href=\"#SecondLevelCacheDetail\">Detail</A>");

        links.append(" | <A class=\"l7\" href=\"#Query\">Query</A>");
        if (!summaryOnly)
            links.append(" - <A class=\"l7\" href=\"#QueryDetail\">Detail</A>");

        TableRow row = new TableRow();
        row.addContent(cell(links.toString(), 1, 2, true, "center", "middle"));
        table.addContent(row);

        // Link to top
        TableRow linkToTop = new TableRow();
        linkToTop.addContent(
                cell("<A class=\"l7\" href=\"#BackToTop\">Back to Top</A>", 1, 2, true, "right", "middle"));

        // ---------------------- Overall Stats ------------------------
        row = new TableRow();
        row.addContent(headerCell("<A name=\"BackToTop\">Metric</A>", 1, 1));
        row.addContent(headerCell("Value", 1, 1));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Start Time", 1, 1, true));
        row.addContent(cell(new Date(stats.getStartTime()).toString(), 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Connect Count", 1, 1, true));
        row.addContent(cell(stats.getConnectCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Flush Count", 1, 1, true));
        row.addContent(cell(stats.getFlushCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Session Open Count", 1, 1, true));
        row.addContent(cell(stats.getSessionOpenCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Session Close Count", 1, 1, true));
        row.addContent(cell(stats.getSessionCloseCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Transaction Count", 1, 1, true));
        row.addContent(cell(stats.getTransactionCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Successful Transaction Count", 1, 1, true));
        row.addContent(cell(stats.getSuccessfulTransactionCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Prepare Statement Count", 1, 1, true));
        row.addContent(cell(stats.getPrepareStatementCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Close Statement Count", 1, 1, true));
        row.addContent(cell(stats.getCloseStatementCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Optimistic Failure Count", 1, 1, true));
        row.addContent(cell(stats.getOptimisticFailureCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell("<hr>", 1, 2, false));
        table.addContent(row);

        // ---------------------- Entity Stats ------------------------
        row = new TableRow();
        row.addContent(headerCell("<A name=\"Entity\">Entity</A>:", 1, 2));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Fetch Count", 1, 1, true));
        row.addContent(cell(stats.getEntityFetchCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Load Count", 1, 1, true));
        row.addContent(cell(stats.getEntityLoadCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Insert Count", 1, 1, true));
        row.addContent(cell(stats.getEntityInsertCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Update Count", 1, 1, true));
        row.addContent(cell(stats.getEntityUpdateCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Delete Count", 1, 1, true));
        row.addContent(cell(stats.getEntityDeleteCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell("<hr>", 1, 2, false));
        table.addContent(row);

        table.addContent(linkToTop);

        // ---------------------- Detailed Entity Stats ------------------------

        if (!summaryOnly) {

            row = new TableRow();
            row.addContent(headerCell("<A name=\"EntityDetail\">Entity Statistics Detail</A>:", 1, 2));
            table.addContent(row);

            String[] cEntityNames = stats.getEntityNames();

            if (cEntityNames == null || cEntityNames.length == 0) {
                row = new TableRow();
                row.addContent(cell("No entity names found", 1, 2, false));
                table.addContent(row);
            } else {
                Table subTable = new Table();
                subTable.setCellSpacing(1);
                subTable.setCellPadding(3);

                row = new TableRow();
                row.addContent(headerCell(" &nbsp; ", 1, 1));
                row.addContent(headerCell(" Fetches ", 1, 1));
                row.addContent(headerCell(" Loads ", 1, 1));
                row.addContent(headerCell(" Inserts ", 1, 1));
                row.addContent(headerCell(" Updates ", 1, 1));
                row.addContent(headerCell(" Deletes ", 1, 1));
                subTable.addContent(row);

                for (int i = 0; i < cEntityNames.length; i++) {
                    String entityName = cEntityNames[i];
                    EntityStatistics eStats = stats.getEntityStatistics(entityName);

                    row = new TableRow();
                    if (i % 2 == 0)
                        row.setBgColor(evenRowColor);
                    row.addContent(cell(entityName + " &nbsp;", 1, 1, true));
                    row.addContent(cell(eStats.getFetchCount() + "", 1, 1, false));
                    row.addContent(cell(eStats.getLoadCount() + "", 1, 1, false));
                    row.addContent(cell(eStats.getInsertCount() + "", 1, 1, false));
                    row.addContent(cell(eStats.getUpdateCount() + "", 1, 1, false));
                    row.addContent(cell(eStats.getDeleteCount() + "", 1, 1, false));
                    subTable.addContent(row);
                }

                row = new TableRow();
                row.addContent(cell(subTable.toHtml(), 1, 2, true));
                table.addContent(row);
            }

            row = new TableRow();
            row.addContent(cell("<hr>", 1, 2, false));
            table.addContent(row);

            table.addContent(linkToTop);
        }

        // ---------------------- Collection Stats ------------------------
        row = new TableRow();
        row.addContent(headerCell("<A name=\"Collection\">Collection</A>:", 1, 2));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Fetch Count", 1, 1, true));
        row.addContent(cell(stats.getCollectionFetchCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Load Count", 1, 1, true));
        row.addContent(cell(stats.getCollectionLoadCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Update Count", 1, 1, true));
        row.addContent(cell(stats.getCollectionUpdateCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Remove Count", 1, 1, true));
        row.addContent(cell(stats.getCollectionRemoveCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Recreate Count", 1, 1, true));
        row.addContent(cell(stats.getCollectionRecreateCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell("<hr>", 1, 2, false));
        table.addContent(row);

        table.addContent(linkToTop);

        // ---------------------- Detailed Collection Stats ------------------------
        if (!summaryOnly) {

            row = new TableRow();
            row.addContent(headerCell("<A name=\"CollectionDetail\">Collection Statistics Detail</A>:", 1, 2));
            table.addContent(row);

            String[] cRoleNames = stats.getCollectionRoleNames();

            if (cRoleNames == null || cRoleNames.length == 0) {
                row = new TableRow();
                row.addContent(cell("No collection roles found", 1, 2, false));
                table.addContent(row);
            } else {
                Table subTable = new Table();
                subTable.setCellSpacing(1);
                subTable.setCellPadding(3);

                row = new TableRow();
                row.addContent(headerCell(" &nbsp; ", 1, 1));
                row.addContent(headerCell(" Fetches ", 1, 1));
                row.addContent(headerCell(" Loads ", 1, 1));
                row.addContent(headerCell(" Updates ", 1, 1));
                row.addContent(headerCell(" Removes ", 1, 1));
                row.addContent(headerCell(" Recreates ", 1, 1));
                subTable.addContent(row);

                for (int i = 0; i < cRoleNames.length; i++) {
                    String roleName = cRoleNames[i];
                    CollectionStatistics cStats = stats.getCollectionStatistics(roleName);

                    row = new TableRow();
                    if (i % 2 == 0)
                        row.setBgColor(evenRowColor);
                    row.addContent(cell(roleName + " &nbsp;", 1, 1, true));
                    row.addContent(cell(cStats.getFetchCount() + "", 1, 1, false));
                    row.addContent(cell(cStats.getLoadCount() + "", 1, 1, false));
                    row.addContent(cell(cStats.getUpdateCount() + "", 1, 1, false));
                    row.addContent(cell(cStats.getRemoveCount() + "", 1, 1, false));
                    row.addContent(cell(cStats.getRecreateCount() + "", 1, 1, false));
                    subTable.addContent(row);
                }

                row = new TableRow();
                row.addContent(cell(subTable.toHtml(), 1, 2, true));
                table.addContent(row);
            }

            row = new TableRow();
            row.addContent(cell("<hr>", 1, 2, false));
            table.addContent(row);

            table.addContent(linkToTop);
        }

        // ---------------------- Second Level Cache Stats ------------------------
        row = new TableRow();
        row.addContent(headerCell("<A name=\"SecondLevelCache\">Second Level Cache</A>:", 1, 2));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Hit Count", 1, 1, true));
        row.addContent(cell(stats.getSecondLevelCacheHitCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Miss Count", 1, 1, true));
        row.addContent(cell(stats.getSecondLevelCacheMissCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Put Count", 1, 1, true));
        row.addContent(cell(stats.getSecondLevelCachePutCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell("<hr>", 1, 2, false));
        table.addContent(row);

        table.addContent(linkToTop);

        // ---------------------- Detailed Second Level Cache Stats ------------------------
        if (!summaryOnly) {

            row = new TableRow();
            row.addContent(headerCell(
                    "<A name=\"SecondLevelCacheDetail\">Second Level Cache Statistics Detail</A>:", 1, 2));
            table.addContent(row);

            String[] cRegionNames = stats.getSecondLevelCacheRegionNames();

            if (cRegionNames == null || cRegionNames.length == 0) {
                row = new TableRow();
                row.addContent(cell("No region names found", 1, 2, false));
                table.addContent(row);
            } else {
                Table subTable = new Table();
                subTable.setCellSpacing(1);
                subTable.setCellPadding(3);

                row = new TableRow();
                row.addContent(headerCell(" &nbsp; ", 1, 1));
                row.addContent(headerCell(" Entities ", 1, 1));
                row.addContent(headerCell(" Hits ", 1, 1));
                row.addContent(headerCell(" Misses ", 1, 1));
                row.addContent(headerCell(" Puts ", 1, 1));
                row.addContent(headerCell(" In Memory ", 1, 1));
                row.addContent(headerCell(" On Disk ", 1, 1));
                row.addContent(headerCell(" Memory ", 1, 1));
                subTable.addContent(row);

                long elementsInMem = 0, elementsOnDisk = 0, putCnt = 0, missCnt = 0, hitCnt = 0, size = 0;

                for (int i = 0; i < cRegionNames.length; i++) {
                    String cRegionName = cRegionNames[i];
                    SecondLevelCacheStatistics sStats = stats.getSecondLevelCacheStatistics(cRegionName);

                    row = new TableRow();
                    if (i % 2 == 0)
                        row.setBgColor(evenRowColor);
                    row.addContent(cell(cRegionName + " &nbsp;", 1, 1, true));
                    row.addContent(cell(
                            String.valueOf(sStats.getElementCountInMemory() + sStats.getElementCountOnDisk()),
                            1, 1, false)); //sStats.getEntries().size()
                    row.addContent(cell(sStats.getHitCount() + "", 1, 1, false));
                    row.addContent(cell(sStats.getMissCount() + "", 1, 1, false));
                    row.addContent(cell(sStats.getPutCount() + "", 1, 1, false));
                    row.addContent(cell(sStats.getElementCountInMemory() + "", 1, 1, false));
                    row.addContent(cell(sStats.getElementCountOnDisk() + "", 1, 1, false));
                    row.addContent(cell(sStats.getSizeInMemory() + " bytes", 1, 1, false));
                    elementsInMem += sStats.getElementCountInMemory();
                    elementsOnDisk += sStats.getElementCountOnDisk();
                    putCnt += sStats.getPutCount();
                    missCnt += sStats.getMissCount();
                    hitCnt += sStats.getHitCount();
                    size += sStats.getSizeInMemory();
                    subTable.addContent(row);
                }

                row = new TableRow();
                row.addContent(headerCell("Total &nbsp;", 1, 1));
                row.addContent(headerCell("" + (elementsInMem + elementsOnDisk), 1, 1));
                row.addContent(headerCell("" + hitCnt, 1, 1));
                row.addContent(headerCell("" + missCnt, 1, 1));
                row.addContent(headerCell("" + putCnt, 1, 1));
                row.addContent(headerCell("" + elementsInMem, 1, 1));
                row.addContent(headerCell("" + elementsOnDisk, 1, 1));
                row.addContent(headerCell(size + " bytes", 1, 1));
                subTable.addContent(row);

                row = new TableRow();
                row.addContent(cell(subTable.toHtml(), 1, 2, true));
                table.addContent(row);
            }

            row = new TableRow();
            row.addContent(cell("<hr>", 1, 2, false));
            table.addContent(row);

            table.addContent(linkToTop);
        }

        // ---------------------- Query Stats ------------------------
        row = new TableRow();
        row.addContent(headerCell("<A name=\"Query\">Query</A>:", 1, 2));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Execution Count", 1, 1, true));
        row.addContent(cell(stats.getQueryExecutionCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Execution Max Time", 1, 1, true));
        row.addContent(cell(stats.getQueryExecutionMaxTime() + " ms", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Cache Hit Count", 1, 1, true));
        row.addContent(cell(stats.getQueryCacheHitCount() + " ms", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Cache Miss Count", 1, 1, true));
        row.addContent(cell(stats.getQueryCacheMissCount() + " ms", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell(" &nbsp; Cache Put Count", 1, 1, true));
        row.addContent(cell(stats.getQueryCachePutCount() + "", 1, 1, false));
        table.addContent(row);

        row = new TableRow();
        row.addContent(cell("<hr>", 1, 2, false));
        table.addContent(row);

        table.addContent(linkToTop);

        // ---------------------- Detailed Query Stats ------------------------
        if (!summaryOnly) {

            row = new TableRow();
            row.addContent(headerCell("<A name=\"QueryDetail\">Query Statistics Detail</A>:", 1, 2));
            table.addContent(row);

            String[] cQueryStrings = stats.getQueries();

            if (cQueryStrings == null || cQueryStrings.length == 0) {
                row = new TableRow();
                row.addContent(cell("No query strings found", 1, 2, false));
                table.addContent(row);
            } else {
                Table subTable = new Table();
                subTable.setCellSpacing(1);
                subTable.setCellPadding(3);

                row = new TableRow();
                row.addContent(headerCell(" &nbsp; ", 1, 1));
                row.addContent(headerCell(" Execs ", 1, 1));
                row.addContent(headerCell(" Rows ", 1, 1));
                row.addContent(headerCell(" Max Time ", 1, 1));
                row.addContent(headerCell(" Min Time ", 1, 1));
                row.addContent(headerCell(" Avg Time ", 1, 1));
                row.addContent(headerCell(" Cache Hits ", 1, 1));
                row.addContent(headerCell(" Cache Misses ", 1, 1));
                row.addContent(headerCell(" Cache Puts ", 1, 1));
                subTable.addContent(row);

                for (int i = 0; i < cQueryStrings.length; i++) {
                    String cQueryString = cQueryStrings[i];
                    QueryStatistics qStats = stats.getQueryStatistics(cQueryString);

                    row = new TableRow();
                    if (i % 2 == 0)
                        row.setBgColor(evenRowColor);
                    row.addContent(cell(cQueryString + " &nbsp;", 1, 1, false));
                    row.addContent(cell(qStats.getExecutionCount() + "", 1, 1, false));
                    row.addContent(cell(qStats.getExecutionRowCount() + "", 1, 1, false));
                    row.addContent(cell(qStats.getExecutionMaxTime() + " ms", 1, 1, false));
                    row.addContent(cell(qStats.getExecutionMinTime() + " ms", 1, 1, false));
                    row.addContent(cell(qStats.getExecutionAvgTime() + " ms", 1, 1, false));
                    row.addContent(cell(qStats.getCacheHitCount() + "", 1, 1, false));
                    row.addContent(cell(qStats.getCacheMissCount() + "", 1, 1, false));
                    row.addContent(cell(qStats.getCachePutCount() + "", 1, 1, false));
                    subTable.addContent(row);
                }

                row = new TableRow();
                row.addContent(cell(subTable.toHtml(), 1, 2, true));
                table.addContent(row);
            }

            row = new TableRow();
            row.addContent(cell("<hr>", 1, 2, false));
            table.addContent(row);

            table.addContent(linkToTop);
        }

        // Add to generated HTML
        hibStats.append(table.toHtml());
    } catch (Exception e) {
        hibStats.append("Exception occured: " + e.getMessage());
        e.printStackTrace();
    }

    return hibStats.toString();
}