/** * Helper method for testing the entity manager before and after serialization. * * @param em Entity manager used for indexing and searching * @throws Exception */ private static void indexSearchAssert(FullTextEntityManager em) throws Exception { // index a Bretzel em.getTransaction().begin(); Bretzel bretzel = new Bretzel(23, 34); em.persist(bretzel); em.getTransaction().commit(); em.clear(); em.getTransaction().begin(); // execute a non matching query QueryParser parser = new QueryParser( TestConstants.getTargetLuceneVersion(), "title", TestConstants.stopAnalyzer); Query query = parser.parse("saltQty:noword"); assertEquals(0, em.createFullTextQuery(query).getResultList().size()); // execute a matching query query = new TermQuery(new Term("saltQty", "23")); assertEquals("getResultList", 1, em.createFullTextQuery(query).getResultList().size()); assertEquals( "getSingleResult and object retrieval", 23, ((Bretzel) em.createFullTextQuery(query).getSingleResult()).getSaltQty()); assertEquals(1, em.createFullTextQuery(query).getResultSize()); em.getTransaction().commit(); em.clear(); em.getTransaction().begin(); em.remove(em.find(Bretzel.class, bretzel.getId())); em.getTransaction().commit(); }
@Override public Properties getHibernateProperties() { Properties properties = super.getHibernateProperties(); properties.setProperty("hibernate.search.default.directory_provider", "filesystem"); properties.setProperty( "hibernate.search.default.indexBase", TestConstants.getIndexDirectory(TargetDirHelper.getTargetDir())); return properties; }
@Override public void configure(Map<String, Object> cfg) { File targetDir = TestConstants.getTargetDir(NoMBeansEnabledTest.class); File simpleJndiDir = new File(targetDir, "simpleJndi"); simpleJndiDir.mkdir(); cfg.put("hibernate.session_factory_name", "java:comp/SessionFactory"); cfg.put("hibernate.jndi.class", "org.osjava.sj.SimpleContextFactory"); cfg.put("hibernate.jndi.org.osjava.sj.root", simpleJndiDir.getAbsolutePath()); cfg.put("hibernate.jndi.org.osjava.sj.jndi.shared", "true"); // not setting the property is effectively the same as setting is explicitly to false // cfg.setProperty( Environment.JMX_ENABLED, "false" ); }
@Test public void testMessageSending() throws Exception { TShirt shirt = createObjectWithSQL(); List<LuceneWork> queue = createDocumentAndWorkQueue(shirt); registerMessageListener(); sendMessage(queue); // need to sleep to give JMS processing and indexing time Thread.sleep(1000); FullTextSession ftSess = Search.getFullTextSession(openSession()); ftSess.getTransaction().begin(); QueryParser parser = new QueryParser(TestConstants.getTargetLuceneVersion(), "id", TestConstants.stopAnalyzer); Query luceneQuery = parser.parse("logo:jboss"); org.hibernate.Query query = ftSess.createFullTextQuery(luceneQuery); List result = query.list(); assertEquals(1, result.size()); ftSess.delete(result.get(0)); ftSess.getTransaction().commit(); ftSess.close(); }
/** @author Tomas Hradec */ public abstract class TestScenario { private static final Boolean PERFORMANCE_ENABLED = TestConstants.arePerformanceTestsEnabled(); public final long initialOffset; public final long initialAutorCount; public final long initialBookCount; public final long warmupCyclesCount; public final long measuredCyclesCount; public final StopWatch initIndexStopWatch = new StopWatch(); public final StopWatch initDatabaseStopWatch = new StopWatch(); public final StopWatch warmupStopWatch = new StopWatch(); public TestScenario() { if (PERFORMANCE_ENABLED) { this.initialAutorCount = 10_000; this.initialBookCount = 1_000_000; this.warmupCyclesCount = 1_000; this.measuredCyclesCount = 5_000; this.initialOffset = 1_000_000; } else { this.initialAutorCount = 10; this.initialBookCount = 100; this.warmupCyclesCount = 1; this.measuredCyclesCount = 1; this.initialOffset = 100; } } public TestScenario( long initialAutorCount, long initialBookCount, long warmupCyclesCount, long measuredCyclesCount) { super(); this.initialAutorCount = initialAutorCount; this.initialBookCount = initialBookCount; this.warmupCyclesCount = warmupCyclesCount; this.measuredCyclesCount = measuredCyclesCount; this.initialOffset = 1_000_000; } public Properties getHibernateProperties() { Properties properties = new Properties(); properties.setProperty("hibernate.hbm2ddl.auto", "create"); properties.setProperty("hibernate.search.default.lucene_version", "LUCENE_CURRENT"); properties.setProperty("hibernate.search.index_uninverting_allowed", "false"); properties.setProperty("wildfly.jpa.hibernate.search.module", "none"); return properties; } public final void run(SessionFactory sf) throws IOException { initUncaughtExceptionHandler(); initDatabase(sf); performWarmUp(sf); performCleanUp(sf); initIndex(sf); performMeasuring(sf); } protected void initDatabase(SessionFactory sf) { log("starting initialize database"); initDatabaseStopWatch.start(); BatchSupport batchSupport = new BatchSupport(sf, initialOffset); batchSupport.execute( "insert into author(id, name) values(?, ?)", initialAutorCount, new BatchCallback() { @Override public void initStatement(PreparedStatement ps, long id) throws SQLException { ps.setLong(1, id); ps.setString(2, "autor" + id); } }); batchSupport.execute( "insert into book(id, title, summary, rating, totalSold, publicationDate) values(?, ?, ?, ?, ?, ?)", initialBookCount, new BatchCallback() { @Override public void initStatement(PreparedStatement ps, long id) throws SQLException { ps.setLong(1, id); ps.setString(2, "title" + id); ps.setString(3, reverse(SUMMARIES[(int) (id % SUMMARIES.length)])); ps.setLong(4, -1); ps.setLong(5, -1); ps.setDate(6, new Date(PUBLICATION_DATE_ZERO.getTime())); } }); batchSupport.execute( "insert into book_author(book_id, authors_id) values(?, ?)", initialBookCount, new BatchCallback() { @Override public void initStatement(PreparedStatement ps, long id) throws SQLException { ps.setLong(1, id); ps.setLong(2, initialOffset + (id % initialAutorCount)); } }); initDatabaseStopWatch.stop(); } protected void initIndex(SessionFactory sf) { log("starting initialize index"); initIndexStopWatch.start(); FullTextSession s = Search.getFullTextSession(sf.openSession()); try { s.createIndexer().startAndWait(); } catch (InterruptedException e) { throw new RuntimeException(e); } finally { s.close(); } initIndexStopWatch.stop(); } protected void performWarmUp(SessionFactory sf) { log("starting warm up phase"); warmupStopWatch.start(); TestContext ctx = new TestContext(this, sf); scheduleTasksAndStart(ctx, warmupCyclesCount); warmupStopWatch.stop(); } protected void performCleanUp(SessionFactory sf) { log("starting clean up phase"); FullTextSession s = Search.getFullTextSession(sf.openSession()); try { Transaction tx = s.beginTransaction(); s.createSQLQuery("delete from book_author where book_id < :id") .setParameter("id", initialOffset) .executeUpdate(); s.createSQLQuery("delete from book where id < :id") .setParameter("id", initialOffset) .executeUpdate(); s.createSQLQuery("delete from author where id < :id") .setParameter("id", initialOffset) .executeUpdate(); s.purgeAll(Book.class); s.flush(); s.flushToIndexes(); tx.commit(); } finally { s.close(); } } protected void performMeasuring(SessionFactory sf) throws IOException { log("starting measuring"); TestContext ctx = new TestContext(this, sf); scheduleTasksAndStart(ctx, measuredCyclesCount); try { TestReporter.printReport(ctx); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } protected void scheduleTasksAndStart(TestContext ctx, long cyclesCount) { InsertBookTask insertBookTask = new InsertBookTask(ctx); UpdateBookRatingTask updateBookRatingTask = new UpdateBookRatingTask(ctx); UpdateBookTotalSoldTask updateBookTotalSoldTask = new UpdateBookTotalSoldTask(ctx); QueryBooksByAuthorTask queryBooksByAuthorTask = new QueryBooksByAuthorTask(ctx); QueryBooksByAverageRatingTask queryBooksByAverageRatingTask = new QueryBooksByAverageRatingTask(ctx); QueryBooksByBestRatingTask queryBooksByBestRatingTask = new QueryBooksByBestRatingTask(ctx); QueryBooksByNewestPublishedTask queryBooksByNewestPublishedTask = new QueryBooksByNewestPublishedTask(ctx); QueryBooksBySummaryTask queryBooksBySummaryTask = new QueryBooksBySummaryTask(ctx); QueryBooksByTitleTask queryBooksByTitleTask = new QueryBooksByTitleTask(ctx); QueryBooksByTotalSoldTask queryBooksByTotalSoldTask = new QueryBooksByTotalSoldTask(ctx); for (int i = 0; i < cyclesCount; i++) { ctx.executor.execute(insertBookTask); ctx.executor.execute(updateBookRatingTask); ctx.executor.execute(updateBookTotalSoldTask); ctx.executor.execute(queryBooksByAuthorTask); ctx.executor.execute(queryBooksByAverageRatingTask); ctx.executor.execute(queryBooksByBestRatingTask); ctx.executor.execute(queryBooksByNewestPublishedTask); ctx.executor.execute(queryBooksBySummaryTask); ctx.executor.execute(queryBooksByTitleTask); ctx.executor.execute(queryBooksByTotalSoldTask); } ctx.startAndWait(); } }