HibernateMySQLBenchmark.java from Texai at Krugle
Show HibernateMySQLBenchmark.java syntax highlighted
/*
* HibernateMySQLBenchmark.java
*
* Created on March 25, 2007, 6:18 PM
*
* Description: Benchmarks the Hibernate/MySQL knowledge object store.
*
* Copyright (C) 2007 Stephen L. Reed.
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.texai.kb.conversion;
import java.util.List;
import java.util.Random;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.Persistence;
import net.sf.ehcache.CacheManager;
import org.apache.log4j.Logger;
import org.texai.kb.CacheInitializer;
import org.texai.kb.Constants;
import org.texai.kb.ejb.entity.AtomicTerm;
import org.texai.kb.ejb.session.DomainEntityManagerBean;
import org.texai.util.TexaiException;
/**
*
* @author reed
*/
public final class HibernateMySQLBenchmark {
/** the entity manager factory */
private EntityManagerFactory entityManagerFactory;
/** the entity manager */
private EntityManager entityManager;
/** the domain entity manager */
private DomainEntityManagerBean domainEntityManager;
/** the entity transaction */
private EntityTransaction entityTransaction;
/** the log4j logger */
private static final Logger LOGGER = Logger.getLogger(HibernateMySQLBenchmark.class.getName());
/** the number of items processed */
private int nbrItemsProcessed = 0;
/** the list of hibernate term ids for a particular term type */
private List hibernateTermIds;
/** Creates a new instance of HibernateMySQLBenchmark. */
public HibernateMySQLBenchmark() {
super();
}
/** Initializes the application and injects the dependencies for out-of-the-container execution of J2EE session beans. */
private void initialize() {
entityManagerFactory = Persistence.createEntityManagerFactory(Constants.TEST_PERSISTENCE_UNIT_NAME);
entityManager = entityManagerFactory.createEntityManager();
CacheInitializer.initializeCaches();
domainEntityManager = new DomainEntityManagerBean();
domainEntityManager.setEntityManager(entityManager);
domainEntityManager.injectSharedBeanDependencies();
entityTransaction = entityManager.getTransaction();
}
/** Finds all the atomic term ids. */
private void findAllAtomicTermIds() {
LOGGER.info("gathering atomic term ids");
hibernateTermIds =
entityManager.createNamedQuery(Constants.QUERY_FIND_ATOMIC_TERM_IDS)
.setFlushMode(FlushModeType.COMMIT)
.setHint("org.hibernate.readOnly", Boolean.TRUE)
.getResultList();
}
/** Converts the atomic terms. */
private void benchmarkAtomicTerms() {
entityTransaction.begin();
final int NBR_TO_BENCHMARK = 5000;
LOGGER.info("loading " + NBR_TO_BENCHMARK + " random atomic term ids");
final Random random = new Random();
long beginTimeMillis = System.currentTimeMillis();
for (int i = 0; i < NBR_TO_BENCHMARK; i++) {
int randomInt = random.nextInt();
if (randomInt < 0) {
randomInt = -randomInt;
}
randomInt = randomInt % hibernateTermIds.size();
if (randomInt == hibernateTermIds.size()) {
randomInt = randomInt - 1;
}
final Long termId = (Long) hibernateTermIds.get(randomInt);
final AtomicTerm atomicTerm = entityManager.find(AtomicTerm.class, termId);
final String atomicTermName = atomicTerm.toString();
LOGGER.debug(atomicTermName);
LOGGER.debug(termId);
nbrItemsProcessed++;
if (nbrItemsProcessed % 50 == 0) {
entityTransaction.commit();
entityManager.clear();
entityTransaction.begin();
}
}
long endTimeMillis = System.currentTimeMillis();
long nbrPerSecond = (NBR_TO_BENCHMARK * 1000) / (endTimeMillis - beginTimeMillis);
LOGGER.info(nbrPerSecond + " MySQL/Hibernate atomic term loads per second");
}
/** Commits a group of persisted domain entities to the knowledge base.
*
* @param count the count to log
*/
private void commit(final int count) {
LOGGER.debug("");
LOGGER.debug("*** committing *** " + count);
LOGGER.debug("");
entityTransaction.commit();
entityManager.clear();
entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
}
/** Finalizes this application. */
private void finalization() {
entityTransaction.commit();
CacheManager.getInstance().shutdown();
entityManager.close();
entityManagerFactory.close();
LOGGER.info("HibernateMySQLBenchmark completed");
}
/** Executes this application.
*
* @param args the command line arguments (unused)
*/
public static void main(final String[] args) {
final HibernateMySQLBenchmark hibernateMySQLBenchmark = new HibernateMySQLBenchmark();
hibernateMySQLBenchmark.initialize();
try {
hibernateMySQLBenchmark.findAllAtomicTermIds();
hibernateMySQLBenchmark.benchmarkAtomicTerms();
} catch (final TexaiException ex) {
LOGGER.error(ex);
ex.printStackTrace(System.err);
} catch (final NullPointerException ex) {
LOGGER.error(ex);
ex.printStackTrace(System.err);
}
hibernateMySQLBenchmark.finalization();
}
}
See more files for this project here