Search in sources :

Example 81 with Statistics

use of org.hibernate.stat.Statistics in project microservices by pwillhan.

the class SecondLevel method cacheModes.

@Test
public void cacheModes() throws Exception {
    CacheTestData testData = storeTestData();
    Long USER_ID = testData.users.getFirstId();
    Long ITEM_ID = testData.items.getFirstId();
    UserTransaction tx = TM.getUserTransaction();
    try {
        {
            tx.begin();
            EntityManager em = JPA.createEntityManager();
            Statistics stats = JPA.getEntityManagerFactory().unwrap(SessionFactory.class).getStatistics();
            SecondLevelCacheStatistics itemCacheStats = stats.getSecondLevelCacheStatistics(Item.class.getName());
            // Bypass the cache when retrieving an entity instance by identifier
            {
                Map<String, Object> properties = new HashMap<String, Object>();
                properties.put("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
                // Hit the database
                Item item = em.find(Item.class, ITEM_ID, properties);
                assertEquals(itemCacheStats.getHitCount(), 0);
            }
            // Bypass the cache when storing an entity instance
            assertEquals(itemCacheStats.getElementCountInMemory(), 3);
            em.setProperty("javax.persistence.cache.storeMode", CacheStoreMode.BYPASS);
            Item item = new Item(// ...
            "Some Item", CalendarUtil.TOMORROW.getTime(), em.find(User.class, USER_ID));
            // Not stored in the cache
            em.persist(item);
            em.flush();
            // Unchanged
            assertEquals(itemCacheStats.getElementCountInMemory(), 3);
            tx.commit();
            em.close();
        }
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.cache.Item) EntityManager(javax.persistence.EntityManager) HashMap(java.util.HashMap) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) NaturalIdCacheStatistics(org.hibernate.stat.NaturalIdCacheStatistics) Test(org.testng.annotations.Test) JPATest(org.jpwh.env.JPATest)

Example 82 with Statistics

use of org.hibernate.stat.Statistics in project openmrs-core by openmrs.

the class HibernateContextDAO method showUsageStatistics.

/**
 * Convenience method to print out the hibernate cache usage stats to the log
 */
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());
        }
    }
}
Also used : QueryStatistics(org.hibernate.stat.QueryStatistics) Statistics(org.hibernate.stat.Statistics) QueryStatistics(org.hibernate.stat.QueryStatistics)

Example 83 with Statistics

use of org.hibernate.stat.Statistics in project wildfly by wildfly.

the class SFSB2LC method sameSessionCheck.

/**
 * Checking entity 2LC in one EntityManager session
 * TODO: rewrite to separate transaction used to createEmployee, and either load entities outside tx or
 * start new tx for loading entities.  Maybe simplest to have calling code, handle creating the Employees first.
 * TODO: need conclusion to discussion about whether createEmployee should cause 2lc PutCount to be incremented,
 *       as no data is loaded into the cache.
 */
