KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 /**
22  * Derivate this class to implement a test client for the performance test.
23  *
24  * @version $Id: PerfTest.java,v 1.9.2.5 2005/12/30 00:01:42 arminw Exp $
25  */

26 public abstract class PerfTest implements Runnable JavaDoc
27 {
28     private String JavaDoc PREFIX_LOG = "[" + this.getClass().getName() + "] ";
29     private PerfRunner test;
30     private String JavaDoc objectName;
31
32     public PerfTest()
33     {
34     }
35
36     /**
37      * Returns the name of the test
38      */

39     public abstract String JavaDoc testName();
40
41     /**
42      * Returns the count of all found {@link PerfArticle}
43      * in database.
44      * This method is not involved in the performance test
45      * methods, thus it's not mandatory to use the api-methods
46      * for implementation.
47      */

48     public abstract int articleCount();
49
50     /**
51      * Init the test. do setup stuff here
52      */

53     public abstract void init() throws Exception JavaDoc;
54
55     /**
56      * Do clean up.
57      */

58     public abstract void tearDown() throws Exception JavaDoc;
59
60     /**
61      * Store the given articles to database. Do optimize
62      * performance.
63      */

64     public abstract void insertNewArticles(PerfArticle[] arr) throws Exception JavaDoc;
65
66     /**
67      * Store the given articles to database. Implement a really
68      * resource stressing way.
69      */

70     public abstract void insertNewArticlesStress(PerfArticle[] arr) throws Exception JavaDoc;
71
72     /**
73      * Read all stored articles from the database and return the
74      * result as collection of <code>PerfArticles</code>.
75      * Do optimize performance.
76      * @param articleName article name used for all {@link PerfArticle} created
77      * by this instance/thread. Use this name in your query to match all belonging articles
78      */

79     public abstract Collection JavaDoc readArticlesByCursor(String JavaDoc articleName) throws Exception JavaDoc;
80
81     /**
82      * Read all stored articles from the database and return the
83      * result as collection of <code>PerfArticles</code>.
84      * Do optimize performance.
85      * @param articleId the primary key of a {@link PerfArticle} instance
86      * @return The matching {@link PerfArticle} instance or <em>null</em> if not found.
87      */

88     public abstract PerfArticle getArticleByIdentity(Long JavaDoc articleId) throws Exception JavaDoc;
89
90     /**
91      * Delete all given article from the database.
92      * Do optimize performance.
93      */

94     public abstract void deleteArticles(PerfArticle[] arr) throws Exception JavaDoc;
95
96     /**
97      * Delete all given article from the database in a really resource
98      * sressing way.
99      */

100     public abstract void deleteArticlesStress(PerfArticle[] arr) throws Exception JavaDoc;
101
102     /**
103      * Update the given articles. Do optimize
104      * performance.
105      */

106     public abstract void updateArticles(PerfArticle[] arr) throws Exception JavaDoc;
107
108     /**
109      * Update the given articles. Implement a really
110      * resource stressing way.
111      */

112     public abstract void updateArticlesStress(PerfArticle[] arr) throws Exception JavaDoc;
113
114     /**
115      * Called to get a new instance class of the {@link org.apache.ojb.performance.PerfArticle}
116      * interface, override this method if you need your own implementation
117      * (with default constructor) of the PerfArticle-Interface.
118      * <br/>
119      * By default this method returns a new instance of the
120      * {@link org.apache.ojb.performance.PerfArticleImpl} class.
121      *
122      */

123     public PerfArticle newPerfArticle()
124     {
125         return new PerfArticleImpl();
126     }
127
128     /**
129      * The returned name was used as 'articleName' for all
130      * created <code>PerfArticles</code> for this thread.
131      * This allows an easy build of the query statement
132      * to match the created {@link PerfArticle} for this
133      * instance/thread.
134      */

135     public String JavaDoc getTestObjectName()
136     {
137         if (objectName == null)
138             objectName = testName() + "_" +
139                     Thread.currentThread().toString() + "_" + test.getPerfTestId();
140         return objectName;
141     }
142
143     /**
144      * Factory method that creates an {@link org.apache.ojb.performance.PerfArticle}
145      * using the {@link org.apache.ojb.performance.PerfArticleImpl} class,
146      * override this method if you need your own implementation
147      * of the PerfArticle-Interface.
148      *
149      * @param articleName set the 'articleName'
150      * @return the created PerfArticle object
151      */

152     public PerfArticle getPreparedPerfArticle(String JavaDoc articleName)
153     {
154         PerfArticle a = newPerfArticle();
155         a.setArticleName(articleName);
156         a.setMinimumStock(100);
157         a.setPrice(0.45);
158         a.setProductGroupId(1);
159         a.setStock(234);
160         a.setSupplierId(4);
161         a.setUnit("bottle");
162         return a;
163     }
164
165     void setPerfRunner(PerfRunner test)
166     {
167         this.test = test;
168     }
169
170     /**
171      * Runnable implementation method.
172      */

173     public void run()
174     {
175         PerfArticle[] m_arr = new PerfArticle[PerfMain.getIterationsPerThread()];
176         for (int i = 0; i < PerfMain.getIterationsPerThread(); i++)
177         {
178             m_arr[i] = getPreparedPerfArticle(getTestObjectName());
179         }
180
181         try
182         {
183             long totalTime = 0;
184             long period;
185             init();
186
187             // insert objects
188
if (PerfMain.isUseStressMode())
189             {
190                 period = System.currentTimeMillis();
191                 insertNewArticlesStress(m_arr);
192                 period = System.currentTimeMillis() - period;
193                 test.addTime(PerfMain.TIME_INSERT, period);
194                 totalTime+=period;
195             }
196             else
197             {
198                 period = System.currentTimeMillis();
199                 insertNewArticles(m_arr);
200                 period = System.currentTimeMillis() - period;
201                 test.addTime(PerfMain.TIME_INSERT, period);
202                 totalTime+=period;
203                 // System.out.println("I=" + period);
204
}
205             checkInsertResult(m_arr);
206
207             // read objects
208
period = System.currentTimeMillis();
209             Collection JavaDoc col = readArticlesByCursor(objectName);
210             period = System.currentTimeMillis() - period;
211             try
212             {
213                 checkQueryResult(col, m_arr);
214             }
215             catch (Exception JavaDoc e)
216             {
217                 test.registerException(PREFIX_LOG
218                         + "(Something wrong with query result or with object insert operation) ", e);
219             }
220             test.addTime(PerfMain.TIME_FETCH, period);
221             totalTime+=period;
222             // System.out.println("R=" + period);
223

224
225             // read objects 2
226
period = System.currentTimeMillis();
227             col = readArticlesByCursor(objectName);
228             period = System.currentTimeMillis() - period;
229             try
230             {
231                 checkQueryResult(col, m_arr);
232             }
233             catch (Exception JavaDoc e)
234             {
235                 test.registerException(PREFIX_LOG
236                         + "(Something wrong with query result or with object insert operation) ", e);
237             }
238             test.addTime(PerfMain.TIME_FETCH_2, period);
239             totalTime+=period;
240             // System.out.println("R2=" + period);
241

242
243             // get by Identity
244
period = System.currentTimeMillis();
245             PerfArticle result;
246             for(int i = 0; i < m_arr.length; i++)
247             {
248                 if(i%4==0)
249                 {
250                     PerfArticle perfArticle = m_arr[i];
251                     result = getArticleByIdentity(perfArticle.getArticleId());
252                     if(result == null)
253                     {
254                         test.registerException("Unexpected result: Get by Identity is 'null' for "
255                                 + PerfArticle.class.getName() + " with primary key "
256                                 + perfArticle.getArticleId(), null);
257                     }
258                 }
259             }
260             period = (System.currentTimeMillis() - period);
261             test.addTime(PerfMain.TIME_BY_IDENTITY, period);
262             totalTime+=period;
263             // System.out.println("B=" + period);
264

265
266             // update objects
267
modifyPerfArticle(m_arr);
268             if (PerfMain.isUseStressMode())
269             {
270                 period = System.currentTimeMillis();
271                 updateArticlesStress(m_arr);
272                 period = System.currentTimeMillis() - period;
273                 test.addTime(PerfMain.TIME_UPDATE, period);
274                 totalTime+=period;
275             }
276             else
277             {
278                 period = System.currentTimeMillis();
279                 updateArticles(m_arr);
280                 period = System.currentTimeMillis() - period;
281                 test.addTime(PerfMain.TIME_UPDATE, period);
282                 totalTime+=period;
283                 // System.out.println("U=" + period);
284
}
285
286             // delete objects
287
if (PerfMain.isUseStressMode())
288             {
289                 period = System.currentTimeMillis();
290                 deleteArticlesStress(m_arr);
291                 period = System.currentTimeMillis() - period;
292                 test.addTime(PerfMain.TIME_DELETE, period);
293                 totalTime+=period;
294             }
295             else
296             {
297                 period = System.currentTimeMillis();
298                 deleteArticles(m_arr);
299                 period = System.currentTimeMillis() - period;
300                 test.addTime(PerfMain.TIME_DELETE, period);
301                 totalTime+=period;
302                 // System.out.println("D=" + period);
303
}
304             test.addTime(PerfMain.TIME_TOTAL, totalTime);
305             tearDown();
306         }
307         catch (Exception JavaDoc e)
308         {
309             e.printStackTrace();
310             test.registerException(PREFIX_LOG + "(Unexpected behaviour) ", e);
311             test.interruptThreads();
312         }
313     }
314
315     private void modifyPerfArticle(PerfArticle[] m_arr)
316     {
317         PerfArticle article;
318         String JavaDoc prefix = "updated_";
319         for (int i = 0; i < m_arr.length; i++)
320         {
321             article = m_arr[i];
322             article.setArticleName(prefix + article.getArticleName());
323         }
324     }
325
326     private void checkQueryResult(Collection JavaDoc col, PerfArticle[] m_arr) throws Exception JavaDoc
327     {
328         if(col.size() > 0)
329         {
330             Iterator JavaDoc it = col.iterator();
331             Object JavaDoc obj = it.next();
332             if(!(obj instanceof PerfArticle))
333             {
334                 throw new Exception JavaDoc("Wrong object type found. Expected instance of"+
335                     PerfArticle.class.getName() + ", found " + obj.getClass().getName());
336             }
337         }
338         if (col.size() != m_arr.length)
339         {
340             throw new Exception JavaDoc("Read objects: Wrong number of objects found. Expected " +
341                     (m_arr.length) + ", found " + col.size());
342         }
343     }
344
345     private void checkInsertResult(PerfArticle[] m_arr) throws Exception JavaDoc
346     {
347         for(int i = 0; i < m_arr.length; i++)
348         {
349             PerfArticle perfArticle = m_arr[i];
350             if(perfArticle.getArticleId() == null)
351             {
352                 throw new Exception JavaDoc("Insert objects: Object with 'null' PK found");
353             }
354         }
355     }
356 }
357
Popular Tags