~ubuntu-branches/ubuntu/trusty/pylucene/trusty

« back to all changes in this revision

Viewing changes to lucene-java-2.3.1/contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java

  • Committer: Package Import Robot
  • Author(s): Dmitry Nezhevenko
  • Date: 2012-04-23 16:43:55 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120423164355-grqtepnwtecdjfk2
Tags: 3.5.0-1
* New maintainer (closes: 670179)
* New upstream release
* Switch to dpkg-source 3.0 (quilt) format
* Switch to machine-readable debian/copyright
* Bump debian/compat to 8, drop debian/pycompat
* Switch from cdbs to dh
* Add watch file
* Build for all supported versions of python2 (closes: 581198, 632240)
* Rename binary package to python-lucene (closes: 581197)
* Add -dbg package

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.lucene.benchmark.byTask;
19
 
 
20
 
import java.io.StringReader;
21
 
import java.io.File;
22
 
import java.io.FileReader;
23
 
import java.io.BufferedReader;
24
 
import java.util.List;
25
 
import java.util.Iterator;
26
 
 
27
 
import org.apache.lucene.benchmark.byTask.Benchmark;
28
 
import org.apache.lucene.benchmark.byTask.feeds.DocData;
29
 
import org.apache.lucene.benchmark.byTask.feeds.NoMoreDataException;
30
 
import org.apache.lucene.benchmark.byTask.feeds.ReutersDocMaker;
31
 
import org.apache.lucene.benchmark.byTask.tasks.CountingSearchTestTask;
32
 
import org.apache.lucene.benchmark.byTask.stats.TaskStats;
33
 
import org.apache.lucene.index.IndexReader;
34
 
import org.apache.lucene.index.IndexWriter;
35
 
import org.apache.lucene.index.TermEnum;
36
 
import org.apache.lucene.index.TermDocs;
37
 
 
38
 
import junit.framework.TestCase;
39
 
 
40
 
/**
41
 
 * Test very simply that perf tasks - simple algorithms - are doing what they should.
42
 
 */
43
 
public class TestPerfTasksLogic extends TestCase {
44
 
 
45
 
  private static final boolean DEBUG = false;
46
 
  static final String NEW_LINE = System.getProperty("line.separator");
47
 
  
48
 
  // properties in effect in all tests here
49
 
  static final String propLines [] = {
50
 
    "directory=RAMDirectory",
51
 
    "print.props=false",
52
 
  };
53
 
  
54
 
  /**
55
 
   * @param name test name
56
 
   */
57
 
  public TestPerfTasksLogic(String name) {
58
 
    super(name);
59
 
  }
60
 
 
61
 
  /**
62
 
   * Test index creation logic
63
 
   */
64
 
  public void testIndexAndSearchTasks() throws Exception {
65
 
    // 1. alg definition (required in every "logic" test)
66
 
    String algLines[] = {
67
 
        "ResetSystemErase",
68
 
        "CreateIndex",
69
 
        "{ AddDoc } : 1000",
70
 
        "Optimize",
71
 
        "CloseIndex",
72
 
        "OpenReader",
73
 
        "{ CountingSearchTest } : 200",
74
 
        "CloseReader",
75
 
        "[ CountingSearchTest > : 70",
76
 
        "[ CountingSearchTest > : 9",
77
 
    };
78
 
    
79
 
    // 2. we test this value later
80
 
    CountingSearchTestTask.numSearches = 0;
81
 
    
82
 
    // 3. execute the algorithm  (required in every "logic" test)
83
 
    Benchmark benchmark = execBenchmark(algLines);
84
 
 
85
 
    // 4. test specific checks after the benchmark run completed.
86
 
    assertEquals("TestSearchTask was supposed to be called!",279,CountingSearchTestTask.numSearches);
87
 
    assertTrue("Index does not exist?...!", IndexReader.indexExists(benchmark.getRunData().getDirectory()));
88
 
    // now we should be able to open the index for write. 
89
 
    IndexWriter iw = new IndexWriter(benchmark.getRunData().getDirectory(),null,false);
90
 
    iw.close();
91
 
    IndexReader ir = IndexReader.open(benchmark.getRunData().getDirectory());
92
 
    assertEquals("1000 docs were added to the index, this is what we expect to find!",1000,ir.numDocs());
93
 
    ir.close();
94
 
  }
95
 
 
96
 
  /**
97
 
   * Test Exhasting Doc Maker logic
98
 
   */
99
 
