HHH-9406 - Add method for get avg execution time for offen but really fast queries (where avg execution time ~0-1 ms)

This commit is contained in:
a.navrotskiy 2014-09-18 16:57:24 +04:00 committed by Vlad Mihalcea
parent 3ef05dea2f
commit e6d3ad5013
4 changed files with 70 additions and 7 deletions

View File

@ -31,4 +31,8 @@ public interface QueryStatistics extends Serializable {
long getExecutionMaxTime();
long getExecutionMinTime();
long getExecutionTotalTime();
double getExecutionAvgTimeAsDouble();
}

View File

@ -79,39 +79,52 @@ public class ConcurrentQueryStatisticsImpl extends CategorizedStatistics impleme
}
/**
* average time in ms taken by the excution of this query onto the DB
* average time in ms taken by the execution of this query onto the DB
*/
public long getExecutionAvgTime() {
return (long) getExecutionAvgTimeAsDouble();
}
/**
* average time in ms as double taken by the execution of this query onto the DB
*/
public double getExecutionAvgTimeAsDouble() {
// We write lock here to be sure that we always calculate the average time
// with all updates from the executed applied: executionCount and totalExecutionTime
// both used in the calculation
writeLock.lock();
try {
long avgExecutionTime = 0;
double avgExecutionTime = 0;
if (executionCount.get() > 0) {
avgExecutionTime = totalExecutionTime.get() / executionCount.get();
avgExecutionTime = totalExecutionTime.get() / (double) executionCount.get();
}
return avgExecutionTime;
}
finally {
} finally {
writeLock.unlock();
}
}
/**
* max time in ms taken by the excution of this query onto the DB
* max time in ms taken by the execution of this query onto the DB
*/
public long getExecutionMaxTime() {
return executionMaxTime.get();
}
/**
* min time in ms taken by the excution of this query onto the DB
* min time in ms taken by the execution of this query onto the DB
*/
public long getExecutionMinTime() {
return executionMinTime.get();
}
/**
* total time in ms taken by the execution of this query onto the DB
*/
public long getExecutionTotalTime() {
return totalExecutionTime.get();
}
/**
* add statistics report of a DB query
*

View File

@ -0,0 +1,46 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.stat.internal;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* <code>ConcurrentQueryStatisticsTest</code> -
*
* @author Vlad Mihalcea
*/
public class ConcurrentQueryStatisticsTest extends BaseUnitTestCase {
private ConcurrentQueryStatisticsImpl stats = new ConcurrentQueryStatisticsImpl(
"test" );
@Test
public void testStats() {
assertEquals( 0, stats.getExecutionTotalTime() );
assertEquals( Long.MAX_VALUE, stats.getExecutionMinTime() );
assertEquals( 0, stats.getExecutionMaxTime() );
assertEquals( 0, stats.getExecutionAvgTime() );
stats.executed( 1000, 12 );
assertEquals( 12, stats.getExecutionTotalTime() );
assertEquals( 12, stats.getExecutionMinTime() );
assertEquals( 12, stats.getExecutionMaxTime() );
assertEquals( 12, stats.getExecutionAvgTime() );
stats.executed( 200, 11 );
assertEquals( 23, stats.getExecutionTotalTime() );
assertEquals( 11, stats.getExecutionMinTime() );
assertEquals( 12, stats.getExecutionMaxTime() );
assertEquals( 11, stats.getExecutionAvgTime() );
assertEquals( 11.5, stats.getExecutionAvgTimeAsDouble(), 0.1 );
}
}