~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/test/org/apache/lucene/search/TestMultiThreadTermVectors.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.search;
2
 
 
3
 
/**
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
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;
28
 
 
29
 
import java.io.IOException;
30
 
 
31
 
public class TestMultiThreadTermVectors extends LuceneTestCase {
32
 
  private Directory directory;
33
 
  public int numDocs = 100;
34
 
  public int numThreads = 3;
35
 
  
36
 
  @Override
37
 
  public void setUp() throws Exception {
38
 
    super.setUp();
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);
46
 
      doc.add(fld);
47
 
      writer.addDocument(doc);
48
 
    }
49
 
    writer.close();
50
 
    
51
 
  }
52
 
  
53
 
  @Override
54
 
  public void tearDown() throws Exception {
55
 
    directory.close();
56
 
    super.tearDown();
57
 
  }
58
 
  
59
 
  public void test() throws Exception {
60
 
    
61
 
    IndexReader reader = null;
62
 
    
63
 
    try {
64
 
      reader = IndexReader.open(directory, true);
65
 
      for(int i = 1; i <= numThreads; i++)
66
 
        testTermPositionVectors(reader, i);
67
 
      
68
 
      
69
 
    }
70
 
    catch (IOException ioe) {
71
 
      fail(ioe.getMessage());
72
 
    }
73
 
    finally {
74
 
      if (reader != null) {
75
 
        try {
76
 
          /** close the opened reader */
77
 
          reader.close();
78
 
        } catch (IOException ioe) {
79
 
          ioe.printStackTrace();
80
 
        }
81
 
      }
82
 
    }
83
 
  }
84
 
  
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();
89
 
      mtr[i].init(reader);
90
 
    }
91
 
    
92
 
    
93
 
    /** run until all threads finished */ 
94
 
    int threadsAlive = mtr.length;
95
 
    while (threadsAlive > 0) {
96
 
        //System.out.println("Threads alive");
97
 
        Thread.sleep(10);
98
 
        threadsAlive = mtr.length;
99
 
        for (int i = 0; i < mtr.length; i++) {
100
 
          if (mtr[i].isAlive() == true) {
101
 
            break;
102
 
          }
103
 
          
104
 
          threadsAlive--; 
105
 
        }
106
 
    }
107
 
    
108
 
    long totalTime = 0L;
109
 
    for (int i = 0; i < mtr.length; i++) {
110
 
      totalTime += mtr[i].timeElapsed;
111
 
      mtr[i] = null;
112
 
    }
113
 
    
114
 
    //System.out.println("threadcount: " + mtr.length + " average term vector time: " + totalTime/mtr.length);
115
 
    
116
 
  }
117
 
  
118
 
}
119
 
 
120
 
class MultiThreadTermVectorsReader implements Runnable {
121
 
  
122
 
  private IndexReader reader = null;
123
 
  private Thread t = null;
124
 
  
125
 
  private final int runsToDo = 100;
126
 
  long timeElapsed = 0;
127
 
  
128
 
  
129
 
  public void init(IndexReader reader) {
130
 
    this.reader = reader;
131
 
    timeElapsed = 0;
132
 
    t=new Thread(this);
133
 
    t.start();
134
 
  }
135
 
    
136
 
  public boolean isAlive() {
137
 
    if (t == null) return false;
138
 
    
139
 
    return t.isAlive();
140
 
  }
141
 
  
142
 
  public void run() {
143
 
      try {
144
 
        // run the test 100 times
145
 
        for (int i = 0; i < runsToDo; i++)
146
 
          testTermVectors();
147
 
      }
148
 
      catch (Exception e) {
149
 
        e.printStackTrace();
150
 
      }
151
 
      return;
152
 
  }
153
 
  
154
 
  private void testTermVectors() throws Exception {
155
 
    // check:
156
 
    int numDocs = reader.numDocs();
157
 
    long start = 0L;
158
 
    for (int docId = 0; docId < numDocs; docId++) {
159
 
      start = System.currentTimeMillis();
160
 
      TermFreqVector [] vectors = reader.getTermFreqVectors(docId);
161
 
      timeElapsed += System.currentTimeMillis()-start;
162
 
      
163
 
      // verify vectors result
164
 
      verifyVectors(vectors, docId);
165
 
      
166
 
      start = System.currentTimeMillis();
167
 
      TermFreqVector vector = reader.getTermFreqVector(docId, "field");
168
 
      timeElapsed += System.currentTimeMillis()-start;
169
 
      
170
 
      vectors = new TermFreqVector[1];
171
 
      vectors[0] = vector;
172
 
      
173
 
      verifyVectors(vectors, docId);
174
 
      
175
 
    }
176
 
  }
177
 
  
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]);
185
 
      }
186
 
    }
187
 
    
188
 
    if (!English.intToEnglish(num).trim().equals(temp.toString().trim()))
189
 
        System.out.println("wrong term result");
190
 
  }
191
 
}