  public void testExhaustDocMaker() throws Exception {
100
 
    // 1. alg definition (required in every "logic" test)
101
 
    String algLines[] = {
102
 
        "# ----- properties ",
103
 
        "doc.maker=org.apache.lucene.benchmark.byTask.feeds.SimpleDocMaker",
104
 
        "doc.add.log.step=1",
105
 
        "doc.term.vector=false",
106
 
        "doc.maker.forever=false",
107
 
        "directory=RAMDirectory",
108
 
        "doc.stored=false",
109
 
        "doc.tokenized=false",
110
 
        "# ----- alg ",
111
 
        "CreateIndex",
112
 
        "{ AddDoc } : * ",
113
 
        "Optimize",
114
 
        "CloseIndex",
115
 
        "OpenReader",
116
 
        "{ CountingSearchTest } : 100",
117
 
        "CloseReader",
118
 
        "[ CountingSearchTest > : 30",
119
 
        "[ CountingSearchTest > : 9",
120
 
    };
121
 
    
122
 
    // 2. we test this value later
123
 
    CountingSearchTestTask.numSearches = 0;
124
 
    
125
 
    // 3. execute the algorithm  (required in every "logic" test)
126
 
    Benchmark benchmark = execBenchmark(algLines);
127
 
 
128
 
    // 4. test specific checks after the benchmark run completed.
129
 
    assertEquals("TestSearchTask was supposed to be called!",139,CountingSearchTestTask.numSearches);
130
 
    assertTrue("Index does not exist?...!", IndexReader.indexExists(benchmark.getRunData().getDirectory()));
131
 
    // now we should be able to open the index for write. 
132
 
    IndexWriter iw = new IndexWriter(benchmark.getRunData().getDirectory(),null,false);
133
 
    iw.close();
134
 
    IndexReader ir = IndexReader.open(benchmark.getRunData().getDirectory());
135
 
    assertEquals("1 docs were added to the index, this is what we expect to find!",1,ir.numDocs());
136
 
    ir.close();
137
 
  }
138
 
 
139
 
  /**
140
 
   * Test Parallel Doc Maker logic (for LUCENE-940)
141
 
   */
142
 
  public void testParallelDocMaker() throws Exception {
143
 
    // 1. alg definition (required in every "logic" test)
144
 
    String algLines[] = {
145
 
        "# ----- properties ",
146
 
        "doc.maker="+Reuters20DocMaker.class.getName(),
147
 
        "doc.add.log.step=3",
148
 
        "doc.term.vector=false",
149
 
        "doc.maker.forever=false",
150
 
        "directory=FSDirectory",
151
 
        "doc.stored=false",
152
 
        "doc.tokenized=false",
153
 
        "# ----- alg ",
154
 
        "CreateIndex",
155
 
        "[ { AddDoc } : * ] : 4 ",
156
 
        "CloseIndex",
157
 
    };
158
 
    
159
 
    // 2. execute the algorithm  (required in every "logic" test)
160
 
    Benchmark benchmark = execBenchmark(algLines);
161
 
 
162
 
    // 3. test number of docs in the index
163
 
    IndexReader ir = IndexReader.open(benchmark.getRunData().getDirectory());
164
 
    int ndocsExpected = 20; // Reuters20DocMaker exhausts after 20 docs.
165
 
    assertEquals("wrong number of docs in the index!", ndocsExpected, ir.numDocs());
166
 
    ir.close();
167
 
  }
168
 
 
169
 
  /**
170
 
   * Test WriteLineDoc and LineDocMaker.
171
 
   */
172
 
