add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / index / TestTermdocPerf.java
1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2006 The Apache Software Foundation
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19
20 import java.io.IOException;
21 import java.io.Reader;
22 import java.util.Random;
23
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.analysis.TokenStream;
26 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
30 import org.apache.lucene.store.Directory;
31 import org.apache.lucene.util.LuceneTestCase;
32
33 class RepeatingTokenStream extends TokenStream {
34   public int num;
35   CharTermAttribute termAtt;
36   String value;
37
38    public RepeatingTokenStream(String val) {
39      this.value = val;
40      this.termAtt = addAttribute(CharTermAttribute.class);
41    }
42
43    @Override
44    public boolean incrementToken() throws IOException {
45      num--;
46      if (num >= 0) {
47        clearAttributes();
48        termAtt.append(value);
49        return true;
50      }
51      return false;
52    }
53 }
54
55
56 public class TestTermdocPerf extends LuceneTestCase {
57
58   void addDocs(final Random random, Directory dir, final int ndocs, String field, final String val, final int maxTF, final float percentDocs) throws IOException {
59     final RepeatingTokenStream ts = new RepeatingTokenStream(val);
60
61     Analyzer analyzer = new Analyzer() {
62       @Override
63       public TokenStream tokenStream(String fieldName, Reader reader) {
64         if (random.nextFloat() < percentDocs) ts.num = random.nextInt(maxTF)+1;
65         else ts.num=0;
66         return ts;
67       }
68     };
69
70     Document doc = new Document();
71     doc.add(newField(field,val, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
72     IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
73         TEST_VERSION_CURRENT, analyzer)
74         .setOpenMode(OpenMode.CREATE).setMaxBufferedDocs(100));
75     ((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(100);
76
77     for (int i=0; i<ndocs; i++) {
78       writer.addDocument(doc);
79     }
80
81     writer.optimize();
82     writer.close();
83   }
84
85
86   public int doTest(int iter, int ndocs, int maxTF, float percentDocs) throws IOException {
87     Directory dir = newDirectory();
88
89     long start = System.currentTimeMillis();
90     addDocs(random, dir, ndocs, "foo", "val", maxTF, percentDocs);
91     long end = System.currentTimeMillis();
92     if (VERBOSE) System.out.println("milliseconds for creation of " + ndocs + " docs = " + (end-start));
93
94     IndexReader reader = IndexReader.open(dir, true);
95     TermEnum tenum = reader.terms(new Term("foo","val"));
96     TermDocs tdocs = reader.termDocs();
97
98     start = System.currentTimeMillis();
99
100     int ret=0;
101     for (int i=0; i<iter; i++) {
102       tdocs.seek(tenum);
103       while (tdocs.next()) {
104         ret += tdocs.doc();
105       }
106     }
107
108     end = System.currentTimeMillis();
109     if (VERBOSE) System.out.println("milliseconds for " + iter + " TermDocs iteration: " + (end-start));
110
111     return ret;
112   }
113
114   public void testTermDocPerf() throws IOException {
115     // performance test for 10% of documents containing a term
116     // doTest(100000, 10000,3,.1f);
117   }
118
119
120 }