Author: epbernard
Date: 2007-02-21 14:37:39 -0500 (Wed, 21 Feb 2007)
New Revision: 11230
Modified:
branches/Branch_3_2/HibernateExt/search/src/test/org/hibernate/search/test/worker/WorkerTestCase.java
Log:
Better performance and concurrency tests
Modified:
branches/Branch_3_2/HibernateExt/search/src/test/org/hibernate/search/test/worker/WorkerTestCase.java
===================================================================
---
branches/Branch_3_2/HibernateExt/search/src/test/org/hibernate/search/test/worker/WorkerTestCase.java 2007-02-21
19:28:07 UTC (rev 11229)
+++
branches/Branch_3_2/HibernateExt/search/src/test/org/hibernate/search/test/worker/WorkerTestCase.java 2007-02-21
19:37:39 UTC (rev 11230)
@@ -3,29 +3,83 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.io.File;
import org.hibernate.search.test.SearchTestCase;
+import org.hibernate.search.store.FSDirectoryProvider;
+import org.hibernate.search.Environment;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.impl.FullTextSessionImpl;
+import org.hibernate.search.event.FullTextIndexEventListener;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
+import org.hibernate.event.PostDeleteEventListener;
+import org.hibernate.event.PostUpdateEventListener;
+import org.hibernate.event.PostInsertEventListener;
+import org.apache.lucene.analysis.StopAnalyzer;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.queryParser.ParseException;
/**
* @author Emmanuel Bernard
*/
public class WorkerTestCase extends SearchTestCase {
- public void testConcurrency() throws Exception {
- ExecutorService es = Executors.newFixedThreadPool( 15 );
+ protected void setUp() throws Exception {
+ File sub = getBaseIndexDir();
+ sub.mkdir();
+ File[] files = sub.listFiles();
+ for (File file : files) {
+ if ( file.isDirectory() ) {
+ delete( file );
+ }
+ }
+ //super.setUp(); //we need a fresh session factory each time for index set up
+ buildSessionFactory( getMappings(), getAnnotatedPackages(), getXmlFiles() );
+ }
+
+ private File getBaseIndexDir() {
+ File current = new File( "." );
+ File sub = new File( current, "indextemp" );
+ return sub;
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ File sub = getBaseIndexDir();
+ delete( sub );
+ }
+
+ private void delete(File sub) {
+ if ( sub.isDirectory() ) {
+ for ( File file : sub.listFiles() ) {
+ delete( file );
+ }
+ sub.delete();
+ }
+ else {
+ sub.delete();
+ }
+ }
+
+ public void testConcurrency() throws Exception {
+ int nThreads = 15;
+ ExecutorService es = Executors.newFixedThreadPool( nThreads );
Work work = new Work( getSessions() );
ReverseWork reverseWork = new ReverseWork( getSessions() );
- for (int i = 0 ; i < 200 ; i++) {
+ long start = System.currentTimeMillis();
+ int iteration = 200;
+ for (int i = 0 ; i < iteration; i++) {
es.execute( work );
es.execute( reverseWork );
}
while(work.count < 199) {
Thread.sleep( 20 );
}
- }
+ System.out.println( 3*iteration + " iterations (4 tx per iteration) in "
+ nThreads + " threads: " + (System.currentTimeMillis() - start) );
+ }
protected class Work implements Runnable {
private SessionFactory sf;
@@ -56,8 +110,26 @@
tx.commit();
s.close();
- s = sf.openSession( );
+ s = sf.openSession( );
tx = s.beginTransaction();
+ FullTextSession fts = new FullTextSessionImpl(s);
+ QueryParser parser = new QueryParser( "id", new StopAnalyzer() );
+ Query query;
+ try
+ {
+ query = parser.parse( "name:emmanuel2" );
+ }
+ catch (ParseException e)
+ {
+ throw new RuntimeException(e);
+ }
+ boolean results = fts.createFullTextQuery( query ).list().size() > 0;
+ if (!results) throw new RuntimeException("No results!");
+ tx.commit();
+ s.close();
+
+ s = sf.openSession( );
+ tx = s.beginTransaction();
ee = (Employee) s.get( Employee.class, ee.getId() );
s.delete( ee );
er = (Employer) s.get( Employer.class, er.getId() );
@@ -107,7 +179,18 @@
}
}
- protected Class[] getMappings() {
+ protected void configure(org.hibernate.cfg.Configuration cfg) {
+ File sub = getBaseIndexDir();
+ cfg.setProperty( "hibernate.search.default.indexBase", sub.getAbsolutePath()
);
+ cfg.setProperty( "hibernate.search.Clock.directory_provider",
FSDirectoryProvider.class.getName() );
+ cfg.setProperty( Environment.ANALYZER_CLASS, StopAnalyzer.class.getName() );
+ FullTextIndexEventListener del = new FullTextIndexEventListener();
+ cfg.getEventListeners().setPostDeleteEventListeners( new PostDeleteEventListener[]{del}
);
+ cfg.getEventListeners().setPostUpdateEventListeners( new PostUpdateEventListener[]{del}
);
+ cfg.getEventListeners().setPostInsertEventListeners( new PostInsertEventListener[]{del}
);
+ }
+
+ protected Class[] getMappings() {
return new Class[] {
Employee.class,
Employer.class