001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.load.multi;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.util.Date;
022: import java.util.Properties;
023:
024: import org.compass.core.Compass;
025: import org.compass.core.CompassCallbackWithoutResult;
026: import org.compass.core.CompassException;
027: import org.compass.core.CompassHits;
028: import org.compass.core.CompassSession;
029: import org.compass.core.CompassTemplate;
030: import org.compass.core.config.CompassConfiguration;
031:
032: /**
033: * @author kimchy
034: */
035: public class SimpleLoadTester {
036:
037: private static Long id = new Long(0);
038:
039: private static final Object idLock = new Object();
040:
041: private static Long createNextId() {
042: synchronized (idLock) {
043: id = new Long(id.longValue() + 1);
044: return id;
045: }
046: }
047:
048: public static class SimpleLoadTesterRunnable implements Runnable {
049:
050: private long cycles;
051:
052: private long runId;
053:
054: private int writeFactor;
055:
056: private Long lastIdWritten;
057:
058: private CompassTemplate template;
059:
060: public SimpleLoadTesterRunnable(CompassTemplate template,
061: long runId, long cycles, int writeFactor) {
062: this .cycles = cycles;
063: this .runId = runId;
064: this .writeFactor = writeFactor;
065: this .template = template;
066: }
067:
068: private String failureStringPrefix(long cycle, Long id) {
069: return "FAILURE RUN[" + runId + "] CYCLE[" + cycle
070: + "] ID[" + id + "] ";
071: }
072:
073: private void check(CompassSession session, long cycle, Long id) {
074: A a = (A) session.get(A.class, id);
075: if (a == null) {
076: System.err.println(failureStringPrefix(cycle, id)
077: + " A NULL");
078: }
079:
080: CompassHits hits = session.find("mdata1:" + id);
081: if (0 == hits.length()) {
082: System.err.println(failureStringPrefix(cycle, id)
083: + " HITS ZERO [" + hits.length() + "]");
084: }
085: }
086:
087: public void run() {
088: long totalTime = System.currentTimeMillis();
089: for (long i = 0; i < cycles; i++) {
090: final long cycle = i;
091:
092: if (cycle % writeFactor == 0) {
093: template
094: .execute(new CompassCallbackWithoutResult() {
095: protected void doInCompassWithoutResult(
096: CompassSession session)
097: throws CompassException {
098: lastIdWritten = createNextId();
099: A a = (A) session.get(A.class,
100: lastIdWritten);
101: if (a != null) {
102: System.err
103: .println(failureStringPrefix(
104: cycle,
105: lastIdWritten)
106: + " A NOT NULL ["
107: + a + "]");
108: }
109:
110: CompassHits hits = session
111: .find("mdata1:"
112: + lastIdWritten);
113: if (0 != hits.length()) {
114: System.err
115: .println(failureStringPrefix(
116: cycle,
117: lastIdWritten)
118: + " HITS NOT ZERO ["
119: + hits.length()
120: + "]");
121: }
122:
123: a = new A();
124: a.setId(lastIdWritten);
125: a.setData1("" + lastIdWritten);
126: a.setIndexTime(new Date());
127: session.save(a);
128:
129: check(session, cycle, lastIdWritten);
130: }
131: });
132: } else {
133: template
134: .execute(new CompassCallbackWithoutResult() {
135: protected void doInCompassWithoutResult(
136: CompassSession session)
137: throws CompassException {
138: check(session, cycle, lastIdWritten);
139: }
140: });
141: }
142:
143: }
144: totalTime = System.currentTimeMillis() - totalTime;
145: System.out.println("FINISHED RUN [" + runId + "] TOOK ["
146: + totalTime + "]");
147: }
148: }
149:
150: public static void main(String[] args) throws Exception {
151:
152: int numberOfRuns = 5;
153: long numberOfCycles = 200;
154: int writeFactor = 10;
155: int numberOfCompassInstances = 1;
156:
157: CompassConfiguration conf = new CompassConfiguration();
158: conf.configure("/org/compass/core/load/multi/compass.cfg.xml");
159: File testPropsFile = new File("compass.test.properties");
160: if (testPropsFile.exists()) {
161: Properties testProps = new Properties();
162: testProps.load(new FileInputStream(testPropsFile));
163: conf.getSettings().addSettings(testProps);
164: }
165: conf.addClass(A.class);
166:
167: CompassTemplate[] templates = new CompassTemplate[numberOfCompassInstances];
168: for (int i = 0; i < numberOfCompassInstances; i++) {
169: Compass compass = conf.buildCompass();
170: templates[i] = new CompassTemplate(compass);
171: }
172:
173: templates[0].getCompass().getSearchEngineIndexManager()
174: .deleteIndex();
175: templates[0].getCompass().getSearchEngineIndexManager()
176: .createIndex();
177:
178: Thread[] threads = new Thread[numberOfRuns];
179: for (int i = 0; i < threads.length; i++) {
180: threads[i] = new Thread(new SimpleLoadTesterRunnable(
181: templates[i % numberOfCompassInstances], i,
182: numberOfCycles, writeFactor), "L" + i);
183: }
184:
185: for (int i = 0; i < threads.length; i++) {
186: threads[i].start();
187: }
188:
189: for (int i = 0; i < threads.length; i++) {
190: try {
191: threads[i].join();
192: } catch (InterruptedException e) {
193: e.printStackTrace();
194: }
195: }
196:
197: System.out.println("VERIFYING INDEX USING EXISTING TEMPLATE");
198: // now check that everything is in the index
199: check(templates[0]);
200:
201: for (int i = 0; i < numberOfCompassInstances; i++) {
202: templates[i].getCompass().close();
203: }
204:
205: System.out
206: .println("VERIFYING INDEX USING NEW COMPASS INSTANCE");
207: // now build a new one and check again
208: CompassTemplate template = new CompassTemplate(conf
209: .buildCompass());
210: check(template);
211: template.getCompass().close();
212: }
213:
214: private static void check(CompassTemplate template) {
215: long time = System.currentTimeMillis();
216: long limit = id.longValue();
217: for (long i = 1; i < limit; i++) {
218: final Long id = new Long(i);
219: template.execute(new CompassCallbackWithoutResult() {
220: protected void doInCompassWithoutResult(
221: CompassSession session) throws CompassException {
222: A a = (A) session.get(A.class, id);
223: if (a == null) {
224: System.err.println("FAILURE ID [" + id
225: + "] FINAL CHECK NULL");
226: }
227: }
228: });
229: }
230: System.out.println("FINISHED CHECK [1-" + limit + "] TOOK ["
231: + (System.currentTimeMillis() - time) + "]");
232: }
233: }
|