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
IndexReader ir = IndexReader.open(directory, true);
107
IndexSearcher is = new IndexSearcher(ir);
116
Run one indexer and 2 searchers against single index as
119
public void runStressTest(Directory directory, MergeScheduler mergeScheduler) throws Exception {
120
IndexWriter modifier = new IndexWriter(directory, newIndexWriterConfig(
121
TEST_VERSION_CURRENT, new MockAnalyzer(random))
122
.setOpenMode(OpenMode.CREATE).setMaxBufferedDocs(10).setMergeScheduler(
126
TimedThread[] threads = new TimedThread[4];
130
// One modifier that writes 10 docs then removes 5, over
132
IndexerThread indexerThread = new IndexerThread(modifier, threads);
133
threads[numThread++] = indexerThread;
134
indexerThread.start();
136
IndexerThread indexerThread2 = new IndexerThread(modifier, threads);
137
threads[numThread++] = indexerThread2;
138
indexerThread2.start();
140
// Two searchers that constantly just re-instantiate the
142
SearcherThread searcherThread1 = new SearcherThread(directory, threads);
143
threads[numThread++] = searcherThread1;
144
searcherThread1.start();
146
SearcherThread searcherThread2 = new SearcherThread(directory, threads);
147
threads[numThread++] = searcherThread2;
148
searcherThread2.start();
150
for(int i=0;i<numThread;i++)
155
for(int i=0;i<numThread;i++)
156
assertTrue(! threads[i].failed);
158
//System.out.println(" Writer: " + indexerThread.count + " iterations");
159
//System.out.println("Searcher 1: " + searcherThread1.count + " searchers created");
160
//System.out.println("Searcher 2: " + searcherThread2.count + " searchers created");
164
Run above stress test against RAMDirectory and then
167
public void testStressIndexAndSearching() throws Exception {
168
Directory directory = newDirectory();
169
runStressTest(directory, new ConcurrentMergeScheduler());