KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > performance > PerfRunner


1 package org.apache.ojb.performance;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
19 /**
20  * Derivate this class to implement a test instance for the performance test.
21  *
22  * @version $Id: PerfRunner.java,v 1.1.2.1 2005/12/30 00:01:42 arminw Exp $
23  */

24 class PerfRunner
25 {
26     private final String JavaDoc PREFIX_LOG = "[" + this.getClass().getName() + "] ";
27
28     /**
29      * testTimes[0] startTime/test length
30      * testTimes[1] inserting times
31      * testTimes[2] fetching times
32      * testTimes[3] fetching repeat times
33      * testTimes[4] get by Identity times
34      * testTimes[5] updating times
35      * testTimes[6] deleting times
36      */

37     private long[] testTimes;
38     private ThreadGroup JavaDoc threadGroup;
39     private PerfMain perfMain;
40     private long perfTestId;
41     private boolean checked;
42     /**
43      * The threads that are executing.
44      */

45     private Thread JavaDoc threads[] = null;
46     private Class JavaDoc testClass;
47     private PerfTest test;
48
49
50     public PerfRunner(Class JavaDoc perfTestClass)
51     {
52         this.perfTestId = System.currentTimeMillis();
53         this.checked = false;
54         this.testClass = perfTestClass;
55         // create a tmp test instance
56
this.test = createTest();
57         this.threadGroup = new ThreadGroup JavaDoc(testName() + "_Group");
58     }
59
60     private PerfTest createTest()
61     {
62         try
63         {
64             PerfTest result = (PerfTest) testClass.newInstance();
65             result.setPerfRunner(this);
66             return result;
67         }
68         catch(Exception JavaDoc e)
69         {
70             e.printStackTrace();
71             throw new RuntimeException JavaDoc("Can't create test instance: " + e.getMessage());
72         }
73     }
74
75     /**
76      * Returns the name of the test
77      */

78     public String JavaDoc testName()
79     {
80         return test.testName();
81     }
82
83     private void checkApi() throws Exception JavaDoc
84     {
85         String JavaDoc name = testName() + "_Pre_Test_Object";
86         PerfArticle article = test.getPreparedPerfArticle(name);
87         PerfArticle[] arr = new PerfArticle[]{article};
88         test.insertNewArticles(arr);
89         test.readArticlesByCursor(name);
90         test.updateArticles(arr);
91         test.deleteArticles(arr);
92         checked = true;
93     }
94
95     /**
96      * Interrupt the running threads.
97      */

98     protected void interruptThreads()
99     {
100         if (threads != null)
101         {
102             for (int i = 0; i < threads.length; i++)
103             {
104                 threads[i].interrupt();
105             }
106         }
107         PerfMain.printer().println("## Test failed! ##");
108         PerfMain.printer().println("## Test failed! ##");
109     }
110
111     /**
112      * Run the threads.
113      */

114     protected void runTestHandles(final PerfTest[] runnables)
115     {
116         if (runnables == null)
117         {
118             throw new IllegalArgumentException JavaDoc("runnables is null");
119         }
120         threads = new Thread JavaDoc[runnables.length];
121         for (int i = 0; i < threads.length; i++)
122         {
123             threads[i] = new Thread JavaDoc(threadGroup, runnables[i]);
124         }
125         for (int i = 0; i < threads.length; i++)
126         {
127             threads[i].start();
128         }
129         try
130         {
131             for (int i = 0; i < threads.length; i++)
132             {
133                 threads[i].join();
134             }
135         }
136         catch (InterruptedException JavaDoc ignore)
137         {
138             PerfMain.printer().println(PREFIX_LOG + "Thread join interrupted.");
139         }
140
141         // should always be skipped, because we use 'thread.join'
142
while(threadGroup.activeCount() > 0)
143         {
144             PerfMain.printer().println("## active threads: " + threadGroup.activeCount());
145         }
146
147         threads = null;
148     }
149
150     public void performTest()
151     {
152         try
153         {
154             // prepare tmp used test
155
test.init();
156
157             if (!checked)
158             {
159                 checkApi();
160                 //PerfMain.printer().println("# PerfTest: " + testName() + " # ");
161
}
162
163             int objectCount;
164             int objectCountAfter;
165
166             testTimes = new long[7];
167
168             objectCount = test.articleCount();
169
170             // now we start the test threads
171
PerfTest[] perfHandles = new PerfTest[PerfMain.getConcurrentThreads()];
172             for (int i = 0; i < PerfMain.getConcurrentThreads(); i++)
173             {
174                 perfHandles[i] = createTest();
175             }
176             runTestHandles(perfHandles);
177             
178             // end of test threads
179
objectCountAfter = test.articleCount();
180             perfMain.addPeriodResult(testName(), testTimes);
181             perfMain.addConsistentResult(testName(), objectCount, objectCountAfter);
182
183             // tear down tmp used test
184
test.tearDown();
185         }
186         catch (Exception JavaDoc e)
187         {
188             e.printStackTrace();
189             perfMain.registerException(PREFIX_LOG, e);
190         }
191     }
192
193     public void registerException(String JavaDoc causer, Exception JavaDoc e)
194     {
195         perfMain.registerException(causer, e);
196     }
197
198     public synchronized void addTime(short position, long time)
199     {
200         testTimes[position] += time;
201     }
202
203     public void registerPerfMain(PerfMain aPerfMain)
204     {
205         this.perfMain = aPerfMain;
206     }
207
208     public ThreadGroup JavaDoc getThreadGroup()
209     {
210         return threadGroup;
211     }
212
213     public long getPerfTestId()
214     {
215         return perfTestId;
216     }
217 }
218
Popular Tags