1 package org.apache.lucene.search;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.util.concurrent.atomic.AtomicBoolean;
21 import java.util.concurrent.atomic.AtomicLong;
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;
32 public class TestSearchWithThreads extends LuceneTestCase {
34 final int NUM_DOCS = atLeast(10000);
35 final int NUM_SEARCH_THREADS = 5;
36 final int RUN_TIME_MSEC = atLeast(1000);
38 public void test() throws Exception {
39 final Directory dir = newDirectory();
40 final RandomIndexWriter w = new RandomIndexWriter(random, dir);
42 final long startTime = System.currentTimeMillis();
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);
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");
56 body.setValue(sb.toString());
58 sb.delete(0, sb.length());
60 final IndexReader r = w.getReader();
63 final long endTime = System.currentTimeMillis();
64 if (VERBOSE) System.out.println("BUILD took " + (endTime-startTime));
66 final IndexSearcher s = newSearcher(r);
68 final AtomicBoolean failed = new AtomicBoolean();
69 final AtomicLong netSearch = new AtomicLong();
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();
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();
88 assertTrue(totSearch > 0 && totHits > 0);
89 netSearch.addAndGet(totSearch);
90 } catch (Exception exc) {
92 throw new RuntimeException(exc);
96 threads[threadID].setDaemon(true);
99 for (Thread t : threads) {
103 for (Thread t : threads) {
107 if (VERBOSE) System.out.println(NUM_SEARCH_THREADS + " threads did " + netSearch.get() + " searches");