001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.util.LuceneTestCase;
021: import org.apache.lucene.analysis.SimpleAnalyzer;
022: import org.apache.lucene.document.*;
023: import org.apache.lucene.index.IndexReader;
024: import org.apache.lucene.index.IndexWriter;
025: import org.apache.lucene.index.TermFreqVector;
026: import org.apache.lucene.store.RAMDirectory;
027: import org.apache.lucene.util.English;
028:
029: import java.io.IOException;
030:
031: /**
032: *
033: * @version $rcs = ' $Id: TestMultiThreadTermVectors.java 583534 2007-10-10 16:46:35Z mikemccand $ ' ;
034: */
035: public class TestMultiThreadTermVectors extends LuceneTestCase {
036: private RAMDirectory directory = new RAMDirectory();
037: public int numDocs = 100;
038: public int numThreads = 3;
039:
040: public TestMultiThreadTermVectors(String s) {
041: super (s);
042: }
043:
044: public void setUp() throws Exception {
045: super .setUp();
046: IndexWriter writer = new IndexWriter(directory,
047: new SimpleAnalyzer(), true);
048: //writer.setUseCompoundFile(false);
049: //writer.infoStream = System.out;
050: for (int i = 0; i < numDocs; i++) {
051: Document doc = new Document();
052: Fieldable fld = new Field("field", English.intToEnglish(i),
053: Field.Store.YES, Field.Index.UN_TOKENIZED,
054: Field.TermVector.YES);
055: doc.add(fld);
056: writer.addDocument(doc);
057: }
058: writer.close();
059:
060: }
061:
062: public void test() {
063:
064: IndexReader reader = null;
065:
066: try {
067: reader = IndexReader.open(directory);
068: for (int i = 1; i <= numThreads; i++)
069: testTermPositionVectors(reader, i);
070:
071: } catch (IOException ioe) {
072: fail(ioe.getMessage());
073: } finally {
074: if (reader != null) {
075: try {
076: /** close the opened reader */
077: reader.close();
078: } catch (IOException ioe) {
079: ioe.printStackTrace();
080: }
081: }
082: }
083: }
084:
085: public void testTermPositionVectors(final IndexReader reader,
086: int threadCount) {
087: MultiThreadTermVectorsReader[] mtr = new MultiThreadTermVectorsReader[threadCount];
088: for (int i = 0; i < threadCount; i++) {
089: mtr[i] = new MultiThreadTermVectorsReader();
090: mtr[i].init(reader);
091: }
092:
093: /** run until all threads finished */
094: int threadsAlive = mtr.length;
095: while (threadsAlive > 0) {
096: try {
097: //System.out.println("Threads alive");
098: Thread.sleep(10);
099: threadsAlive = mtr.length;
100: for (int i = 0; i < mtr.length; i++) {
101: if (mtr[i].isAlive() == true) {
102: break;
103: }
104:
105: threadsAlive--;
106:
107: }
108:
109: } catch (InterruptedException ie) {
110: }
111: }
112:
113: long totalTime = 0L;
114: for (int i = 0; i < mtr.length; i++) {
115: totalTime += mtr[i].timeElapsed;
116: mtr[i] = null;
117: }
118:
119: //System.out.println("threadcount: " + mtr.length + " average term vector time: " + totalTime/mtr.length);
120:
121: }
122:
123: }
124:
125: class MultiThreadTermVectorsReader implements Runnable {
126:
127: private IndexReader reader = null;
128: private Thread t = null;
129:
130: private final int runsToDo = 100;
131: long timeElapsed = 0;
132:
133: public void init(IndexReader reader) {
134: this .reader = reader;
135: timeElapsed = 0;
136: t = new Thread(this );
137: t.start();
138: }
139:
140: public boolean isAlive() {
141: if (t == null)
142: return false;
143:
144: return t.isAlive();
145: }
146:
147: public void run() {
148: try {
149: // run the test 100 times
150: for (int i = 0; i < runsToDo; i++)
151: testTermVectors();
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155: return;
156: }
157:
158: private void testTermVectors() throws Exception {
159: // check:
160: int numDocs = reader.numDocs();
161: long start = 0L;
162: for (int docId = 0; docId < numDocs; docId++) {
163: start = System.currentTimeMillis();
164: TermFreqVector[] vectors = reader.getTermFreqVectors(docId);
165: timeElapsed += System.currentTimeMillis() - start;
166:
167: // verify vectors result
168: verifyVectors(vectors, docId);
169:
170: start = System.currentTimeMillis();
171: TermFreqVector vector = reader.getTermFreqVector(docId,
172: "field");
173: timeElapsed += System.currentTimeMillis() - start;
174:
175: vectors = new TermFreqVector[1];
176: vectors[0] = vector;
177:
178: verifyVectors(vectors, docId);
179:
180: }
181: }
182:
183: private void verifyVectors(TermFreqVector[] vectors, int num) {
184: StringBuffer temp = new StringBuffer();
185: String[] terms = null;
186: for (int i = 0; i < vectors.length; i++) {
187: terms = vectors[i].getTerms();
188: for (int z = 0; z < terms.length; z++) {
189: temp.append(terms[z]);
190: }
191: }
192:
193: if (!English.intToEnglish(num).trim().equals(
194: temp.toString().trim()))
195: System.out.println("wrong term result");
196: }
197: }
|