  public void testLineDocFile() throws Exception {
173
 
    File lineFile = new File(System.getProperty("tempDir"), "test.reuters.lines.txt");
174
 
 
175
 
    // We will call WriteLineDocs this many times
176
 
    final int NUM_TRY_DOCS = 500;
177
 
 
178
 
    // Creates a line file with first 500 docs from reuters
179
 
    String algLines1[] = {
180
 
      "# ----- properties ",
181
 
      "doc.maker=org.apache.lucene.benchmark.byTask.feeds.ReutersDocMaker",
182
 
      "doc.maker.forever=false",
183
 
      "line.file.out=" + lineFile.getAbsolutePath().replace('\\', '/'),
184
 
      "# ----- alg ",
185
 
      "{WriteLineDoc()}:" + NUM_TRY_DOCS,
186
 
    };
187
 
 
188
 
    // Run algo
189
 
    Benchmark benchmark = execBenchmark(algLines1);
190
 
 
191
 
    // Verify we got somewhere between 1-500 lines (some
192
 
    // Reuters docs have no body, which WriteLineDoc task
193
 
    // skips).
194
 
    BufferedReader r = new BufferedReader(new FileReader(lineFile));
195
 
    int numLines = 0;
196
 
    while(r.readLine() != null)
197
 
      numLines++;
198
 
    r.close();
199
 
    assertTrue("did not see the right number of docs; should be > 0 and <= " + NUM_TRY_DOCS + " but was " + numLines, numLines > 0 && numLines <= NUM_TRY_DOCS);
200
 
    
201
 
    // Index the line docs
202
 
    String algLines2[] = {
203
 
      "# ----- properties ",
204
 
      "analyzer=org.apache.lucene.analysis.SimpleAnalyzer",
205
 
      "doc.maker=org.apache.lucene.benchmark.byTask.feeds.LineDocMaker",
206
 
      "docs.file=" + lineFile.getAbsolutePath().replace('\\', '/'),
207
 
      "doc.maker.forever=false",
208
 
      "autocommit=false",
209
 
      "ram.flush.mb=4",
210
 
      "# ----- alg ",
211
 
      "ResetSystemErase",
212
 
      "CreateIndex",
213
 
      "{AddDoc}: *",
214
 
      "CloseIndex",
215
 
    };
216
 
    
217
 
    // Run algo
218
 
    benchmark = execBenchmark(algLines2);
219
 
 
220
 
    // now we should be able to open the index for write. 
221
 
    IndexWriter iw = new IndexWriter(benchmark.getRunData().getDirectory(),null,false);
222
 
    iw.close();
223
 
 
224
 
    IndexReader ir = IndexReader.open(benchmark.getRunData().getDirectory());
225
 
    assertEquals(numLines + " lines were were created but " + ir.numDocs() + " docs are in the index", numLines, ir.numDocs());
226
 
    ir.close();
227
 
 
228
 
    lineFile.delete();
229
 
  }
230
 
  
231
 
  /**
232
 
   * Test ReadTokensTask
233
 
   */
234
 
  public void testReadTokens() throws Exception {
235
 
 
236
 
    // We will call ReadTokens on this many docs
237
 
    final int NUM_DOCS = 100;
238
 
 
239
 
    // Read tokens from first NUM_DOCS docs from Reuters and
240
 
    // then build index from the same docs
241
 
    String algLines1[] = {
242
 
      "# ----- properties ",
243
 
      "analyzer=org.apache.lucene.analysis.WhitespaceAnalyzer",
244
 
      "doc.maker=org.apache.lucene.benchmark.byTask.feeds.ReutersDocMaker",
245
 
      "# ----- alg ",
246
 
      "{ReadTokens}: " + NUM_DOCS,
247
 
      "ResetSystemErase",
248
 
      "CreateIndex",
249
 
      "{AddDoc}: " + NUM_DOCS,
250
 
      "CloseIndex",
251
 
    };
252
 
 
253
 
    // Run algo
254
 
    Benchmark benchmark = execBenchmark(algLines1);
255
 
 
256
 
    List stats = benchmark.getRunData().getPoints().taskStats();
257
 
 
258
 
    // Count how many tokens all ReadTokens saw
259
 
    int totalTokenCount1 = 0;
260
 
    for (Iterator it = stats.iterator(); it.hasNext();) {
261
 
      TaskStats stat = (TaskStats) it.next();
262
 
      if (stat.getTask().getName().equals("ReadTokens")) {
263
 
        totalTokenCount1 += stat.getCount();
264
 
      }
265
 
    }
266
 
 
267
 
    // Separately count how many tokens are actually in the index:
268
 
    IndexReader reader = IndexReader.open(benchmark.getRunData().getDirectory());
269
 
    assertEquals(NUM_DOCS, reader.numDocs());
270
 
 
271
 
    TermEnum terms = reader.terms();
272
 
    TermDocs termDocs = reader.termDocs();
273
 
    int totalTokenCount2 = 0;
274
 
    while(terms.next()) {
275
 
      termDocs.seek(terms.term());
276
 
      while(termDocs.next())
277
 
        totalTokenCount2 += termDocs.freq();
278
 
    }
279
 
    reader.close();
280
 
 
281
 
    // Make sure they are the same
282
 
    assertEquals(totalTokenCount1, totalTokenCount2);
283
 
  }
284
 
  
285
 
  /**
286
 
   * Test that " {[AddDoc(4000)]: 4} : * " works corrcetly (for LUCENE-941)
287
 
   */
288
 
