KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > ejb > odmg > StressTest


1 package org.apache.ojb.ejb.odmg;
2
3 /* Copyright 2004-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 import javax.ejb.EJBHome JavaDoc;
19 import javax.naming.Context JavaDoc;
20 import javax.rmi.PortableRemoteObject JavaDoc;
21 import java.math.BigDecimal JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.commons.lang.SystemUtils;
27 import org.apache.ojb.broker.OJBRuntimeException;
28 import org.apache.ojb.ejb.ArticleVO;
29 import org.apache.ojb.ejb.ContextHelper;
30
31 /**
32  * stress test against one database using several threads
33  *
34  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>.
35  * @version $Id: StressTest.java,v 1.5.2.1 2005/12/21 22:21:39 tomdz Exp $
36  */

37 public class StressTest
38 {
39     private static int iterationsPerThread = 50;
40     private static int concurrentThreads = 2;
41     private ODMGSessionRemote bean;
42     private static boolean testClientPass = true;
43
44     /**
45      * times[0] startTime/test length
46      * times[1] inserting times
47      * times[2] fetching times
48      * times[3] deleting times
49      */

50     private long[] times;
51
52     /**
53      * The threads that are executing.
54      */

55     private Thread JavaDoc threads[] = null;
56
57     public StressTest()
58     {
59     }
60
61     public static void performTest(int[] args) throws Exception JavaDoc
62     {
63         if (args.length > 0)
64         {
65             concurrentThreads = args[0];
66         }
67         if (args.length > 1)
68         {
69             iterationsPerThread = args[1];
70         }
71
72         StressTest test = new StressTest();
73         int objectCount = 0;
74         int objectCountAfter = 0;
75         test.init();
76         objectCount = test.getArticleCount();
77         test.runMultithreaded();
78
79         System.out.println("Test-Info: Objects in DB before ODMG test: " + objectCount);
80         objectCountAfter = test.getArticleCount();
81         System.out.println("Test-Info: Objects in DB after ODMG test: " + objectCountAfter);
82         System.out.println("Test-Info: Stress test was successful? - " + (objectCount == objectCountAfter && testClientPass) + " -");
83         if(!testClientPass) throw new RuntimeException JavaDoc("Test does not pass");
84     }
85
86     /**
87      * Setting up the test fixture.
88      */

89     private void init() throws Exception JavaDoc
90     {
91         Context JavaDoc ctx = ContextHelper.getContext();
92         times = new long[4];
93         try
94         {
95             Object JavaDoc object = PortableRemoteObject.narrow(ctx.lookup(ODMGSessionHome.JNDI_NAME), EJBHome JavaDoc.class);
96             bean = ((ODMGSessionHome) object).create();
97         }
98         catch(Exception JavaDoc e)
99         {
100             e.printStackTrace(System.err);
101             throw e;
102         }
103     }
104
105     private int getArticleCount() throws Exception JavaDoc
106     {
107         try
108         {
109             return bean.getArticleCount();
110         }
111         catch(java.rmi.RemoteException JavaDoc e)
112         {
113             e.printStackTrace();
114             throw e;
115         }
116     }
117
118     private void runMultithreaded() throws Exception JavaDoc
119     {
120         String JavaDoc sep = SystemUtils.LINE_SEPARATOR;
121
122         System.out.println(sep + sep + "++ Start thread generation for ODMG api test ++");
123         System.out.println("Begin with performance test, " + concurrentThreads +
124                 " concurrent threads, handle " + iterationsPerThread + " articles per thread");
125         ODMGTestClient[] clientsODMG = new ODMGTestClient[concurrentThreads];
126         for (int i = 0; i < concurrentThreads; i++)
127         {
128             ODMGTestClient obj = new ODMGTestClient(this);
129             clientsODMG[i] = obj;
130         }
131         System.out.println("");
132         times[0] = System.currentTimeMillis();
133         runTestClients(clientsODMG);
134         times[0] = (System.currentTimeMillis() - times[0]);
135         System.out.println(buildTestSummary("ODMG API"));
136         System.out.println("++ End of performance test ODMG api ++" + sep + sep);
137     }
138
139     /**
140      * Interrupt the running threads.
141      */

142     synchronized void interruptThreads()
143     {
144         testClientPass = false;
145         if (threads != null)
146         {
147             for (int i = 0; i < threads.length; i++)
148             {
149                 threads[i].interrupt();
150             }
151         }
152         System.err.println("## Test failed! ##");
153         System.err.println("## Test failed! ##");
154     }
155
156     /**
157      * Run the threads.
158      */

159     void runTestClients(final TestClient[] runnables) throws Exception JavaDoc
160     {
161         if (runnables == null)
162         {
163             throw new IllegalArgumentException JavaDoc("runnables is null");
164         }
165         threads = new Thread JavaDoc[runnables.length];
166         for (int i = 0; i < threads.length; i++)
167         {
168             threads[i] = new Thread JavaDoc(runnables[i]);
169         }
170         for (int i = 0; i < threads.length; i++)
171         {
172             threads[i].start();
173             threads[i].join();
174         }
175         threads = null;
176     }
177
178     synchronized void addTime(int position, long time)
179     {
180         times[position] = times[position] + time;
181     }
182
183     private String JavaDoc buildTestSummary(String JavaDoc key)
184     {
185         String JavaDoc sep = System.getProperty("line.separator");
186         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
187         buf.append(sep);
188         buf.append("----------------------------------------------------");
189         buf.append(sep);
190         buf.append("TEST SUMMARY - " + key);
191         buf.append(sep);
192         buf.append(concurrentThreads + " concurrent threads, handle " + iterationsPerThread + " articles per thread");
193         buf.append(sep);
194         buf.append("Test period: " + (((double) times[0]) / 1000) + " [sec]");
195         buf.append(sep);
196         buf.append("Inserting period: " + times[1] + " [msec]");
197         buf.append(sep);
198         buf.append("Fetching period: " + times[2] + " [msec]");
199         buf.append(sep);
200         buf.append("Deleting period: " + times[3] + " [msec]");
201         buf.append(sep);
202         buf.append("----------------------------------------------------");
203
204         return buf.toString();
205     }
206
207
208
209     //*********************************************************************************
210
// inner classes
211
//*********************************************************************************
212

213     static abstract class TestClient implements Runnable JavaDoc
214     {
215
216     }
217
218 /**
219      * ODMG-api test class
220      */

221     static class ODMGTestClient extends TestClient
222     {
223         private List JavaDoc articlesList;
224         private ODMGSessionRemote bean;
225         private String JavaDoc articleName;
226         private StressTest test;
227
228         public ODMGTestClient(StressTest test)
229         {
230             this.test = test;
231             init();
232             articlesList = new ArrayList JavaDoc();
233             articleName = "ODMGTestClient_" + System.currentTimeMillis();
234             for(int i = 0; i < iterationsPerThread; i++)
235             {
236                 articlesList.add(createArticle(articleName));
237             }
238         }
239
240         protected void init()
241         {
242             Context JavaDoc ctx = ContextHelper.getContext();
243             try
244             {
245                 Object JavaDoc object = PortableRemoteObject.narrow(ctx.lookup(ODMGSessionHome.JNDI_NAME), EJBHome JavaDoc.class);
246                 bean = ((ODMGSessionHome) object).create();
247             }
248             catch(Exception JavaDoc e)
249             {
250                 e.printStackTrace();
251                 throw new OJBRuntimeException("Can't lookup bean: " + ODMGSessionHome.JNDI_NAME, e);
252             }
253         }
254
255         public void run()
256         {
257             //log.info("Thread "+this+" run");
258
try
259             {
260                 insertNewArticles();
261                 readAllArticles();
262                 deleteArticles();
263             }
264             catch(Throwable JavaDoc e)
265             {
266                 System.err.println("Error in client: " + e.getMessage());
267                 test.interruptThreads();
268                 throw new OJBRuntimeException("[" + ODMGTestClient.class.getName()
269                         + "] Stress test client cause exception, thread was " + Thread.currentThread(), e);
270             }
271         }
272
273         /**
274          * factory method that createa an ArticleVO
275          * @return the created ArticleVO object
276          */

277         private ArticleVO createArticle(String JavaDoc articleName)
278         {
279             ArticleVO a = new ArticleVO();
280             a.setName(articleName);
281             a.setPrice(new BigDecimal JavaDoc(0.45 * articleName.hashCode()));
282             a.setDescription("description " + articleName.hashCode());
283             return a;
284         }
285
286         protected void deleteArticles() throws Exception JavaDoc
287         {
288             long start = System.currentTimeMillis();
289
290             bean.deleteObjects(articlesList);
291
292             long stop = System.currentTimeMillis();
293             test.addTime(3, stop - start);
294         }
295
296         protected void insertNewArticles() throws Exception JavaDoc
297         {
298             long start = System.currentTimeMillis();
299
300             articlesList = bean.storeObjects(articlesList);
301
302             long stop = System.currentTimeMillis();
303             test.addTime(1, stop - start);
304         }
305
306         protected void readAllArticles() throws Exception JavaDoc
307         {
308             long start = System.currentTimeMillis();
309
310             Iterator JavaDoc it = bean.getArticlesByName(articleName).iterator();
311             while(it.hasNext()) it.next();
312
313             long stop = System.currentTimeMillis();
314             test.addTime(2, stop - start);
315         }
316     }
317 }
318
Popular Tags