use of org.hibernate.stat.QueryStatistics in project bw-calendar-engine by Bedework.
the class DbStatistics method queryStats.
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()));
}
use of org.hibernate.stat.QueryStatistics in project hibernate-orm by hibernate.
the class QueryCacheTest method testQueryCacheInvalidation.
@Test
public void testQueryCacheInvalidation() throws Exception {
sessionFactory().getCache().evictQueryRegions();
final StatisticsImplementor statistics = sessionFactory().getStatistics();
statistics.clear();
final String queryString = "from Item i where i.name='widget'";
final QueryStatistics qs = statistics.getQueryStatistics(queryString);
final EntityStatistics es = statistics.getEntityStatistics(Item.class.getName());
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery(queryString).setCacheable(true).list();
Item i = new Item();
i.setName("widget");
i.setDescription("A really top-quality, full-featured widget.");
s.save(i);
t.commit();
s.close();
// hit -> 0
// miss -> 1
// put -> 1
assertEquals(es.getInsertCount(), 1);
assertEquals(es.getUpdateCount(), 0);
assertEquals(statistics.getQueryCacheHitCount(), 0);
assertEquals(qs.getCacheHitCount(), 0);
assertEquals(statistics.getQueryCacheMissCount(), 1);
assertEquals(qs.getCacheMissCount(), 1);
assertEquals(statistics.getQueryCachePutCount(), 1);
assertEquals(qs.getCachePutCount(), 1);
assertEquals(statistics.getQueryExecutionCount(), 1);
assertEquals(qs.getExecutionCount(), 1);
assertEquals(statistics.getEntityFetchCount(), 0);
Thread.sleep(200);
s = openSession();
t = s.beginTransaction();
List result = s.createQuery(queryString).setCacheable(true).list();
assertEquals(result.size(), 1);
t.commit();
s.close();
// hit -> 0
// miss -> 2
// put -> 2
assertEquals(es.getInsertCount(), 1);
assertEquals(es.getUpdateCount(), 0);
assertEquals(statistics.getQueryCacheHitCount(), 0);
assertEquals(qs.getCacheHitCount(), 0);
assertEquals(statistics.getQueryCacheMissCount(), 2);
assertEquals(qs.getCacheMissCount(), 2);
assertEquals(statistics.getQueryCachePutCount(), 2);
assertEquals(qs.getCachePutCount(), 2);
assertEquals(statistics.getQueryExecutionCount(), 2);
assertEquals(qs.getExecutionCount(), 2);
assertEquals(statistics.getEntityFetchCount(), 0);
s = openSession();
t = s.beginTransaction();
result = s.createQuery(queryString).setCacheable(true).list();
assertEquals(result.size(), 1);
t.commit();
s.close();
// hit -> 1
// miss -> 2
// put -> 2
assertEquals(es.getInsertCount(), 1);
assertEquals(es.getUpdateCount(), 0);
assertEquals(statistics.getQueryCacheHitCount(), 1);
assertEquals(qs.getCacheHitCount(), 1);
assertEquals(statistics.getQueryCacheMissCount(), 2);
assertEquals(qs.getCacheMissCount(), 2);
assertEquals(statistics.getQueryCachePutCount(), 2);
assertEquals(qs.getCachePutCount(), 2);
assertEquals(statistics.getQueryExecutionCount(), 2);
assertEquals(qs.getExecutionCount(), 2);
assertEquals(statistics.getEntityFetchCount(), 0);
assertEquals(qs.getCacheHitCount(), 1);
assertEquals(statistics.getEntityFetchCount(), 0);
s = openSession();
t = s.beginTransaction();
result = s.createQuery(queryString).setCacheable(true).list();
assertEquals(result.size(), 1);
i = (Item) result.get(0);
assertTrue(Hibernate.isInitialized(i));
assertTrue(s.contains(i));
i.setName("Widget");
s.flush();
t.commit();
s.close();
// hit -> 2
// miss -> 2
// put -> 2
//
// + another invalidation
assertEquals(es.getInsertCount(), 1);
assertEquals(es.getUpdateCount(), 1);
assertEquals(statistics.getQueryCacheHitCount(), 2);
assertEquals(qs.getCacheHitCount(), 2);
assertEquals(statistics.getQueryCacheMissCount(), 2);
assertEquals(qs.getCacheMissCount(), 2);
assertEquals(statistics.getQueryCachePutCount(), 2);
assertEquals(qs.getCachePutCount(), 2);
assertEquals(statistics.getQueryExecutionCount(), 2);
assertEquals(qs.getExecutionCount(), 2);
assertEquals(statistics.getEntityFetchCount(), 0);
Thread.sleep(200);
s = openSession();
t = s.beginTransaction();
result = s.createQuery(queryString).setCacheable(true).list();
i = (Item) s.get(Item.class, new Long(i.getId()));
s.delete(i);
t.commit();
s.close();
// hit -> 2
// miss -> 3
// put -> 3
assertEquals(es.getInsertCount(), 1);
assertEquals(es.getUpdateCount(), 1);
assertEquals(statistics.getQueryCacheHitCount(), 2);
assertEquals(qs.getCacheHitCount(), 2);
assertEquals(statistics.getQueryCacheMissCount(), 3);
assertEquals(qs.getCacheMissCount(), 3);
assertEquals(statistics.getQueryCachePutCount(), 3);
assertEquals(qs.getCachePutCount(), 3);
assertEquals(statistics.getQueryExecutionCount(), 3);
assertEquals(qs.getExecutionCount(), 3);
assertEquals(statistics.getEntityFetchCount(), 0);
assertEquals(es.getFetchCount(), 0);
assertEquals(qs.getCacheHitCount(), 2);
assertEquals(qs.getCacheMissCount(), 3);
assertEquals(qs.getCachePutCount(), 3);
assertEquals(qs.getExecutionCount(), 3);
// check that it was being cached
assertEquals(es.getFetchCount(), 0);
}
use of org.hibernate.stat.QueryStatistics in project hibernate-orm by hibernate.
the class QueryCacheTest method testProjectionCache.
@Test
public void testProjectionCache() throws Exception {
sessionFactory().getCache().evictQueryRegions();
sessionFactory().getStatistics().clear();
final String queryString = "select i.description as desc from Item i where i.name='widget'";
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery(queryString).setCacheable(true).list();
Item i = new Item();
i.setName("widget");
i.setDescription("A really top-quality, full-featured widget.");
s.save(i);
t.commit();
s.close();
QueryStatistics qs = s.getSessionFactory().getStatistics().getQueryStatistics(queryString);
EntityStatistics es = s.getSessionFactory().getStatistics().getEntityStatistics(Item.class.getName());
assertEquals(qs.getCacheHitCount(), 0);
assertEquals(qs.getCacheMissCount(), 1);
assertEquals(qs.getCachePutCount(), 1);
Thread.sleep(200);
s = openSession();
t = s.beginTransaction();
List result = s.createQuery(queryString).setCacheable(true).list();
assertEquals(result.size(), 1);
assertEquals(i.getDescription(), (result.get(0)));
t.commit();
s.close();
assertEquals(qs.getCacheHitCount(), 0);
assertEquals(qs.getCacheMissCount(), 2);
assertEquals(qs.getCachePutCount(), 2);
s = openSession();
t = s.beginTransaction();
result = s.createQuery(queryString).setCacheable(true).list();
assertEquals(result.size(), 1);
assertEquals(i.getDescription(), result.get(0));
t.commit();
s.close();
assertEquals(qs.getCacheHitCount(), 1);
assertEquals(qs.getCacheMissCount(), 2);
assertEquals(qs.getCachePutCount(), 2);
s = openSession();
t = s.beginTransaction();
result = s.createQuery(queryString).setCacheable(true).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
assertEquals(result.size(), 1);
Map m = (Map) result.get(0);
assertEquals(1, m.size());
assertEquals(i.getDescription(), m.get("desc"));
t.commit();
s.close();
assertEquals("hit count should go up since data is not transformed until after it is cached", qs.getCacheHitCount(), 2);
assertEquals(qs.getCacheMissCount(), 2);
assertEquals(qs.getCachePutCount(), 2);
s = openSession();
t = s.beginTransaction();
result = s.createQuery(queryString).setCacheable(true).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
assertEquals(result.size(), 1);
m = (Map) result.get(0);
assertEquals(1, m.size());
assertEquals(i.getDescription(), m.get("desc"));
t.commit();
s.close();
assertEquals("hit count should go up since data is not transformed until after it is cachedr", qs.getCacheHitCount(), 3);
assertEquals(qs.getCacheMissCount(), 2);
assertEquals(qs.getCachePutCount(), 2);
s = openSession();
t = s.beginTransaction();
result = s.createQuery(queryString).setCacheable(true).list();
assertEquals(result.size(), 1);
assertTrue(Hibernate.isInitialized(result.get(0)));
i = (Item) s.get(Item.class, new Long(i.getId()));
i.setName("widget");
i.setDescription("A middle-quality widget.");
t.commit();
s.close();
assertEquals(qs.getCacheHitCount(), 4);
assertEquals(qs.getCacheMissCount(), 2);
assertEquals(qs.getCachePutCount(), 2);
Thread.sleep(200);
s = openSession();
t = s.beginTransaction();
result = s.createQuery(queryString).setCacheable(true).list();
assertEquals(result.size(), 1);
i = (Item) s.get(Item.class, new Long(i.getId()));
assertEquals(result.get(0), "A middle-quality widget.");
assertEquals(qs.getCacheHitCount(), 4);
assertEquals(qs.getCacheMissCount(), 3);
assertEquals(qs.getCachePutCount(), 3);
s.delete(i);
t.commit();
s.close();
assertEquals(qs.getCacheHitCount(), 4);
assertEquals(qs.getCacheMissCount(), 3);
assertEquals(qs.getCachePutCount(), 3);
assertEquals(qs.getExecutionCount(), 3);
// check that it was being cached
assertEquals(es.getFetchCount(), 0);
}
use of org.hibernate.stat.QueryStatistics in project hibernate-orm by hibernate.
the class HibernateCacheTest method testGeneralUsage.
@Test
public void testGeneralUsage() {
EventManager mgr = new EventManager(sessionFactory());
Statistics stats = sessionFactory().getStatistics();
// create 3 persons Steve, Orion, Tim
Person stevePerson = new Person();
stevePerson.setFirstname("Steve");
stevePerson.setLastname("Harris");
Long steveId = mgr.createAndStorePerson(stevePerson);
mgr.addEmailToPerson(steveId, "steve@tc.com");
mgr.addEmailToPerson(steveId, "sharrif@tc.com");
mgr.addTalismanToPerson(steveId, "rabbit foot");
mgr.addTalismanToPerson(steveId, "john de conqueroo");
PhoneNumber p1 = new PhoneNumber();
p1.setNumberType("Office");
p1.setPhone(111111);
mgr.addPhoneNumberToPerson(steveId, p1);
PhoneNumber p2 = new PhoneNumber();
p2.setNumberType("Home");
p2.setPhone(222222);
mgr.addPhoneNumberToPerson(steveId, p2);
Person orionPerson = new Person();
orionPerson.setFirstname("Orion");
orionPerson.setLastname("Letizi");
Long orionId = mgr.createAndStorePerson(orionPerson);
mgr.addEmailToPerson(orionId, "orion@tc.com");
mgr.addTalismanToPerson(orionId, "voodoo doll");
Long timId = mgr.createAndStorePerson("Tim", "Teck");
mgr.addEmailToPerson(timId, "teck@tc.com");
mgr.addTalismanToPerson(timId, "magic decoder ring");
Long engMeetingId = mgr.createAndStoreEvent("Eng Meeting", stevePerson, new Date());
mgr.addPersonToEvent(steveId, engMeetingId);
mgr.addPersonToEvent(orionId, engMeetingId);
mgr.addPersonToEvent(timId, engMeetingId);
Long docMeetingId = mgr.createAndStoreEvent("Doc Meeting", orionPerson, new Date());
mgr.addPersonToEvent(steveId, docMeetingId);
mgr.addPersonToEvent(orionId, docMeetingId);
for (Event event : (List<Event>) mgr.listEvents()) {
mgr.listEmailsOfEvent(event.getId());
}
QueryStatistics queryStats = stats.getQueryStatistics("from Event");
assertThat("Cache Miss Count", queryStats.getCacheMissCount(), equalTo(1L));
assertThat("Cache Hit Count", queryStats.getCacheHitCount(), equalTo(0L));
assertThat("Cache Put Count", queryStats.getCachePutCount(), equalTo(1L));
}
use of org.hibernate.stat.QueryStatistics in project hibernate-orm by hibernate.
the class StatsTest method testQueryStatGathering.
//
// @Test
// @SuppressWarnings( {"UnusedAssignment"})
// public void testCollectionFetchVsLoad() throws Exception {
// SessionFactory sf = buildBaseConfiguration()
// .setProperty( AvailableSettings.HBM2DDL_AUTO, "create-drop" )
// .buildSessionFactory();
//
// Session s = sf.openSession();
// Transaction tx = s.beginTransaction();
// Continent europe = fillDb(s);
// tx.commit();
// s.close();
//
// s = sf.openSession();
// tx = s.beginTransaction();
// assertEquals(0, sf.getStatistics().getCollectionLoadCount() );
// assertEquals(0, sf.getStatistics().getCollectionFetchCount() );
// Continent europe2 = (Continent) s.get( Continent.class, europe.getId() );
// assertEquals("Lazy true: no collection should be loaded", 0, sf.getStatistics().getCollectionLoadCount() );
// assertEquals( 0, sf.getStatistics().getCollectionFetchCount() );
// europe2.getCountries().size();
// assertEquals( 1, sf.getStatistics().getCollectionLoadCount() );
// assertEquals("Explicit fetch of the collection state", 1, sf.getStatistics().getCollectionFetchCount() );
// tx.commit();
// s.close();
//
// sf.getStatistics().clear();
//
// s = sf.openSession();
// tx = s.beginTransaction();
// europe = fillDb(s);
// tx.commit();
// s.clear();
// tx = s.beginTransaction();
// assertEquals( 0, sf.getStatistics().getCollectionLoadCount() );
// assertEquals( 0, sf.getStatistics().getCollectionFetchCount() );
// europe2 = (Continent) s.createQuery(
// "from " + Continent.class.getName() + " a join fetch a.countries where a.id = " + europe.getId()
// ).uniqueResult();
// assertEquals( 1, sf.getStatistics().getCollectionLoadCount() );
// assertEquals( "collection should be loaded in the same query as its parent", 0, sf.getStatistics().getCollectionFetchCount() );
// tx.commit();
// s.close();
//
// // open a new SF
// sf.close();
// Configuration cfg = buildBaseConfiguration().setProperty( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
// cfg.buildMappings();
// Collection coll = cfg.getCollectionMapping(Continent.class.getName() + ".countries");
// coll.setFetchMode(FetchMode.JOIN);
// coll.setLazy(false);
// sf = cfg.buildSessionFactory();
//
// s = sf.openSession();
// tx = s.beginTransaction();
// europe = fillDb(s);
// tx.commit();
// s.close();
//
// s = sf.openSession();
// tx = s.beginTransaction();
// assertEquals( 0, sf.getStatistics().getCollectionLoadCount() );
// assertEquals( 0, sf.getStatistics().getCollectionFetchCount() );
// europe2 = (Continent) s.get( Continent.class, europe.getId() );
// assertEquals( 1, sf.getStatistics().getCollectionLoadCount() );
// assertEquals( "Should do direct load, not indirect second load when lazy false and JOIN", 0, sf.getStatistics().getCollectionFetchCount() );
// tx.commit();
// s.close();
// sf.close();
//
// // open yet another SF
// sf.close();
// cfg = buildBaseConfiguration().setProperty( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
// cfg.buildMappings();
// coll = cfg.getCollectionMapping( Continent.class.getName() + ".countries" );
// coll.setFetchMode(FetchMode.SELECT);
// coll.setLazy(false);
// sf = cfg.buildSessionFactory();
//
// s = sf.openSession();
// tx = s.beginTransaction();
// europe = fillDb(s);
// tx.commit();
// s.close();
//
// s = sf.openSession();
// tx = s.beginTransaction();
// assertEquals( 0, sf.getStatistics().getCollectionLoadCount() );
// assertEquals( 0, sf.getStatistics().getCollectionFetchCount() );
// europe2 = (Continent) s.get( Continent.class, europe.getId() );
// assertEquals( 1, sf.getStatistics().getCollectionLoadCount() );
// assertEquals( "Should do explicit collection load, not part of the first one", 1, sf.getStatistics().getCollectionFetchCount() );
// for ( Object o : europe2.getCountries() ) {
// s.delete( o );
// }
// cleanDb( s );
// tx.commit();
// s.close();
//
// sf.close();
// }
@Test
public void testQueryStatGathering() {
SessionFactory sf = buildBaseConfiguration().setProperty(AvailableSettings.HBM2DDL_AUTO, "create-drop").buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
fillDb(s);
tx.commit();
s.close();
s = sf.openSession();
tx = s.beginTransaction();
final String continents = "from Continent";
int results = s.createQuery(continents).list().size();
QueryStatistics continentStats = sf.getStatistics().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, sf.getStatistics().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());
// if data is not read before closing the ResultSet
while (scrollableResults.next()) {
// do nothing
}
scrollableResults.close();
tx.commit();
s.close();
// explicitly check that statistics for "split queries" get collected
// under the original query
sf.getStatistics().clear();
s = sf.openSession();
tx = s.beginTransaction();
final String localities = "from Locality";
results = s.createQuery(localities).list().size();
QueryStatistics localityStats = sf.getStatistics().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, sf.getStatistics().getQueryExecutionMaxTime());
// assertEquals( localities, stats.getQueryExecutionMaxTimeQueryString() );
tx.commit();
s.close();
assertFalse(s.isOpen());
// native sql queries
sf.getStatistics().clear();
s = sf.openSession();
tx = s.beginTransaction();
final String sql = "select id, name from Country";
results = s.createSQLQuery(sql).addEntity(Country.class).list().size();
QueryStatistics sqlStats = sf.getStatistics().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, sf.getStatistics().getQueryExecutionMaxTime());
// assertEquals( sql, stats.getQueryExecutionMaxTimeQueryString() );
tx.commit();
s.close();
s = sf.openSession();
tx = s.beginTransaction();
cleanDb(s);
tx.commit();
s.close();
sf.close();
}
Aggregations