public String sameSessionCheck(String CACHE_REGION_NAME) {
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    CacheRegionStatistics emp2LCStats = stats.getDomainDataRegionStatistics(CACHE_REGION_NAME + "Employee");
    try {
        // add new entities and check if they are put in 2LC
        createEmployee(em, "Peter", "Ostrava", 2);
        createEmployee(em, "Tom", "Brno", 3);
        assertEquals("There are 2 puts in the 2LC" + generateEntityCacheStats(emp2LCStats), 2, emp2LCStats.getPutCount());
        // loading all Employee entities should put in 2LC all Employee
        List<?> empList = getAllEmployeesQuery(em);
        assertEquals("There are 2 entities.", empList.size(), 2);
        assertEquals("There are 2 entities in the 2LC" + generateEntityCacheStats(emp2LCStats), 2, emp2LCStats.getElementCountInMemory());
        // clear session
        em.clear();
        // entity should be loaded from 2L cache, we'are expecting hit in 2L cache
        Employee emp = getEmployee(em, 2);
        assertNotNull("Employee returned", emp);
        assertEquals("Expected 1 hit in cache" + generateEntityCacheStats(emp2LCStats), 1, emp2LCStats.getHitCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) QueryStatistics(org.hibernate.stat.QueryStatistics) CacheRegionStatistics(org.hibernate.stat.CacheRegionStatistics) Statistics(org.hibernate.stat.Statistics) CacheRegionStatistics(org.hibernate.stat.CacheRegionStatistics)

Example 84 with Statistics

use of org.hibernate.stat.Statistics in project wildfly by wildfly.

the class SFSB2LC method disabled2LCCheck.

/**
 * Check if disabling 2LC works as expected
 */
public String disabled2LCCheck() {
    EntityManager em = emfNo2LC.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    try {
        // check if entities are NOT cached in 2LC
        String[] names = stats.getSecondLevelCacheRegionNames();
        assertEquals("There aren't any 2LC regions.", 0, names.length);
        createEmployee(em, "Martin", "Prague 132", 1);
        assertEquals("There aren't any puts in the 2LC.", 0, stats.getSecondLevelCachePutCount());
        // check if queries are NOT cached in 2LC
        Employee emp = getEmployeeQuery(em, 1);
        assertNotNull("Employee returned", emp);
        assertEquals("There aren't any query puts in the 2LC.", 0, stats.getQueryCachePutCount());
        // cleanup
        em.remove(emp);
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) QueryStatistics(org.hibernate.stat.QueryStatistics) CacheRegionStatistics(org.hibernate.stat.CacheRegionStatistics) Statistics(org.hibernate.stat.Statistics)

Example 85 with Statistics

use of org.hibernate.stat.Statistics in project OpenClinica by OpenClinica.

the class RuleSetRuleDao method findByRuleSetStudyIdAndStatusAvail.

/**
 * Use this method carefully as we force an eager fetch. It is also annotated with
 * Transactional so it can be called from Quartz threads.
 * @param studyId
 * @return List of RuleSetRuleBeans
 */
@SuppressWarnings("unchecked")
@Transactional
public ArrayList<RuleSetRuleBean> findByRuleSetStudyIdAndStatusAvail(Integer studyId) {
    String query = "from " + getDomainClassName() + " ruleSetRule  where ruleSetRule.ruleSetBean.studyId = :studyId and status = :status ";
    org.hibernate.Query q = getCurrentSession().createQuery(query);
    q.setInteger("studyId", studyId);
    q.setParameter("status", org.akaza.openclinica.domain.Status.AVAILABLE);
    q.setCacheable(true);
    q.setCacheRegion(getDomainClassName());
    // JN: enabling statistics for hibernate queries etc... to monitor the performance
    Statistics stats = getSessionFactory().getStatistics();
    logger.info("EntityRuleSet" + stats.getEntityInsertCount());
    logger.info(stats.getQueryExecutionMaxTimeQueryString());
    logger.info("hit count" + stats.getSecondLevelCacheHitCount());
    stats.logSummary();
    ArrayList<RuleSetRuleBean> ruleSetRules = (ArrayList<RuleSetRuleBean>) q.list();
    // Forcing eager fetch of actions & their properties
    for (RuleSetRuleBean ruleSetRuleBean : ruleSetRules) {
        for (RuleActionBean action : ruleSetRuleBean.getActions()) {
            if (action instanceof RandomizeActionBean) {
                ((RandomizeActionBean) action).getProperties().size();
            }
            if (action instanceof InsertActionBean) {
                ((InsertActionBean) action).getProperties().size();
            }
            if (action instanceof ShowActionBean) {
                ((ShowActionBean) action).getProperties().size();
            }
            if (action instanceof HideActionBean) {
                ((HideActionBean) action).getProperties().size();
            }
            if (action instanceof EventActionBean) {
                ((EventActionBean) action).getProperties().size();
            }
        }
    }
    return ruleSetRules;
}
Also used : ShowActionBean(org.akaza.openclinica.domain.rule.action.ShowActionBean) HideActionBean(org.akaza.openclinica.domain.rule.action.HideActionBean) RuleActionBean(org.akaza.openclinica.domain.rule.action.RuleActionBean) RuleSetRuleBean(org.akaza.openclinica.domain.rule.RuleSetRuleBean) InsertActionBean(org.akaza.openclinica.domain.rule.action.InsertActionBean) ArrayList(java.util.ArrayList) Statistics(org.hibernate.stat.Statistics) EventActionBean(org.akaza.openclinica.domain.rule.action.EventActionBean) RandomizeActionBean(org.akaza.openclinica.domain.rule.action.RandomizeActionBean) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Statistics (org.hibernate.stat.Statistics)97 Test (org.junit.Test)59 Session (org.hibernate.Session)27 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)26 EntityManager (javax.persistence.EntityManager)21 QueryStatistics (org.hibernate.stat.QueryStatistics)19 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)14 Transaction (org.hibernate.Transaction)12 ArrayList (java.util.ArrayList)11 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)11 List (java.util.List)10 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)10 SessionFactory (org.hibernate.SessionFactory)9 CacheRegionStatistics (org.hibernate.stat.CacheRegionStatistics)9 ByRef (org.infinispan.commons.util.ByRef)8 TestForIssue (org.hibernate.testing.TestForIssue)6 Date (java.util.Date)5 UserTransaction (javax.transaction.UserTransaction)5 NaturalIdCacheStatistics (org.hibernate.stat.NaturalIdCacheStatistics)5 Test (org.testng.annotations.Test)5