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.simple;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.io.PrintStream;
023: import java.util.Date;
024: import java.util.Properties;
025:
026: import org.compass.core.Compass;
027: import org.compass.core.CompassSession;
028: import org.compass.core.CompassTransaction;
029: import org.compass.core.Resource;
030: import org.compass.core.config.CompassConfiguration;
031: import org.compass.core.lucene.util.LuceneHelper;
032:
033: /**
034: * @author kimchy
035: */
036: public class LoadTester {
037:
038: private static final String[] datas = new String[] {
039: "The big brown fox",
040: "The hitchiker guide to the galaxy",
041: "White russian",
042: "The player of Games",
043: "But it's so simple. All I have to do is divine from what I know"
044: + "of you: are you the sort of man who would put the poison into his"
045: + "own goblet or his enemy's? Now, a clever man would put the poison"
046: + "into his own goblet, because he would know that only a great fool"
047: + "would reach for what he was given. I am not a great fool, so I"
048: + "can clearly not choose the wine in front of you. But you must"
049: + "have known I was not a great fool, you would have counted on it,"
050: + "so I can clearly not choose the wine in front of me.",
051: "I am the law, now give me my white russian" };
052:
053: private long numberPerCycle = 10;
054:
055: private int numberOfCycles = 200;
056:
057: public void runTests(PrintStream printer) throws IOException {
058: printer.println("Run: Number Of Cycles[" + numberOfCycles
059: + "] Number Per Cycle [" + numberPerCycle + "]");
060: CompassConfiguration conf = new CompassConfiguration();
061: conf.configure("/org/compass/core/load/simple/compass.cfg.xml");
062: File testPropsFile = new File("compass.test.properties");
063: if (testPropsFile.exists()) {
064: Properties testProps = new Properties();
065: testProps.load(new FileInputStream(testPropsFile));
066: conf.getSettings().addSettings(testProps);
067: }
068: conf.addClass(A.class);
069: Compass compass = conf.buildCompass();
070: runTest("Default", compass, printer);
071: compass.close();
072: }
073:
074: public void runTest(String runName, Compass compass,
075: PrintStream writer) throws IOException {
076: compass.getSearchEngineIndexManager().deleteIndex();
077: compass.getSearchEngineIndexManager().verifyIndex();
078: writer.println(runName);
079: writer
080: .println("Cycle\tTotal\tSave\tFind\tCommit\tFind\tFind\tLoad\tLoad\tLoadR\tTermInfo\tOptimize");
081: long temp;
082: long totalCycleTime[] = new long[numberOfCycles];
083: long saveTime[] = new long[numberOfCycles];
084: long commitTime[] = new long[numberOfCycles];
085: long findBeforeCommit[] = new long[numberOfCycles];
086: long findAfterCommit[] = new long[numberOfCycles];
087: long findAfterCommit2[] = new long[numberOfCycles];
088: long load[] = new long[numberOfCycles];
089: long load2[] = new long[numberOfCycles];
090: long loadR[] = new long[numberOfCycles];
091: long termInfo[] = new long[numberOfCycles];
092: long optimize[] = new long[numberOfCycles];
093: for (int cycle = 0; cycle < numberOfCycles; cycle++) {
094: CompassSession session = compass.openSession();
095: CompassTransaction tr = session.beginTransaction();
096: long cycleStartTime = System.currentTimeMillis();
097: for (long i = 0; i < numberPerCycle; i++) {
098: A a = new A();
099: long id = cycle * numberPerCycle + i;
100: a.setId(new Long(id));
101: a.setData1(datas[(int) (id % datas.length)]);
102: a.setData2(datas[(int) ((id + 1) % datas.length)]);
103: a.setIndexTime(new Date());
104:
105: temp = System.currentTimeMillis();
106: session.save(a);
107: saveTime[cycle] += System.currentTimeMillis() - temp;
108: }
109:
110: temp = System.currentTimeMillis();
111: try {
112: session.find("white");
113: findBeforeCommit[cycle] = System.currentTimeMillis()
114: - temp;
115: } catch (Exception e) {
116: findBeforeCommit[cycle] = -1;
117: }
118:
119: temp = System.currentTimeMillis();
120: tr.commit();
121: commitTime[cycle] = System.currentTimeMillis() - temp;
122:
123: tr = session.beginTransaction();
124: temp = System.currentTimeMillis();
125: try {
126: session.find("white");
127: findAfterCommit[cycle] = System.currentTimeMillis()
128: - temp;
129: } catch (Exception e) {
130: findAfterCommit[cycle] = -1;
131: }
132: tr.commit();
133:
134: tr = session.beginTransaction();
135: temp = System.currentTimeMillis();
136: try {
137: session.find("white");
138: findAfterCommit2[cycle] = System.currentTimeMillis()
139: - temp;
140: } catch (Exception e) {
141: findAfterCommit2[cycle] = -1;
142: }
143: tr.commit();
144:
145: tr = session.beginTransaction();
146: temp = System.currentTimeMillis();
147: try {
148: session.load(A.class, new Long(1));
149: load[cycle] = System.currentTimeMillis() - temp;
150: } catch (Exception e) {
151: load[cycle] = -1;
152: }
153: tr.commit();
154:
155: tr = session.beginTransaction();
156: temp = System.currentTimeMillis();
157: try {
158: session.load(A.class, new Long(1));
159: load2[cycle] = System.currentTimeMillis() - temp;
160: } catch (Exception e) {
161: load2[cycle] = -1;
162: }
163: tr.commit();
164:
165: tr = session.beginTransaction();
166: temp = System.currentTimeMillis();
167: try {
168: session.loadResource(A.class, new Long(1));
169: loadR[cycle] = System.currentTimeMillis() - temp;
170: } catch (Exception e) {
171: loadR[cycle] = -1;
172: }
173: tr.commit();
174:
175: tr = session.beginTransaction();
176: temp = System.currentTimeMillis();
177: try {
178: Resource r = session.loadResource(A.class, new Long(1));
179: LuceneHelper.getTermFreqVectors(session, r);
180: termInfo[cycle] = System.currentTimeMillis() - temp;
181: } catch (Exception e) {
182: termInfo[cycle] = -1;
183: }
184: tr.commit();
185:
186: temp = System.currentTimeMillis();
187: compass.getSearchEngineOptimizer().optimize();
188: optimize[cycle] = System.currentTimeMillis() - temp;
189:
190: long cycleEndTime = System.currentTimeMillis();
191: totalCycleTime[cycle] = (cycleEndTime - cycleStartTime);
192: writer.print("" + cycle + "\t" + totalCycleTime[cycle]);
193: writer.print("\t" + saveTime[cycle]);
194: writer.print("\t" + findBeforeCommit[cycle]);
195: writer.print("\t" + commitTime[cycle]);
196: writer.print("\t" + findAfterCommit[cycle]);
197: writer.print("\t" + findAfterCommit2[cycle]);
198: writer.print("\t" + load[cycle]);
199: writer.print("\t" + load2[cycle]);
200: writer.print("\t" + loadR[cycle]);
201: writer.print("\t" + termInfo[cycle]);
202: writer.print("\t" + optimize[cycle]);
203: writer.println();
204: writer.flush();
205: }
206: writer
207: .println("Cycle\tTotal\tSave\tFind\tCommit\tFind\tFind\tLoad\tLoad\tLoadR\tTermInfo\tOptimize");
208: writer.print("AVG\t" + average(totalCycleTime));
209: writer.print("\t" + average(saveTime));
210: writer.print("\t" + average(findBeforeCommit));
211: writer.print("\t" + average(commitTime));
212: writer.print("\t" + average(findAfterCommit));
213: writer.print("\t" + average(findAfterCommit2));
214: writer.print("\t" + average(load));
215: writer.print("\t" + average(load2));
216: writer.print("\t" + average(loadR));
217: writer.print("\t" + average(termInfo));
218: writer.print("\t" + average(optimize));
219: writer.println();
220: writer.flush();
221: }
222:
223: private long average(long[] values) {
224: long count = 0;
225: for (int i = 0; i < values.length; i++) {
226: count += values[i];
227: }
228: return (long) (((float) count) / values.length);
229: }
230:
231: public int getNumberOfCycles() {
232: return numberOfCycles;
233: }
234:
235: public void setNumberOfCycles(int numberOfCycles) {
236: this .numberOfCycles = numberOfCycles;
237: }
238:
239: public long getNumberPerCycle() {
240: return numberPerCycle;
241: }
242:
243: public void setNumberPerCycle(long numberPerCycle) {
244: this .numberPerCycle = numberPerCycle;
245: }
246:
247: public static void main(String[] args) throws IOException {
248: LoadTester loadTester = new LoadTester();
249: if (args.length > 0) {
250: loadTester.setNumberOfCycles(Integer.parseInt(args[0]));
251: }
252: if (args.length > 1) {
253: loadTester.setNumberPerCycle(Long.parseLong(args[1]));
254: }
255: loadTester.runTests(System.out);
256: }
257: }
|