in performance/src/main/java/org/hibernate/ogm/perftest/mongodb/ogm/HibernateOgmCombinedBenchmark.java [122:183]
private void doCombinedFindAndUpdate(TestDataInserter inserter, Blackhole blackhole) throws NotSupportedException, SystemException, RollbackException,
HeuristicMixedException, HeuristicRollbackException {
EntityManagerFactoryHolder stateHolder = inserter.stateHolder;
EntityManager entityManager = stateHolder.entityManagerFactory.createEntityManager();
stateHolder.transactionManager.begin();
entityManager.joinTransaction();
AuthorWithSequence author = null;
int updates = 0;
// do GETS_PER_INVOCATION find-by-id
for ( int i = 0; i < GETS_PER_INVOCATION; i++ ) {
long id = stateHolder.rand.nextInt( NUMBER_OF_TEST_ENTITIES - 1 ) + 1;
author = entityManager.find( AuthorWithSequence.class, id );
if ( author == null ) {
throw new IllegalArgumentException( "Couldn't find entry with id " + id );
}
blackhole.consume( author.getFname() );
// do UPDATES_PER_INVOCATION updates
if ( updates < UPDATES_PER_INVOCATION ) {
author.setLname( "Royce" );
updates++;
}
}
// do FINDS_PER_INVOCATION queries
for ( int i = 0; i < FINDS_PER_INVOCATION; i++ ) {
int mName = stateHolder.rand.nextInt( 26 );
TypedQuery<AuthorWithSequence> query = entityManager.createNamedQuery( "author_by_mname", AuthorWithSequence.class );
query.setMaxResults( 50 );
query.setParameter( "mname", "" + mName );
List<AuthorWithSequence> authors = query.getResultList();
for ( AuthorWithSequence foundAuthor : authors ) {
blackhole.consume( foundAuthor.getLname() );
}
}
// do INSERTS_PER_INVOCATION queries
for ( int i = 0; i < INSERTS_PER_INVOCATION; i++ ) {
AuthorWithSequence newAuthor = new AuthorWithSequence();
newAuthor.setBio( "This is a decent size bio made of " + stateHolder.rand.nextDouble() + " stuffs" );
newAuthor.setDob( new Date() );
newAuthor.setFname( "Jessie " + stateHolder.rand.nextInt() );
newAuthor.setLname( "Landis " + stateHolder.rand.nextInt() );
newAuthor.setMname( "" + stateHolder.rand.nextInt( 26 ) );
entityManager.persist( newAuthor );
}
stateHolder.transactionManager.commit();
entityManager.close();
}