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 org.apache.lucene.util.LuceneTestCase;
21 import org.apache.lucene.analysis.MockAnalyzer;
22 import org.apache.lucene.document.*;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.IndexWriter;
25 import org.apache.lucene.index.TermFreqVector;
26 import org.apache.lucene.store.Directory;
27 import org.apache.lucene.util.English;
29 import java.io.IOException;
31 public class TestMultiThreadTermVectors extends LuceneTestCase {
32 private Directory directory;
33 public int numDocs = 100;
34 public int numThreads = 3;
37 public void setUp() throws Exception {
39 directory = newDirectory();
40 IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
41 //writer.setUseCompoundFile(false);
42 //writer.infoStream = System.out;
43 for (int i = 0; i < numDocs; i++) {
44 Document doc = new Document();
45 Fieldable fld = newField("field", English.intToEnglish(i), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES);
47 writer.addDocument(doc);
54 public void tearDown() throws Exception {
59 public void test() throws Exception {
61 IndexReader reader = null;
64 reader = IndexReader.open(directory, true);
65 for(int i = 1; i <= numThreads; i++)
66 testTermPositionVectors(reader, i);
70 catch (IOException ioe) {
71 fail(ioe.getMessage());
76 /** close the opened reader */
78 } catch (IOException ioe) {
79 ioe.printStackTrace();
85 public void testTermPositionVectors(final IndexReader reader, int threadCount) throws Exception {
86 MultiThreadTermVectorsReader[] mtr = new MultiThreadTermVectorsReader[threadCount];
87 for (int i = 0; i < threadCount; i++) {
88 mtr[i] = new MultiThreadTermVectorsReader();
93 /** run until all threads finished */
94 int threadsAlive = mtr.length;
95 while (threadsAlive > 0) {
96 //System.out.println("Threads alive");
98 threadsAlive = mtr.length;
99 for (int i = 0; i < mtr.length; i++) {
100 if (mtr[i].isAlive() == true) {
109 for (int i = 0; i < mtr.length; i++) {
110 totalTime += mtr[i].timeElapsed;
114 //System.out.println("threadcount: " + mtr.length + " average term vector time: " + totalTime/mtr.length);
120 class MultiThreadTermVectorsReader implements Runnable {
122 private IndexReader reader = null;
123 private Thread t = null;
125 private final int runsToDo = 100;
126 long timeElapsed = 0;
129 public void init(IndexReader reader) {
130 this.reader = reader;
136 public boolean isAlive() {
137 if (t == null) return false;
144 // run the test 100 times
145 for (int i = 0; i < runsToDo; i++)
148 catch (Exception e) {
154 private void testTermVectors() throws Exception {
156 int numDocs = reader.numDocs();
158 for (int docId = 0; docId < numDocs; docId++) {
159 start = System.currentTimeMillis();
160 TermFreqVector [] vectors = reader.getTermFreqVectors(docId);
161 timeElapsed += System.currentTimeMillis()-start;
163 // verify vectors result
164 verifyVectors(vectors, docId);
166 start = System.currentTimeMillis();
167 TermFreqVector vector = reader.getTermFreqVector(docId, "field");
168 timeElapsed += System.currentTimeMillis()-start;
170 vectors = new TermFreqVector[1];
173 verifyVectors(vectors, docId);
178 private void verifyVectors(TermFreqVector[] vectors, int num) {
179 StringBuilder temp = new StringBuilder();
180 String[] terms = null;
181 for (int i = 0; i < vectors.length; i++) {
182 terms = vectors[i].getTerms();
183 for (int z = 0; z < terms.length; z++) {
184 temp.append(terms[z]);
188 if (!English.intToEnglish(num).trim().equals(temp.toString().trim()))
189 System.out.println("wrong term result");