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

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/search/DocSetPerf.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
 
/**
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.solr.search;
19
 
 
20
 
import org.apache.solr.search.BitDocSet;
21
 
import org.apache.solr.search.HashDocSet;
22
 
import org.apache.solr.search.DocSet;
23
 
import org.apache.lucene.util.OpenBitSet;
24
 
 
25
 
import java.util.Random;
26
 
import java.util.BitSet;
27
 
 
28
 
/**
29
 
 */
30
 
public class DocSetPerf {
31
 
 
32
 
  // use test instead of assert since asserts may be turned off
33
 
  public static void test(boolean condition) {
34
 
      if (!condition) {
35
 
        throw new RuntimeException("test requestHandler: assertion failed!");
36
 
      }
37
 
  }
38
 
 
39
 
  static Random rand = new Random();
40
 
 
41
 
 
42
 
  static OpenBitSet bs;
43
 
  static BitDocSet bds;
44
 
  static HashDocSet hds;
45
 
  static int[] ids; // not unique
46
 
 
47
 
  static void generate(int maxSize, int bitsToSet) {
48
 
    bs = new OpenBitSet(maxSize);
49
 
    ids = new int[bitsToSet];
50
 
    int count=0;
51
 
    if (maxSize>0) {
52
 
      for (int i=0; i<bitsToSet; i++) {
53
 
        int id=rand.nextInt(maxSize);
54
 
        if (!bs.get(id)) {
55
 
          bs.fastSet(id);
56
 
          ids[count++]=id;
57
 
        }
58
 
      }
59
 
    }
60
 
    bds = new BitDocSet(bs,bitsToSet);
61
 
    hds = new HashDocSet(ids,0,count);
62
 
  }
63
 
 
64
 
 
65
 
 
66
 
  public static void main(String[] args) {
67
 
    String bsSize=args[0];
68
 
    boolean randSize=false;
69
 
 
70
 
    if (bsSize.endsWith("-")) {
71
 
      bsSize=bsSize.substring(0,bsSize.length()-1);
72
 
      randSize=true;
73
 
    }
74
 
 
75
 
    int bitSetSize = Integer.parseInt(bsSize);
76
 
    int numSets = Integer.parseInt(args[1]);
77
 
    int numBitsSet = Integer.parseInt(args[2]);
78
 
    String test = args[3].intern();
79
 
    int iter = Integer.parseInt(args[4]);
80
 
 
81
 
    int ret=0;
82
 
 
83
 
    OpenBitSet[] sets = new OpenBitSet[numSets];
84
 
    DocSet[] bset = new DocSet[numSets];
85
 
    DocSet[] hset = new DocSet[numSets];
86
 
    BitSet scratch=new BitSet();
87
 
 
88
 
    for (int i=0; i<numSets; i++) {
89
 
      generate(randSize ? rand.nextInt(bitSetSize) : bitSetSize, numBitsSet);
90
 
      sets[i] = bs;
91
 
      bset[i] = bds;
92
 
      hset[i] = hds;
93
 
    }
94
 
 
95
 
    long start = System.currentTimeMillis();
96
 
 
97
 
    if ("test".equals(test)) {
98
 
      for (int it=0; it<iter; it++) {
99
 
        generate(randSize ? rand.nextInt(bitSetSize) : bitSetSize, numBitsSet);
100
 
        OpenBitSet bs1=bs;
101
 
        BitDocSet bds1=bds;
102
 
        HashDocSet hds1=hds;
103
 
        generate(randSize ? rand.nextInt(bitSetSize) : bitSetSize, numBitsSet);
104
 
 
105
 
        OpenBitSet res = ((OpenBitSet)bs1.clone());
106
 
        res.and(bs);
107
 
        int icount = (int)res.cardinality();
108
 
 
109
 
        test(bds1.intersection(bds).size() == icount);
110
 
        test(bds1.intersectionSize(bds) == icount);
111
 
        if (bds1.intersection(hds).size() != icount) {
112
 
          DocSet ds = bds1.intersection(hds);
113
 
          System.out.println("STOP");
114
 
        }
115
 
 
116
 
        test(bds1.intersection(hds).size() == icount);
117
 
        test(bds1.intersectionSize(hds) == icount);
118
 
        test(hds1.intersection(bds).size() == icount);
119
 
        test(hds1.intersectionSize(bds) == icount);
120
 
        test(hds1.intersection(hds).size() == icount);
121
 
        test(hds1.intersectionSize(hds) == icount);
122
 
 
123
 
        ret += icount;
124
 
      }
125
 
    }
126
 
 
127
 
    String type=null;
128
 
    String oper=null;
129
 
 
130
 
    if (test.endsWith("B")) { type="B"; }
131
 
    if (test.endsWith("H")) { type="H"; }
132
 
    if (test.endsWith("M")) { type="M"; }
133
 
    if (test.startsWith("intersect")) oper="intersect";
134
 
    if (test.startsWith("intersectSize")) oper="intersectSize";
135
 
    if (test.startsWith("intersectAndSize")) oper="intersectSize";
136
 
 
137
 
 
138
 
    if (oper!=null) {
139
 
      for (int it=0; it<iter; it++) {
140
 
        int idx1 = rand.nextInt(numSets);
141
 
        int idx2 = rand.nextInt(numSets);
142
 
        DocSet a=null,b=null;
143
 
 
144
 
        if (type=="B") {
145
 
          a=bset[idx1]; b=bset[idx2];
146
 
        } else if (type=="H") {
147
 
          a=hset[idx1]; b=bset[idx2];
148
 
        } else if (type=="M") {
149
 
          if (idx1 < idx2) {
150
 
            a=bset[idx1];
151
 
            b=hset[idx2];
152
 
          } else {
153
 
            a=hset[idx1];
154
 
            b=bset[idx2];
155
 
          }
156
 
        }
157
 
 
158
 
        if (oper=="intersect") {
159
 
          DocSet res = a.intersection(b);
160
 
          ret += res.memSize();
161
 
        } else if (oper=="intersectSize") {
162
 
          ret += a.intersectionSize(b);
163
 
        } else if (oper=="intersectAndSize") {
164
 
          DocSet res = a.intersection(b);
165
 
          ret += res.size();
166
 
        }
167
 
      }
168
 
    }
169
 
 
170
 
 
171
 
 
172
 
    long end = System.currentTimeMillis();
173
 
    System.out.println("TIME="+(end-start));
174
 
 
175
 
    // System.out.println("ret="+ret + " scratchsize="+scratch.size());
176
 
    System.out.println("ret="+ret);
177
 
  }
178
 
 
179
 
 
180
 
 
181
 
}