001: package org.apache.ojb.performance;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * 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: */
017:
018: /**
019: * Derivate this class to implement a test instance for the performance test.
020: *
021: * @version $Id: PerfRunner.java,v 1.1.2.1 2005/12/30 00:01:42 arminw Exp $
022: */
023: class PerfRunner {
024: private final String PREFIX_LOG = "[" + this .getClass().getName()
025: + "] ";
026:
027: /**
028: * testTimes[0] startTime/test length
029: * testTimes[1] inserting times
030: * testTimes[2] fetching times
031: * testTimes[3] fetching repeat times
032: * testTimes[4] get by Identity times
033: * testTimes[5] updating times
034: * testTimes[6] deleting times
035: */
036: private long[] testTimes;
037: private ThreadGroup threadGroup;
038: private PerfMain perfMain;
039: private long perfTestId;
040: private boolean checked;
041: /**
042: * The threads that are executing.
043: */
044: private Thread threads[] = null;
045: private Class testClass;
046: private PerfTest test;
047:
048: public PerfRunner(Class perfTestClass) {
049: this .perfTestId = System.currentTimeMillis();
050: this .checked = false;
051: this .testClass = perfTestClass;
052: // create a tmp test instance
053: this .test = createTest();
054: this .threadGroup = new ThreadGroup(testName() + "_Group");
055: }
056:
057: private PerfTest createTest() {
058: try {
059: PerfTest result = (PerfTest) testClass.newInstance();
060: result.setPerfRunner(this );
061: return result;
062: } catch (Exception e) {
063: e.printStackTrace();
064: throw new RuntimeException("Can't create test instance: "
065: + e.getMessage());
066: }
067: }
068:
069: /**
070: * Returns the name of the test
071: */
072: public String testName() {
073: return test.testName();
074: }
075:
076: private void checkApi() throws Exception {
077: String name = testName() + "_Pre_Test_Object";
078: PerfArticle article = test.getPreparedPerfArticle(name);
079: PerfArticle[] arr = new PerfArticle[] { article };
080: test.insertNewArticles(arr);
081: test.readArticlesByCursor(name);
082: test.updateArticles(arr);
083: test.deleteArticles(arr);
084: checked = true;
085: }
086:
087: /**
088: * Interrupt the running threads.
089: */
090: protected void interruptThreads() {
091: if (threads != null) {
092: for (int i = 0; i < threads.length; i++) {
093: threads[i].interrupt();
094: }
095: }
096: PerfMain.printer().println("## Test failed! ##");
097: PerfMain.printer().println("## Test failed! ##");
098: }
099:
100: /**
101: * Run the threads.
102: */
103: protected void runTestHandles(final PerfTest[] runnables) {
104: if (runnables == null) {
105: throw new IllegalArgumentException("runnables is null");
106: }
107: threads = new Thread[runnables.length];
108: for (int i = 0; i < threads.length; i++) {
109: threads[i] = new Thread(threadGroup, runnables[i]);
110: }
111: for (int i = 0; i < threads.length; i++) {
112: threads[i].start();
113: }
114: try {
115: for (int i = 0; i < threads.length; i++) {
116: threads[i].join();
117: }
118: } catch (InterruptedException ignore) {
119: PerfMain.printer().println(
120: PREFIX_LOG + "Thread join interrupted.");
121: }
122:
123: // should always be skipped, because we use 'thread.join'
124: while (threadGroup.activeCount() > 0) {
125: PerfMain.printer().println(
126: "## active threads: " + threadGroup.activeCount());
127: }
128:
129: threads = null;
130: }
131:
132: public void performTest() {
133: try {
134: // prepare tmp used test
135: test.init();
136:
137: if (!checked) {
138: checkApi();
139: //PerfMain.printer().println("# PerfTest: " + testName() + " # ");
140: }
141:
142: int objectCount;
143: int objectCountAfter;
144:
145: testTimes = new long[7];
146:
147: objectCount = test.articleCount();
148:
149: // now we start the test threads
150: PerfTest[] perfHandles = new PerfTest[PerfMain
151: .getConcurrentThreads()];
152: for (int i = 0; i < PerfMain.getConcurrentThreads(); i++) {
153: perfHandles[i] = createTest();
154: }
155: runTestHandles(perfHandles);
156:
157: // end of test threads
158: objectCountAfter = test.articleCount();
159: perfMain.addPeriodResult(testName(), testTimes);
160: perfMain.addConsistentResult(testName(), objectCount,
161: objectCountAfter);
162:
163: // tear down tmp used test
164: test.tearDown();
165: } catch (Exception e) {
166: e.printStackTrace();
167: perfMain.registerException(PREFIX_LOG, e);
168: }
169: }
170:
171: public void registerException(String causer, Exception e) {
172: perfMain.registerException(causer, e);
173: }
174:
175: public synchronized void addTime(short position, long time) {
176: testTimes[position] += time;
177: }
178:
179: public void registerPerfMain(PerfMain aPerfMain) {
180: this .perfMain = aPerfMain;
181: }
182:
183: public ThreadGroup getThreadGroup() {
184: return threadGroup;
185: }
186:
187: public long getPerfTestId() {
188: return perfTestId;
189: }
190: }
|