Clover Coverage Report
Coverage timestamp: Thu Apr 10 2008 16:32:38 EST
35   115   10   5.83
10   68   0.29   3
6     1.67  
2    
 
  RepeatingTokenStream       Line # 38 2 2 0% 0.0
  TestTermdocPerf       Line # 52 33 8 2.2% 0.022222223
 
  (1)
 
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 junit.framework.TestCase;
21    import org.apache.lucene.store.Directory;
22    import org.apache.lucene.store.RAMDirectory;
23    import org.apache.lucene.analysis.Analyzer;
24    import org.apache.lucene.analysis.TokenStream;
25    import org.apache.lucene.analysis.Token;
26    import org.apache.lucene.document.Document;
27    import org.apache.lucene.document.Field;
28   
29    import java.io.Reader;
30    import java.io.IOException;
31    import java.util.Random;
32   
33    /**
34    * @author yonik
35    * @version $Id$
36    */
37   
 
38    class RepeatingTokenStream extends TokenStream {
39    public int num;
40    Token t;
41   
 
42  0 toggle public RepeatingTokenStream(String val) {
43  0 t = new Token(val,0,val.length());
44    }
45   
 
46  0 toggle public Token next() throws IOException {
47  0 return --num<0 ? null : t;
48    }
49    }
50   
51   
 
52    public class TestTermdocPerf extends TestCase {
53   
 
54  0 toggle void addDocs(Directory dir, final int ndocs, String field, final String val, final int maxTF, final float percentDocs) throws IOException {
55  0 final Random random = new Random(0);
56  0 final RepeatingTokenStream ts = new RepeatingTokenStream(val);
57   
58  0 Analyzer analyzer = new Analyzer() {
 
59  0 toggle public TokenStream tokenStream(String fieldName, Reader reader) {
60  0 if (random.nextFloat() < percentDocs) ts.num = random.nextInt(maxTF)+1;
61  0 else ts.num=0;
62  0 return ts;
63    }
64    };
65   
66  0 Document doc = new Document();
67  0 doc.add(new Field(field,val, Field.Store.NO, Field.Index.NO_NORMS));
68  0 IndexWriter writer = new IndexWriter(dir, analyzer, true);
69  0 writer.setMaxBufferedDocs(100);
70  0 writer.setMergeFactor(100);
71   
72  0 for (int i=0; i<ndocs; i++) {
73  0 writer.addDocument(doc);
74    }
75   
76  0 writer.optimize();
77  0 writer.close();
78    }
79   
80   
 
81  0 toggle public int doTest(int iter, int ndocs, int maxTF, float percentDocs) throws IOException {
82  0 Directory dir = new RAMDirectory();
83   
84  0 long start = System.currentTimeMillis();
85  0 addDocs(dir, ndocs, "foo", "val", maxTF, percentDocs);
86  0 long end = System.currentTimeMillis();
87  0 System.out.println("milliseconds for creation of " + ndocs + " docs = " + (end-start));
88   
89  0 IndexReader reader = IndexReader.open(dir);
90  0 TermEnum tenum = reader.terms(new Term("foo","val"));
91  0 TermDocs tdocs = reader.termDocs();
92   
93  0 start = System.currentTimeMillis();
94   
95  0 int ret=0;
96  0 for (int i=0; i<iter; i++) {
97  0 tdocs.seek(tenum);
98  0 while (tdocs.next()) {
99  0 ret += tdocs.doc();
100    }
101    }
102   
103  0 end = System.currentTimeMillis();
104  0 System.out.println("milliseconds for " + iter + " TermDocs iteration: " + (end-start));
105   
106  0 return ret;
107    }
108   
 
109  1 toggle public void testTermDocPerf() throws IOException {
110    // performance test for 10% of documents containing a term
111    // doTest(100000, 10000,3,.1f);
112    }
113   
114   
115    }