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

The following examples show how to use org.hibernate.stat.Statistics#getQueryStatistics() . 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 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 2
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 3
Source File: StatsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testQueryStatGathering() {
		Statistics stats = getSessions().getStatistics();
		stats.clear();

		Session s = openSession();
		Transaction tx = s.beginTransaction();
		fillDb(s);
		tx.commit();
		s.close();

		s = openSession();
		tx = s.beginTransaction();
		final String continents = "from Continent";
		int results = s.createQuery( continents ).list().size();
		QueryStatistics continentStats = stats.getQueryStatistics( continents );
		assertNotNull( "stats were null",  continentStats );
		assertEquals( "unexpected execution count", 1, continentStats.getExecutionCount() );
		assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() );
		long maxTime = continentStats.getExecutionMaxTime();
		assertEquals( maxTime, stats.getQueryExecutionMaxTime() );
//		assertEquals( continents, stats.getQueryExecutionMaxTimeQueryString() );

		Iterator itr = s.createQuery( continents ).iterate();
		// iterate() should increment the execution count
		assertEquals( "unexpected execution count", 2, continentStats.getExecutionCount() );
		// but should not effect the cumulative row count
		assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() );
		Hibernate.close( itr );

		ScrollableResults scrollableResults = s.createQuery( continents ).scroll();
		// same deal with scroll()...
		assertEquals( "unexpected execution count", 3, continentStats.getExecutionCount() );
		assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() );
		scrollableResults.close();
		tx.commit();
		s.close();

		// explicitly check that statistics for "split queries" get collected
		// under the original query
		stats.clear();
		s = openSession();
		tx = s.beginTransaction();
		final String localities = "from Locality";
		results = s.createQuery( localities ).list().size();
		QueryStatistics localityStats = stats.getQueryStatistics( localities );
		assertNotNull( "stats were null",  localityStats );
		// ...one for each split query
		assertEquals( "unexpected execution count", 2, localityStats.getExecutionCount() );
		assertEquals( "unexpected row count", results, localityStats.getExecutionRowCount() );
		maxTime = localityStats.getExecutionMaxTime();
		assertEquals( maxTime, stats.getQueryExecutionMaxTime() );
//		assertEquals( localities, stats.getQueryExecutionMaxTimeQueryString() );
		tx.commit();
		s.close();
		assertFalse( s.isOpen() );

		// native sql queries
		stats.clear();
		s = openSession();
		tx = s.beginTransaction();
		final String sql = "select id, name from Country";
		results = s.createSQLQuery( sql ).addEntity( Country.class ).list().size();
		QueryStatistics sqlStats = stats.getQueryStatistics( sql );
		assertNotNull( "sql stats were null", sqlStats );
		assertEquals( "unexpected execution count", 1, sqlStats.getExecutionCount() );
		assertEquals( "unexpected row count", results, sqlStats.getExecutionRowCount() );
		maxTime = sqlStats.getExecutionMaxTime();
		assertEquals( maxTime, stats.getQueryExecutionMaxTime() );
//		assertEquals( sql, stats.getQueryExecutionMaxTimeQueryString() );
		tx.commit();
		s.close();

		s = openSession();
		tx = s.beginTransaction();
		cleanDb( s );
		tx.commit();
		s.close();
	}