Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
126   379   43   7
54   219   0.34   4.5
18     2.39  
4    
 
  TestScorerPerf       Line # 39 115 35 47.2% 0.47191012
  TestScorerPerf.BitSetFilter       Line # 93 2 2 100% 1.0
  TestScorerPerf.CountingHitCollector       Line # 103 4 3 100% 1.0
  TestScorerPerf.MatchingHitCollector       Line # 117 5 3 77.8% 0.7777778
 
  (1)
 
1    package org.apache.lucene.search;
2   
3    import junit.framework.TestCase;
4   
5    import java.util.Random;
6    import java.util.BitSet;
7    import java.util.Set;
8    import java.io.IOException;
9   
10    import org.apache.lucene.index.IndexReader;
11    import org.apache.lucene.index.IndexWriter;
12    import org.apache.lucene.index.Term;
13    import org.apache.lucene.store.RAMDirectory;
14    import org.apache.lucene.store.Directory;
15    import org.apache.lucene.analysis.WhitespaceAnalyzer;
16    import org.apache.lucene.document.Document;
17    import org.apache.lucene.document.Field;
18   
19    /**
20    * Copyright 2006 The Apache Software Foundation
21    *
22    * Licensed under the Apache License, Version 2.0 (the "License");
23    * you may not use this file except in compliance with the License.
24    * You may obtain a copy of the License at
25    *
26    * http://www.apache.org/licenses/LICENSE-2.0
27    *
28    * Unless required by applicable law or agreed to in writing, software
29    * distributed under the License is distributed on an "AS IS" BASIS,
30    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31    * See the License for the specific language governing permissions and
32    * limitations under the License.
33    */
34   
35    /**
36    * @author yonik
37    * @version $Id$
38    */
 
39    public class TestScorerPerf extends TestCase {
40    Random r = new Random(0);
41    boolean validate = true; // set to false when doing performance testing
42   
43    BitSet[] sets;
44    IndexSearcher s;
45   
 
46  1 toggle public void createDummySearcher() throws Exception {
47    // Create a dummy index with nothing in it.
48    // This could possibly fail if Lucene starts checking for docid ranges...
49  1 RAMDirectory rd = new RAMDirectory();
50  1 IndexWriter iw = new IndexWriter(rd,new WhitespaceAnalyzer(), true);
51  1 iw.close();
52  1 s = new IndexSearcher(rd);
53    }
54   
 
55  0 toggle public void createRandomTerms(int nDocs, int nTerms, double power, Directory dir) throws Exception {
56  0 int[] freq = new int[nTerms];
57  0 for (int i=0; i<nTerms; i++) {
58  0 int f = (nTerms+1)-i; // make first terms less frequent
59  0 freq[i] = (int)Math.ceil(Math.pow(f,power));
60    }
61   
62  0 IndexWriter iw = new IndexWriter(dir,new WhitespaceAnalyzer(), true);
63  0 iw.setMaxBufferedDocs(123);
64  0 for (int i=0; i<nDocs; i++) {
65  0 Document d = new Document();
66  0 for (int j=0; j<nTerms; j++) {
67  0 if (r.nextInt(freq[j]) == 0) {
68  0 d.add(new Field("f", Character.toString((char)j), Field.Store.NO, Field.Index.UN_TOKENIZED));
69    }
70    }
71  0 iw.addDocument(d);
72    }
73  0 iw.close();
74    }
75   
76   
 
77  1000 toggle public BitSet randBitSet(int sz, int numBitsToSet) {
78  1000 BitSet set = new BitSet(sz);
79  5614 for (int i=0; i<numBitsToSet; i++) {
80  4614 set.set(r.nextInt(sz));
81    }
82  1000 return set;
83    }
84   
 
85  1 toggle public BitSet[] randBitSets(int numSets, int setSize) {
86  1 BitSet[] sets = new BitSet[numSets];
87  1001 for (int i=0; i<sets.length; i++) {
88  1000 sets[i] = randBitSet(setSize, r.nextInt(setSize));
89    }
90  1 return sets;
91    }
92   
 
93    public static class BitSetFilter extends Filter {
94    public BitSet set;
 
95  97500 toggle public BitSetFilter(BitSet set) {
96  97500 this.set = set;
97    }
 
98  97500 toggle public BitSet bits(IndexReader reader) throws IOException {
99  97500 return set;
100    }
101    }
102   
 
103    public static class CountingHitCollector extends HitCollector {
104    int count=0;
105    int sum=0;
106   
 
107  5276 toggle public void collect(int doc, float score) {
108  5276 count++;
109  5276 sum += doc; // use it to avoid any possibility of being optimized away
110    }
111   
 
112  20000 toggle public int getCount() { return count; }
 
113  20000 toggle public int getSum() { return sum; }
114    }
115   
116   
 
117    public static class MatchingHitCollector extends CountingHitCollector {
118    BitSet answer;
119    int pos=-1;
 
120  20000 toggle public MatchingHitCollector(BitSet answer) {
121  20000 this.answer = answer;
122    }
123   
 
124