Author: epbernard
Date: 2008-05-30 18:48:20 -0400 (Fri, 30 May 2008)
New Revision: 14720
Added:
search/trunk/src/test/org/hibernate/search/test/perf/
search/trunk/src/test/org/hibernate/search/test/perf/Boat.java
search/trunk/src/test/org/hibernate/search/test/perf/IndexTestDontRun.java
search/trunk/src/test/org/hibernate/search/test/perf/SearcherThread.java
Log:
Add dumb perf test
Added: search/trunk/src/test/org/hibernate/search/test/perf/Boat.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/perf/Boat.java
(rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/perf/Boat.java 2008-05-30 22:48:20 UTC
(rev 14720)
@@ -0,0 +1,33 @@
+package org.hibernate.search.test.perf;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Store;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+@Indexed
+public class Boat {
+ @Id
+ @GeneratedValue
+ @DocumentId
+ public Integer id;
+ @Field(store= Store.YES)
+ public String name;
+ @Field
+ public String description;
+
+ public Boat() {}
+
+ public Boat(String name, String description) {
+ this.name = name;
+ this.description = description;
+ }
+}
Added: search/trunk/src/test/org/hibernate/search/test/perf/IndexTestDontRun.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/perf/IndexTestDontRun.java
(rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/perf/IndexTestDontRun.java 2008-05-30
22:48:20 UTC (rev 14720)
@@ -0,0 +1,81 @@
+package org.hibernate.search.test.perf;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+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 boolean isLucene;
+
+ public static void main(String[] args) {
+ //isLucene = Boolean.parseBoolean( args[0] );
+ TestRunner.run( IndexTestDontRun.class );
+
+ }
+
+ public void NonestInit() 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 testPerf() throws Exception {
+ boolean useLucene = true;
+
+ 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++) {
+ // 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();
+ }
+ Thread.sleep( 5000 );
+ long totalTime = 0;
+ for (SearcherThread t : threads) totalTime += t.time;
+ System.out.println( "Totaltime=" + totalTime );
+ }
+
+ private IndexSearcher getNewSearcher() throws IOException {
+ final org.hibernate.classic.Session session = getSessions().openSession();
+ Directory d = Search.createFullTextSession( 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() );
+
+ }
+}
Added: search/trunk/src/test/org/hibernate/search/test/perf/SearcherThread.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/perf/SearcherThread.java
(rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/perf/SearcherThread.java 2008-05-30
22:48:20 UTC (rev 14720)
@@ -0,0 +1,128 @@
+package org.hibernate.search.test.perf;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.BooleanQuery;
+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;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @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;
+
+ /**
+ * Initialize with thread-id, querystring, indexsearcher
+ */
+ public SearcherThread(int threadId, String queryString, SessionFactory sf, IndexSearcher
indexSearcher, boolean isLucene) {
+ this.isLucene = isLucene;
+ this.threadId = threadId;
+ this.queryString = queryString;
+ this.sf = sf;
+ this.indexsearcher = indexSearcher;
+ }
+
+ public void run() {
+ if ( isLucene ) {
+ runLucene();
+ }
+ else {
+ runHSearch();
+ }
+ }
+
+ /**
+ * @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 );
+ }
+ long start = System.currentTimeMillis();
+ // Search
+ Hits hits = indexsearcher.search( q );
+ List<String> names = new ArrayList<String>(100);
+ for (int i = 1 ; i <= 100 ; i++) {
+ names.add( hits.doc( i ).get( "name" ) );
+ }
+ 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;
+ }
+ catch (ParseException e) {
+ // TODO Auto-generated catch block
+ System.out.println( "[ Thread-id : " + threadId + " ] Parse Exception
for queryString : " + queryString );
+ e.printStackTrace();
+ }
+ catch (IOException e) {
+ System.out.println( "[ Thread-id : " + threadId + " ] IO Exception for
queryString : " + queryString );
+ }
+ catch (Exception e) {
+ e.printStackTrace( );
+ }
+ }
+
+ public void runHSearch() {
+
+ try {
+ QueryParser qp = new QueryParser( "t",
+ new StandardAnalyzer() );
+ qp.setLowercaseExpandedTerms( true );
+
+ // Parse the query
+ Query q = qp.parse( queryString );
+
+
+ // 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();
+ 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;
+ }
+ catch (ParseException e) {
+ // TODO Auto-generated catch block
+ log.error( "[ Thread-id : " + threadId + " ] Parse Exception for
queryString : " + queryString );
+ e.printStackTrace();
+ }
+ catch (Throwable e) {
+ log.error( "[ Thread-id : " + threadId + " ] Exception for queryString
: " + queryString );
+ e.printStackTrace( );
+ }
+ }
+}