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

The following examples show how to use org.hibernate.stat.Statistics#getQueries() . 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: DefaultInQueryPlanCacheTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Test
public void testInQueryCachePlan() {
    SessionFactory sessionFactory = entityManagerFactory().unwrap(SessionFactory.class);
    Statistics statistics = sessionFactory.getStatistics();
    statistics.clear();

    doInJPA(entityManager -> {
        for (int i = 1; i < 16; i++) {
            getPostByIds(
                entityManager,
                IntStream.range(1, i + 1).boxed().toArray(Integer[]::new)
            );
        }
    });

    assertEquals(16L, statistics.getQueryPlanCacheMissCount());

    for (String query : statistics.getQueries()) {
        LOGGER.info("Executed query: {}", query);
    }
}
 
Example 2
Source File: DefaultInQueryPlanCacheTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Test
public void testJPQL() {
    SessionFactory sessionFactory = entityManagerFactory().unwrap(SessionFactory.class);
    Statistics statistics = sessionFactory.getStatistics();
    statistics.clear();

    doInJPA(entityManager -> {
        List<Post> posts = entityManager.createQuery(
            "select p " +
            "from Post p " +
            "where p.id in :ids", Post.class)
        .setParameter("ids", Arrays.asList(1, 2, 3))
        .getResultList();
    });

    for (String query : statistics.getQueries()) {
        LOGGER.info("Executed query: {}", query);
    }
}
 
Example 3
Source File: DefaultInQueryPlanCacheTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Test
public void testCriteriaAPI() {
    SessionFactory sessionFactory = entityManagerFactory().unwrap(SessionFactory.class);
    Statistics statistics = sessionFactory.getStatistics();
    statistics.clear();

    doInJPA(entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Post> criteria = builder.createQuery(Post.class);
        Root<Post> fromPost = criteria.from(Post.class);

        criteria.where(builder.in(fromPost.get("id")).value(Arrays.asList(1, 2, 3)));
        List<Post> posts = entityManager.createQuery(criteria).getResultList();
    });

    for (String query : statistics.getQueries()) {
        LOGGER.info("Executed query: {}", query);
    }
}
 
Example 4
Source File: HibernateStatisticsCollector.java    From client_java with Apache License 2.0 5 votes vote down vote up
private void addMetricsForQuery(PerQuerySamples samples, ValueProviderPerQuery provider) {

    for (Entry<String, SessionFactory> entry : sessionFactories.entrySet()) {
      SessionFactory sessionFactory = entry.getValue();
      Statistics stats = sessionFactory.getStatistics();
      String unitName = entry.getKey();

      for (String query : stats.getQueries()) {
        samples.addMetric(Arrays.asList(unitName, query), provider.getValue(stats, query));
      }
    }
  }
 
Example 5
Source File: HibernateStatsReporter.java    From keycloak with Apache License 2.0 5 votes vote down vote up
protected void logQueries(StringBuilder builder, String lineSep, Statistics stats) {
    builder.append("Important queries statistics: ").append(lineSep).append(lineSep);
    for (String query : stats.getQueries()) {
        QueryStatistics queryStats = stats.getQueryStatistics(query);

        if (queryStats.getExecutionCount() > LIMIT || (queryStats.getExecutionCount() * queryStats.getExecutionAvgTime() > LIMIT)) {
            builder.append(query).append(lineSep)
                    .append("executionCount=").append(queryStats.getExecutionCount()).append(lineSep)
                    .append("executionAvgTime=").append(queryStats.getExecutionAvgTime()).append(" ms").append(lineSep)
                    .append(lineSep)
                    .append(lineSep);
        }
    }
}
 
Example 6
Source File: HibernateQueryMetrics.java    From micrometer with Apache License 2.0 4 votes vote down vote up
void registerQueryMetric(Statistics statistics) {
    for (String query : statistics.getQueries()) {
        QueryStatistics queryStatistics = statistics.getQueryStatistics(query);

        FunctionCounter.builder("hibernate.query.cache.requests", queryStatistics, QueryStatistics::getCacheHitCount)
                .tags(tags)
                .tags("result", "hit", "query", query)
                .description("Number of query cache hits")
                .register(meterRegistry);

        FunctionCounter.builder("hibernate.query.cache.requests", queryStatistics, QueryStatistics::getCacheMissCount)
                .tags(tags)
                .tags("result", "miss", "query", query)
                .description("Number of query cache misses")
                .register(meterRegistry);

        FunctionCounter.builder("hibernate.query.cache.puts", queryStatistics, QueryStatistics::getCachePutCount)
                .tags(tags)
                .tags("query", query)
                .description("Number of cache puts for a query")
                .register(meterRegistry);

        FunctionTimer.builder("hibernate.query.execution.total", queryStatistics, QueryStatistics::getExecutionCount, QueryStatistics::getExecutionTotalTime, TimeUnit.MILLISECONDS)
                .tags(tags)
                .tags("query", query)
                .description("Query executions")
                .register(meterRegistry);

        TimeGauge.builder("hibernate.query.execution.max", queryStatistics, TimeUnit.MILLISECONDS, QueryStatistics::getExecutionMaxTime)
                .tags(tags)
                .tags("query", query)
                .description("Query maximum execution time")
                .register(meterRegistry);

        TimeGauge.builder("hibernate.query.execution.min", queryStatistics, TimeUnit.MILLISECONDS, QueryStatistics::getExecutionMinTime)
                .tags(tags)
                .tags("query", query)
                .description("Query minimum execution time")
                .register(meterRegistry);

        FunctionCounter.builder("hibernate.query.execution.rows", queryStatistics, QueryStatistics::getExecutionRowCount)
                .tags(tags)
                .tags("query", query)
                .description("Number of rows processed for a query")
                .register(meterRegistry);
    }
}
 
Example 7
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;
}