/performance/src/main/java/org/hibernate/ogm/perftest/mongodb/ogm/HibernateOgmAssociationFindBenchmark.java

https://github.com/hibernate/hibernate-ogm · Java · 142 lines · 86 code · 35 blank · 21 comment · 11 complexity · 6fca76f68d1e650ccb31b11a0a7dbc2a MD5 · raw file

  1. /*
  2. * Hibernate OGM, Domain model persistence for NoSQL datastores
  3. *
  4. * License: GNU Lesser General Public License (LGPL), version 2.1 or later
  5. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  6. */
  7. package org.hibernate.ogm.perftest.mongodb.ogm;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.List;
  11. import javax.persistence.EntityManager;
  12. import org.hibernate.ogm.perftest.model.FieldOfScience;
  13. import org.hibernate.ogm.perftest.model.ScientistWithSequence;
  14. import org.openjdk.jmh.annotations.Benchmark;
  15. import org.openjdk.jmh.annotations.OperationsPerInvocation;
  16. import org.openjdk.jmh.annotations.Scope;
  17. import org.openjdk.jmh.annotations.Setup;
  18. import org.openjdk.jmh.annotations.State;
  19. import org.openjdk.jmh.infra.Blackhole;
  20. /**
  21. * A JMH benchmark measuring performance of association get operations using Hibernate OGM.
  22. *
  23. * @author Gunnar Morling
  24. */
  25. public class HibernateOgmAssociationFindBenchmark {
  26. private static final int NUMBER_OF_TEST_ENTITIES = 10000;
  27. private static final int NUMBER_OF_REFERENCABLE_ENTITIES = 100;
  28. /**
  29. * The number of operations to be performed with one entity manager. Using an EM only for one op is an anti-pattern,
  30. * but setting the number too high will result in an unrealistic result. Aim for a value to be expected during the
  31. * processing of one web request or similar.
  32. */
  33. private static final int OPERATIONS_PER_INVOCATION = 100;
  34. @State(Scope.Benchmark)
  35. public static class TestDataInserter {
  36. private EntityManagerFactoryHolder stateHolder;
  37. private final List<FieldOfScience> fieldsOfSciences = new ArrayList<FieldOfScience>( NUMBER_OF_REFERENCABLE_ENTITIES );
  38. @Setup
  39. public void insertTestData(EntityManagerFactoryHolder stateHolder) throws Exception {
  40. this.stateHolder = stateHolder;
  41. EntityManager entityManager = stateHolder.entityManagerFactory.createEntityManager();
  42. // insert referenced objects
  43. stateHolder.transactionManager.begin();
  44. entityManager.joinTransaction();
  45. for ( int i = 0; i < NUMBER_OF_REFERENCABLE_ENTITIES; i++ ) {
  46. FieldOfScience fieldOfScience = new FieldOfScience();
  47. fieldOfScience.setId( i );
  48. fieldOfScience.setComplexity( stateHolder.rand.nextDouble() );
  49. fieldOfScience.setName( "The dark sciences of " + stateHolder.rand.nextInt( 26 ) );
  50. entityManager.persist( fieldOfScience );
  51. fieldsOfSciences.add( fieldOfScience );
  52. }
  53. stateHolder.transactionManager.commit();
  54. // insert referencing objects
  55. for ( int i = 0; i <= NUMBER_OF_TEST_ENTITIES; i++ ) {
  56. if ( i % 1000 == 0 ) {
  57. stateHolder.transactionManager.begin();
  58. entityManager.joinTransaction();
  59. }
  60. ScientistWithSequence scientist = new ScientistWithSequence();
  61. scientist.setBio( "This is a decent size bio made of " + stateHolder.rand.nextDouble() + " stuffs" );
  62. scientist.setDob( new Date() );
  63. scientist.setName( "Jessie " + stateHolder.rand.nextInt() );
  64. for ( int j = 0; j < 10; j++ ) {
  65. scientist.getInterestedIn().add( fieldsOfSciences.get( stateHolder.rand.nextInt( NUMBER_OF_REFERENCABLE_ENTITIES ) ) );
  66. }
  67. entityManager.persist( scientist );
  68. if ( i % 1000 == 0 ) {
  69. stateHolder.transactionManager.commit();
  70. System.out.println( "Inserted " + i + " entities" );
  71. }
  72. }
  73. entityManager.close();
  74. }
  75. }
  76. @Benchmark
  77. @OperationsPerInvocation(OPERATIONS_PER_INVOCATION)
  78. public void getEntitiesWithAssociationById(TestDataInserter inserter, Blackhole blackhole) throws Exception {
  79. EntityManagerFactoryHolder stateHolder = inserter.stateHolder;
  80. EntityManager entityManager = stateHolder.entityManagerFactory.createEntityManager();
  81. stateHolder.transactionManager.begin();
  82. entityManager.joinTransaction();
  83. for ( int i = 0; i < OPERATIONS_PER_INVOCATION; i++ ) {
  84. long id = stateHolder.rand.nextInt( NUMBER_OF_TEST_ENTITIES - 1 ) + 1;
  85. ScientistWithSequence scientist = entityManager.find( ScientistWithSequence.class, id );
  86. if ( scientist == null ) {
  87. throw new IllegalArgumentException( "Couldn't find entry with id " + id );
  88. }
  89. blackhole.consume( scientist.getBio() );
  90. for ( FieldOfScience fieldOfScience : scientist.getInterestedIn() ) {
  91. blackhole.consume( fieldOfScience.getName() );
  92. }
  93. }
  94. stateHolder.transactionManager.commit();
  95. entityManager.close();
  96. }
  97. /**
  98. * For running/debugging a single invocation of the benchmarking loop.
  99. */
  100. public static void main(String[] args) throws Exception {
  101. EntityManagerFactoryHolder stateHolder = new EntityManagerFactoryHolder();
  102. stateHolder.setupEntityManagerFactory();
  103. TestDataInserter inserter = new TestDataInserter();
  104. inserter.insertTestData( stateHolder );
  105. new HibernateOgmAssociationFindBenchmark().getEntitiesWithAssociationById( inserter, null );
  106. }
  107. }