  public void testParallelExhausted() throws Exception {
289
 
    // 1. alg definition (required in every "logic" test)
290
 
    String algLines[] = {
291
 
        "# ----- properties ",
292
 
        "doc.maker="+Reuters20DocMaker.class.getName(),
293
 
        "doc.add.log.step=3",
294
 
        "doc.term.vector=false",
295
 
        "doc.maker.forever=false",
296
 
        "directory=RAMDirectory",
297
 
        "doc.stored=false",
298
 
        "doc.tokenized=false",
299
 
        "debug.level=1",
300
 
        "# ----- alg ",
301
 
        "CreateIndex",
302
 
        "{ [ AddDoc]: 4} : * ",
303
 
        "ResetInputs ",
304
 
        "{ [ AddDoc]: 4} : * ",
305
 
        "CloseIndex",
306
 
    };
307
 
    
308
 
    // 2. execute the algorithm  (required in every "logic" test)
309
 
    Benchmark benchmark = execBenchmark(algLines);
310
 
 
311
 
    // 3. test number of docs in the index
312
 
    IndexReader ir = IndexReader.open(benchmark.getRunData().getDirectory());
313
 
    int ndocsExpected = 2 * 20; // Reuters20DocMaker exhausts after 20 docs.
314
 
    assertEquals("wrong number of docs in the index!", ndocsExpected, ir.numDocs());
315
 
    ir.close();
316
 
  }
317
 
 
318
 
  // create the benchmark and execute it. 
319
 
  public static Benchmark execBenchmark(String[] algLines) throws Exception {
320
 
    String algText = algLinesToText(algLines);
321
 
    logTstLogic(algText);
322
 
    Benchmark benchmark = new Benchmark(new StringReader(algText));
323
 
    benchmark.execute();
324
 
    return benchmark;
325
 
  }
326
 
  
327
 
  // catenate alg lines to make the alg text
328
 
  private static String algLinesToText(String[] algLines) {
329
 
    String indent = "  ";
330
 
    StringBuffer sb = new StringBuffer();
331
 
    for (int i = 0; i < propLines.length; i++) {
332
 
      sb.append(indent).append(propLines[i]).append(NEW_LINE);
333
 
    }
334
 
    for (int i = 0; i < algLines.length; i++) {
335
 
      sb.append(indent).append(algLines[i]).append(NEW_LINE);
336
 
    }
337
 
    return sb.toString();
338
 
  }
339
 
 
340
 
  private static void logTstLogic (String txt) {
341
 
    if (!DEBUG) 
342
 
      return;
343
 
    System.out.println("Test logic of:");
344
 
    System.out.println(txt);
345
 
  }
346
 
 
347
 
  /** use reuters and the exhaust mechanism, but to be faster, add 20 docs only... */
348
 
  public static class Reuters20DocMaker extends ReutersDocMaker {
349
 
    private int nDocs=0;
350
 
    protected synchronized DocData getNextDocData() throws Exception {
351
 
      if (nDocs>=20 && !forever) {
352
 
        throw new NoMoreDataException();
353
 
      }
354
 
      nDocs++;
355
 
      return super.getNextDocData();
356
 
    }
357
 
    public synchronized void resetInputs() {
358
 
      super.resetInputs();
359
 
      nDocs = 0;
360
 
    }
361
 
  }
362
 
  
363
 
  /**
364
 
   * Test that exhaust in loop works as expected (LUCENE-1115).
365
 
   */
366
 
  public void testExhaustedLooped() throws Exception {
367
 
    // 1. alg definition (required in every "logic" test)
368
 
    String algLines[] = {
369
 
        "# ----- properties ",
370
 
        "doc.maker="+Reuters20DocMaker.class.getName(),
371
 
        "doc.add.log.step=3",
372
 
        "doc.term.vector=false",
373
 
        "doc.maker.forever=false",
374
 
        "directory=RAMDirectory",
375
 
        "doc.stored=false",
376
 
        "doc.tokenized=false",
377
 
        "debug.level=1",
378
 
        "# ----- alg ",
379
 
        "{ \"Rounds\"",
380
 
        "  ResetSystemErase",
381
 
        "  CreateIndex",
382
 
        "  { \"AddDocs\"  AddDoc > : * ",
383
 
        "  CloseIndex",
384
 
        "} : 2",
385
 
    };
386
 
    
387
 
    // 2. execute the algorithm  (required in every "logic" test)
388
 
    Benchmark benchmark = execBenchmark(algLines);
389
 
 
390
 
    // 3. test number of docs in the index
391
 
    IndexReader ir = IndexReader.open(benchmark.getRunData().getDirectory());
392
 
    int ndocsExpected = 20; // Reuters20DocMaker exhausts after 20 docs.
393
 
    assertEquals("wrong number of docs in the index!", ndocsExpected, ir.numDocs());
394
 
    ir.close();
395
 
  }
396
 
}