[hibernate-commits] Hibernate SVN: r14724 - search/trunk/src/test/org/hibernate/search/test/perf.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Jun 2 18:55:36 EDT 2008


Author: sannegrinovero
Date: 2008-06-02 18:55:36 -0400 (Mon, 02 Jun 2008)
New Revision: 14724

Modified:
   search/trunk/src/test/org/hibernate/search/test/perf/IndexTestDontRun.java
   search/trunk/src/test/org/hibernate/search/test/perf/SearcherThread.java
Log:
Improvements in performance test comparing to raw Lucene

Modified: search/trunk/src/test/org/hibernate/search/test/perf/IndexTestDontRun.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/perf/IndexTestDontRun.java	2008-06-02 18:02:15 UTC (rev 14723)
+++ search/trunk/src/test/org/hibernate/search/test/perf/IndexTestDontRun.java	2008-06-02 22:55:36 UTC (rev 14724)
@@ -3,6 +3,10 @@
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 
 import junit.textui.TestRunner;
 import org.apache.lucene.search.IndexSearcher;
@@ -18,15 +22,16 @@
  * @author Emmanuel Bernard
  */
 public class IndexTestDontRun extends SearchTestCase {
-	private static boolean isLucene;
+	
+	private static final int TOTAL_SEARCHES = 800;
+	private static final int SEARCH_THREADS = 100;
 
 	public static void main(String[] args) {
 		//isLucene = Boolean.parseBoolean( args[0] );
 		TestRunner.run( IndexTestDontRun.class );
-
 	}
 
-	public void NonestInit() throws Exception {
+	public void notestInit() throws Exception {
 		long time = System.currentTimeMillis();
 		Session s = openSession();
 		Transaction tx = s.beginTransaction();
@@ -37,26 +42,39 @@
 		s.close();
 		System.out.println( " init time = " + ( System.currentTimeMillis() - time ) );
 	}
+	
+	public void testPerformance() throws Exception {
+		measure(true);//JVM warmup
+		measure(false);//JVM warmup
+		long measureLucene = measure( true );
+		long measureSearch = measure( false );
+		System.out.println( "Totaltime Lucene = " + measureLucene );
+		System.out.println( "Totaltime Search = " + measureSearch );
+	}
 
-	public void testPerf() throws Exception {
-		boolean useLucene = false;
-
-		List<SearcherThread> threads = new ArrayList<SearcherThread>( 100 );
-		IndexSearcher indexsearcher = getNewSearcher();
-		SearcherThread searcherThrea = new SearcherThread( 0, "name:maria OR description:long" + 0, getSessions(), indexsearcher, useLucene );
-		searcherThrea.run();
-		for (int i = 1; i <= 100; i++) {
+	public long measure(boolean plainLucene) throws Exception {
+		ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool( SEARCH_THREADS );
+		threadPool.prestartAllCoreThreads();
+		CountDownLatch startSignal = new CountDownLatch(1);
+		List<SearcherThread> threadsList = new ArrayList<SearcherThread>( TOTAL_SEARCHES );
+		IndexSearcher indexSearcher = getNewSearcher();
+		for (int i = 0; i < TOTAL_SEARCHES; i++) {
 			// Create a thread and invoke it
-			//if ( i % 100 == 0) indexsearcher = getNewSearcher();
-			SearcherThread searcherThread = new SearcherThread( i, "name:maria OR description:long" + i, getSessions(), indexsearcher, useLucene );
-			searcherThread.setDaemon( false );
-			threads.add( searcherThread );
-			searcherThread.start();
+			//if ( i % 100 == 0) indexSearcher = getNewSearcher();
+			SearcherThread searcherThread = new SearcherThread( i, "name:maria OR description:long" + i, getSessions(), indexSearcher, plainLucene, startSignal );
+			threadsList.add( searcherThread );
+			threadPool.execute( searcherThread );
 		}
-		Thread.sleep( 5000 );
+		threadPool.shutdown();//required to enable awaitTermination functionality
+		startSignal.countDown();//start all created threads
+		boolean terminationOk = threadPool.awaitTermination( 60, TimeUnit.SECONDS );
+		if ( terminationOk==false ) {
+			System.out.println( "No enough time to complete the tests!" );
+			return 0;
+		}
 		long totalTime = 0;
-		for (SearcherThread t : threads) totalTime += t.time;
-		System.out.println( "Totaltime=" + totalTime );
+		for (SearcherThread t : threadsList) totalTime += t.getTime();
+		return totalTime;
 	}
 
 	private IndexSearcher getNewSearcher() throws IOException {
@@ -75,7 +93,6 @@
 	protected void configure(Configuration cfg) {
 		super.configure( cfg );
 		cfg.setProperty( "hibernate.search.default.directory_provider", FSDirectoryProvider.class.getName() );
-		//cfg.setProperty( "hibernate.search.reader.strategy", DumbSharedReaderProvider.class.getName() );
-
+//		cfg.setProperty( "hibernate.search.reader.strategy", DumbSharedReaderProvider.class.getName() );
 	}
 }

Modified: search/trunk/src/test/org/hibernate/search/test/perf/SearcherThread.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/perf/SearcherThread.java	2008-06-02 18:02:15 UTC (rev 14723)
+++ search/trunk/src/test/org/hibernate/search/test/perf/SearcherThread.java	2008-06-02 22:55:36 UTC (rev 14724)
@@ -3,6 +3,7 @@
 import java.io.IOException;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.concurrent.CountDownLatch;
 
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.queryParser.ParseException;
@@ -11,9 +12,7 @@
 import org.apache.lucene.search.Hits;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.store.Directory;
 import org.hibernate.SessionFactory;
-import org.hibernate.classic.Session;
 import org.hibernate.search.Search;
 import org.hibernate.search.FullTextSession;
 import org.hibernate.search.FullTextQuery;
@@ -23,27 +22,39 @@
 /**
  * @author Emmanuel Bernard
  */
-public class SearcherThread extends Thread {
-	private static Logger log = LoggerFactory.getLogger( SearcherThread.class );
-	private int threadId;
-	private String queryString;
-	private SessionFactory sf;
-	private IndexSearcher indexsearcher;
-	private boolean isLucene;
-	public long time;
+public class SearcherThread implements Runnable {
+	private static final Logger log = LoggerFactory.getLogger( SearcherThread.class );
+	private final int threadId;
+	private final String queryString;
+	private final SessionFactory sf;
+	private final IndexSearcher indexsearcher;
+	private final boolean isLucene;
+	private final CountDownLatch startSignal;
+	private long time;
 
 	/**
-	 * Initialize with thread-id, querystring, indexsearcher
+	 * Initialize with thread-id, queryString, indexSearcher
+	 * @param startSignal 
 	 */
-	public SearcherThread(int threadId, String queryString, SessionFactory sf, IndexSearcher indexSearcher, boolean isLucene) {
+	public SearcherThread(int threadId, String queryString, SessionFactory sf, IndexSearcher indexSearcher, boolean isLucene, CountDownLatch startSignal) {
 		this.isLucene = isLucene;
 		this.threadId = threadId;
 		this.queryString = queryString;
 		this.sf = sf;
 		this.indexsearcher = indexSearcher;
+		this.startSignal = startSignal;
 	}
 
+	/**
+	 * @see java.lang.Runnable#run()
+	 */
 	public void run() {
+		try {
+			startSignal.await();
+		} catch (InterruptedException e) {
+			log.error( "tests canceled", e );
+			return;
+		}
 		if ( isLucene ) {
 			runLucene();
 		}
@@ -52,34 +63,22 @@
 		}
 	}
 
-	/**
-	 * @see java.lang.Runnable#run()
-	 */
 	public void runLucene() {
-
 		try {
-			QueryParser qp = new QueryParser( "t",
-					new StandardAnalyzer() );
-			qp.setLowercaseExpandedTerms( true );
-			// Parse the query
-			Query q = qp.parse( queryString );
-			if ( q instanceof BooleanQuery ) {
-				BooleanQuery
-						.setMaxClauseCount( Integer.MAX_VALUE );
-			}
+			Query q = getQuery();
 			long start = System.currentTimeMillis();
 			// Search
 			Hits hits = indexsearcher.search( q );
 			List<String> names = new ArrayList<String>(100);
-			for (int i = 1 ; i <= 100 ; i++) {
+			for (int i = 0 ; i < 100 ; i++) {
 				names.add( hits.doc( i ).get( "name" ) );
 			}
+			int resultSize = hits.length();
 			long totalTime = System.currentTimeMillis() - start;
-			log.error( "Lucene [ Thread-id : " + threadId + " ] Total time taken for search is : " + totalTime + "ms with total no. of matching records : " + hits.length() );
-			time = totalTime;
+//			log.error( "Lucene [ Thread-id : " + threadId + " ] Total time taken for search is : " + totalTime + "ms with total no. of matching records : " + hits.length() );
+			setTime( totalTime );
 		}
 		catch (ParseException e) {
-			// TODO Auto-generated catch block
 			System.out.println( "[ Thread-id : " + threadId + " ] Parse Exception for queryString : " + queryString );
 			e.printStackTrace();
 		}
@@ -91,32 +90,33 @@
 		}
 	}
 
+	private Query getQuery() throws ParseException {
+		QueryParser qp = new QueryParser( "t", new StandardAnalyzer() );
+		qp.setLowercaseExpandedTerms( true );
+		// Parse the query
+		Query q = qp.parse( queryString );
+		if ( q instanceof BooleanQuery ) {
+			BooleanQuery.setMaxClauseCount( Integer.MAX_VALUE );
+		}
+		return q;
+	}
+
 	public void runHSearch() {
-
 		try {
-			QueryParser qp = new QueryParser( "t",
-					new StandardAnalyzer() );
-			qp.setLowercaseExpandedTerms( true );
-
-			// Parse the query
-			Query q = qp.parse( queryString );
-			
-
+			Query q = getQuery();
 			// Search
 			FullTextSession ftSession = Search.createFullTextSession( sf.openSession(  ) );
-
 			final FullTextQuery textQuery = ftSession.createFullTextQuery( q, Boat.class )
 					.setMaxResults( 100 ).setProjection( "name" );
 			long start = System.currentTimeMillis();
 			List results = textQuery.list();
+			int resultSize = textQuery.getResultSize();
 			long totalTime = System.currentTimeMillis() - start;
 			ftSession.close();
-
-			log.error( "HSearch [ Thread-id : " + threadId + " ] Total time taken for search is : " + totalTime + "ms with total no. of matching records : " + textQuery.getResultSize() );
-			time = totalTime;
+//			log.error( "HSearch [ Thread-id : " + threadId + " ] Total time taken for search is : " + totalTime + "ms with total no. of matching records : " + resultSize );
+			setTime( totalTime );
 		}
 		catch (ParseException e) {
-			// TODO Auto-generated catch block
 			log.error( "[ Thread-id : " + threadId + " ] Parse Exception for queryString : " + queryString );
 			e.printStackTrace();
 		}
@@ -125,4 +125,13 @@
 			e.printStackTrace(  );
 		}
 	}
+
+	public synchronized long getTime() {
+		return time;
+	}
+
+	public synchronized void setTime(long time) {
+		this.time = time;
+	}
+	
 }




More information about the hibernate-commits mailing list