// $Id: IndexTestDontRun.java 14954 2008-07-17 20:43:10Z sannegrinovero $
package org.hibernate.search.test.perf;
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;
import org.apache.lucene.store.Directory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.search.Search;
import org.hibernate.search.store.FSDirectoryProvider;
import org.hibernate.search.test.SearchTestCase;
/**
* @author Emmanuel Bernard
*/
public class IndexTestDontRun extends SearchTestCase {
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 notestInit() throws Exception {
long time = System.currentTimeMillis();
Session s = openSession();
Transaction tx = s.beginTransaction();
for (int i = 0; i < 50000; i++) {
s.save( new Boat( "Maria el Seb", "a long" + i + " description of the land" + i ) );
}
tx.commit();
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 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, plainLucene, startSignal );
threadsList.add( searcherThread );
threadPool.execute( searcherThread );
}
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 : threadsList) totalTime += t.getTime();
return totalTime;
}
private IndexSearcher getNewSearcher() throws IOException {
final org.hibernate.classic.Session session = getSessions().openSession();
Directory d = Search.getFullTextSession( session ).getSearchFactory().getDirectoryProviders( Boat.class )[0].getDirectory();
IndexSearcher indexsearcher = new IndexSearcher( d );
return indexsearcher;
}
protected Class[] getMappings() {
return new Class[] {
Boat.class
};
}
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() );
}
}