001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.search;
017:
018: import org.apache.solr.search.BitDocSet;
019: import org.apache.solr.search.HashDocSet;
020: import org.apache.solr.search.DocSet;
021: import org.apache.solr.util.OpenBitSet;
022:
023: import java.util.Random;
024: import java.util.BitSet;
025:
026: /**
027: * @author yonik
028: */
029: public class DocSetPerf {
030:
031: // use test instead of assert since asserts may be turned off
032: public static void test(boolean condition) {
033: if (!condition) {
034: throw new RuntimeException(
035: "test requestHandler: assertion failed!");
036: }
037: }
038:
039: static Random rand = new Random();
040:
041: static OpenBitSet bs;
042: static BitDocSet bds;
043: static HashDocSet hds;
044: static int[] ids; // not unique
045:
046: static void generate(int maxSize, int bitsToSet) {
047: bs = new OpenBitSet(maxSize);
048: ids = new int[bitsToSet];
049: int count = 0;
050: if (maxSize > 0) {
051: for (int i = 0; i < bitsToSet; i++) {
052: int id = rand.nextInt(maxSize);
053: if (!bs.get(id)) {
054: bs.fastSet(id);
055: ids[count++] = id;
056: }
057: }
058: }
059: bds = new BitDocSet(bs, bitsToSet);
060: hds = new HashDocSet(ids, 0, count);
061: }
062:
063: public static void main(String[] args) {
064: String bsSize = args[0];
065: boolean randSize = false;
066:
067: if (bsSize.endsWith("-")) {
068: bsSize = bsSize.substring(0, bsSize.length() - 1);
069: randSize = true;
070: }
071:
072: int bitSetSize = Integer.parseInt(bsSize);
073: int numSets = Integer.parseInt(args[1]);
074: int numBitsSet = Integer.parseInt(args[2]);
075: String test = args[3].intern();
076: int iter = Integer.parseInt(args[4]);
077:
078: int ret = 0;
079:
080: OpenBitSet[] sets = new OpenBitSet[numSets];
081: DocSet[] bset = new DocSet[numSets];
082: DocSet[] hset = new DocSet[numSets];
083: BitSet scratch = new BitSet();
084:
085: for (int i = 0; i < numSets; i++) {
086: generate(randSize ? rand.nextInt(bitSetSize) : bitSetSize,
087: numBitsSet);
088: sets[i] = bs;
089: bset[i] = bds;
090: hset[i] = hds;
091: }
092:
093: long start = System.currentTimeMillis();
094:
095: if ("test".equals(test)) {
096: for (int it = 0; it < iter; it++) {
097: generate(randSize ? rand.nextInt(bitSetSize)
098: : bitSetSize, numBitsSet);
099: OpenBitSet bs1 = bs;
100: BitDocSet bds1 = bds;
101: HashDocSet hds1 = hds;
102: generate(randSize ? rand.nextInt(bitSetSize)
103: : 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")) {
131: type = "B";
132: }
133: if (test.endsWith("H")) {
134: type = "H";
135: }
136: if (test.endsWith("M")) {
137: type = "M";
138: }
139: if (test.startsWith("intersect"))
140: oper = "intersect";
141: if (test.startsWith("intersectSize"))
142: oper = "intersectSize";
143: if (test.startsWith("intersectAndSize"))
144: oper = "intersectSize";
145:
146: if (oper != null) {
147: for (int it = 0; it < iter; it++) {
148: int idx1 = rand.nextInt(numSets);
149: int idx2 = rand.nextInt(numSets);
150: DocSet a = null, b = null;
151:
152: if (type == "B") {
153: a = bset[idx1];
154: b = bset[idx2];
155: } else if (type == "H") {
156: a = hset[idx1];
157: b = bset[idx2];
158: } else if (type == "M") {
159: if (idx1 < idx2) {
160: a = bset[idx1];
161: b = hset[idx2];
162: } else {
163: a = hset[idx1];
164: b = bset[idx2];
165: }
166: }
167:
168: if (oper == "intersect") {
169: DocSet res = a.intersection(b);
170: ret += res.memSize();
171: } else if (oper == "intersectSize") {
172: ret += a.intersectionSize(b);
173: } else if (oper == "intersectAndSize") {
174: DocSet res = a.intersection(b);
175: ret += res.size();
176: }
177: }
178: }
179:
180: long end = System.currentTimeMillis();
181: System.out.println("TIME=" + (end - start));
182:
183: // System.out.println("ret="+ret + " scratchsize="+scratch.size());
184: System.out.println("ret=" + ret);
185: }
186:
187: }
|