add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / search / TestSearchWithThreads.java
1 package org.apache.lucene.search;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.util.concurrent.atomic.AtomicBoolean;
21 import java.util.concurrent.atomic.AtomicLong;
22
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.RandomIndexWriter;
27 import org.apache.lucene.index.Term;
28 import org.apache.lucene.search.IndexSearcher;
29 import org.apache.lucene.store.Directory;
30 import org.apache.lucene.util.LuceneTestCase;
31
32 public class TestSearchWithThreads extends LuceneTestCase {
33   
34   final int NUM_DOCS = atLeast(10000);
35   final int NUM_SEARCH_THREADS = 5;
36   final int RUN_TIME_MSEC = atLeast(1000);
37
38   public void test() throws Exception {
39     final Directory dir = newDirectory();
40     final RandomIndexWriter w = new RandomIndexWriter(random, dir);
41
42     final long startTime = System.currentTimeMillis();
43
44     // TODO: replace w/ the @nightly test data; make this
45     // into an optional @nightly stress test
46     final Document doc = new Document();
47     final Field body = newField("body", "", Field.Index.ANALYZED);
48     doc.add(body);
49     final StringBuilder sb = new StringBuilder();
50     for(int docCount=0;docCount<NUM_DOCS;docCount++) {
51       final int numTerms = random.nextInt(10);
52       for(int termCount=0;termCount<numTerms;termCount++) {
53         sb.append(random.nextBoolean() ? "aaa" : "bbb");
54         sb.append(' ');
55       }
56       body.setValue(sb.toString());
57       w.addDocument(doc);
58       sb.delete(0, sb.length());
59     }
60     final IndexReader r = w.getReader();
61     w.close();
62
63     final long endTime = System.currentTimeMillis();
64     if (VERBOSE) System.out.println("BUILD took " + (endTime-startTime));
65
66     final IndexSearcher s = newSearcher(r);
67
68     final AtomicBoolean failed = new AtomicBoolean();
69     final AtomicLong netSearch = new AtomicLong();
70
71     Thread[] threads = new Thread[NUM_SEARCH_THREADS];
72     for (int threadID = 0; threadID < NUM_SEARCH_THREADS; threadID++) {
73       threads[threadID] = new Thread() {
74         TotalHitCountCollector col = new TotalHitCountCollector();
75           @Override
76           public void run() {
77             try {
78               long totHits = 0;
79               long totSearch = 0;
80               long stopAt = System.currentTimeMillis() + RUN_TIME_MSEC;
81               while(System.currentTimeMillis() < stopAt && !failed.get()) {
82                 s.search(new TermQuery(new Term("body", "aaa")), col);
83                 totHits += col.getTotalHits();
84                 s.search(new TermQuery(new Term("body", "bbb")), col);
85                 totHits += col.getTotalHits();
86                 totSearch++;
87               }
88               assertTrue(totSearch > 0 && totHits > 0);
89               netSearch.addAndGet(totSearch);
90             } catch (Exception exc) {
91               failed.set(true);
92               throw new RuntimeException(exc);
93             }
94           }
95         };
96       threads[threadID].setDaemon(true);
97     }
98
99     for (Thread t : threads) {
100       t.start();
101     }
102     
103     for (Thread t : threads) {
104       t.join();
105     }
106
107     if (VERBOSE) System.out.println(NUM_SEARCH_THREADS + " threads did " + netSearch.get() + " searches");
108
109     s.close();
110     r.close();
111     dir.close();
112   }
113 }