1 package org.apache.lucene.index;
4 * Copyright 2004 The Apache Software Foundation
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.lucene.util.*;
20 import org.apache.lucene.store.*;
21 import org.apache.lucene.analysis.MockAnalyzer;
22 import org.apache.lucene.document.*;
23 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
24 import org.apache.lucene.search.*;
26 public class TestStressIndexing extends LuceneTestCase {
27 private static abstract class TimedThread extends Thread {
28 volatile boolean failed;
30 private static int RUN_TIME_MSEC = atLeast(1000);
31 private TimedThread[] allThreads;
33 abstract public void doWork() throws Throwable;
35 TimedThread(TimedThread[] threads) {
36 this.allThreads = threads;
41 final long stopTime = System.currentTimeMillis() + RUN_TIME_MSEC;
47 if (anyErrors()) break;
50 } while(System.currentTimeMillis() < stopTime);
51 } catch (Throwable e) {
52 System.out.println(Thread.currentThread() + ": exc");
53 e.printStackTrace(System.out);
58 private boolean anyErrors() {
59 for(int i=0;i<allThreads.length;i++)
60 if (allThreads[i] != null && allThreads[i].failed)
66 private class IndexerThread extends TimedThread {
70 public IndexerThread(IndexWriter writer, TimedThread[] threads) {
76 public void doWork() throws Exception {
78 for(int j=0; j<10; j++) {
79 Document d = new Document();
80 int n = random.nextInt();
81 d.add(newField("id", Integer.toString(nextID++), Field.Store.YES, Field.Index.NOT_ANALYZED));
82 d.add(newField("contents", English.intToEnglish(n), Field.Store.NO, Field.Index.ANALYZED));
83 writer.addDocument(d);
87 int deleteID = nextID-1;
88 for(int j=0; j<5; j++) {
89 writer.deleteDocuments(new Term("id", ""+deleteID));
95 private static class SearcherThread extends TimedThread {
96 private Directory directory;
98 public SearcherThread(Directory directory, TimedThread[] threads) {
100 this.directory = directory;
104 public void doWork() throws Throwable {
105 for (int i=0; i<100; i++)
106 (new IndexSearcher(directory, true)).close();
112 Run one indexer and 2 searchers against single index as
115 public void runStressTest(Directory directory, MergeScheduler mergeScheduler) throws Exception {
116 IndexWriter modifier = new IndexWriter(directory, newIndexWriterConfig(
117 TEST_VERSION_CURRENT, new MockAnalyzer(random))
118 .setOpenMode(OpenMode.CREATE).setMaxBufferedDocs(10).setMergeScheduler(
122 TimedThread[] threads = new TimedThread[4];
126 // One modifier that writes 10 docs then removes 5, over
128 IndexerThread indexerThread = new IndexerThread(modifier, threads);
129 threads[numThread++] = indexerThread;
130 indexerThread.start();
132 IndexerThread indexerThread2 = new IndexerThread(modifier, threads);
133 threads[numThread++] = indexerThread2;
134 indexerThread2.start();
136 // Two searchers that constantly just re-instantiate the
138 SearcherThread searcherThread1 = new SearcherThread(directory, threads);
139 threads[numThread++] = searcherThread1;
140 searcherThread1.start();
142 SearcherThread searcherThread2 = new SearcherThread(directory, threads);
143 threads[numThread++] = searcherThread2;
144 searcherThread2.start();
146 for(int i=0;i<numThread;i++)
151 for(int i=0;i<numThread;i++)
152 assertTrue(! threads[i].failed);
154 //System.out.println(" Writer: " + indexerThread.count + " iterations");
155 //System.out.println("Searcher 1: " + searcherThread1.count + " searchers created");
156 //System.out.println("Searcher 2: " + searcherThread2.count + " searchers created");
160 Run above stress test against RAMDirectory and then
163 public void testStressIndexAndSearching() throws Exception {
164 Directory directory = newDirectory();
165 runStressTest(directory, new ConcurrentMergeScheduler());