KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.performance;
2
3 /* Copyright 2002-2004 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  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
25  * @version $Id: PerfHandle.java,v 1.11 2004/04/05 12:16:22 tomdz Exp $
26  */

27 public abstract class PerfHandle implements Runnable JavaDoc
28 {
29     private String JavaDoc PREFIX_LOG = "[" + this.getClass().getName() + "] ";
30     private PerfTest test;
31     private String JavaDoc objectName;
32
33     public PerfHandle(PerfTest test)
34     {
35         this.test = test;
36     }
37
38     /**
39      * Init the test. do setup stuff here
40      */

41     public abstract void init() throws Exception JavaDoc;
42
43     /**
44      * Do clean up.
45      */

46     public abstract void tearDown() throws Exception JavaDoc;
47
48     /**
49      * Store the given articles to database. Do optimize
50      * performance.
51      */

52     public abstract void insertNewArticles(PerfArticle[] arr) throws Exception JavaDoc;
53
54     /**
55      * Store the given articles to database. Implement a really
56      * resource stressing way.
57      */

58     public abstract void insertNewArticlesStress(PerfArticle[] arr) throws Exception JavaDoc;
59
60     /**
61      * Read all stored articles from the database and return the
62      * result as collection of <code>PerfArticles</code>.
63      * Do optimize performance.
64      * @param articleName article name used for all {@link PerfArticle} created
65      * by this instance/thread. Use this name in your query to match all belonging articles
66      */

67     public abstract Collection JavaDoc readArticlesByCursor(String JavaDoc articleName) throws Exception JavaDoc;
68
69     /**
70      * Delete all given article from the database.
71      * Do optimize performance.
72      */

73     public abstract void deleteArticles(PerfArticle[] arr) throws Exception JavaDoc;
74
75     /**
76      * Delete all given article from the database in a really resource
77      * sressing way.
78      */

79     public abstract void deleteArticlesStress(PerfArticle[] arr) throws Exception JavaDoc;
80
81     /**
82      * Update the given articles. Do optimize
83      * performance.
84      */

85     public abstract void updateArticles(PerfArticle[] arr) throws Exception JavaDoc;
86
87     /**
88      * Update the given articles. Implement a really
89      * resource stressing way.
90      */

91     public abstract void updateArticlesStress(PerfArticle[] arr) throws Exception JavaDoc;
92
93     /**
94      * Called to get a new instance class of the {@link org.apache.ojb.performance.PerfArticle}
95      * interface, override this method if you need your own implementation
96      * (with default constructor) of the PerfArticle-Interface.
97      * <br/>
98      * By default this method returns a new instance of the
99      * {@link org.apache.ojb.performance.PerfArticleImpl} class.
100      *
101      */

102     public PerfArticle newPerfArticle()
103     {
104         return new PerfArticleImpl();
105     }
106
107     /**
108      * The returned name was used as 'articleName' for all
109      * created <code>PerfArticles</code> for this thread.
110      * This allows an easy build of the query statement
111      * to match the created {@link PerfArticle} for this
112      * instance/thread.
113      */

114     public String JavaDoc getTestObjectName()
115     {
116         if (objectName == null)
117             objectName = test.testName() + "_" +
118                     Thread.currentThread().toString() + "_" + test.getPerfTestId();
119         return objectName;
120     }
121
122     /**
123      * Factory method that creates an {@link org.apache.ojb.performance.PerfArticle}
124      * using the {@link org.apache.ojb.performance.PerfArticleImpl} class,
125      * override this method if you need your own implementation
126      * of the PerfArticle-Interface.
127      *
128      * @param articleName set the 'articleName'
129      * @return the created PerfArticle object
130      */

131     protected PerfArticle getPreparedPerfArticle(String JavaDoc articleName)
132     {
133         PerfArticle a = newPerfArticle();
134         a.setArticleName(articleName);
135         a.setMinimumStock(100);
136         a.setPrice(0.45);
137         a.setProductGroupId(1);
138         a.setStock(234);
139         a.setSupplierId(4);
140         a.setUnit("bottle");
141         return a;
142     }
143
144     /**
145      * Runnable implementation method.
146      */

147     public void run()
148     {
149         PerfArticle[] m_arr = new PerfArticle[PerfMain.getIterationsPerThread()];
150         for (int i = 0; i < PerfMain.getIterationsPerThread(); i++)
151         {
152             PerfArticle a = getPreparedPerfArticle(getTestObjectName());
153             m_arr[i] = a;
154         }
155
156         try
157         {
158             long period = 0;
159             init();
160
161             // insert objects
162
if (!PerfMain.isUseStressMode())
163             {
164                 period = System.currentTimeMillis();
165                 insertNewArticles(m_arr);
166                 period = System.currentTimeMillis() - period;
167                 test.addTime(1, period);
168             }
169             else
170             {
171                 period = System.currentTimeMillis();
172                 insertNewArticlesStress(m_arr);
173                 period = System.currentTimeMillis() - period;
174                 test.addTime(1, period);
175             }
176
177             // read objects
178
period = System.currentTimeMillis();
179             Collection JavaDoc col = readArticlesByCursor(objectName);
180             period = System.currentTimeMillis() - period;
181             try
182             {
183                 checkQueryResult(col, m_arr);
184             }
185             catch (Exception JavaDoc e)
186             {
187                 test.registerException(PREFIX_LOG + "(Something wrong with query result) ", e);
188             }
189             test.addTime(2, period);
190
191             // update objects
192
modifyPerfArticle(m_arr);
193             if (!PerfMain.isUseStressMode())
194             {
195                 period = System.currentTimeMillis();
196                 updateArticles(m_arr);
197                 period = System.currentTimeMillis() - period;
198                 test.addTime(3, period);
199             }
200             else
201             {
202                 period = System.currentTimeMillis();
203                 deleteArticlesStress(m_arr);
204                 period = System.currentTimeMillis() - period;
205                 test.addTime(3, period);
206             }
207
208             // delete objects
209
if (!PerfMain.isUseStressMode())
210             {
211                 period = System.currentTimeMillis();
212                 deleteArticles(m_arr);
213                 period = System.currentTimeMillis() - period;
214                 test.addTime(4, period);
215             }
216             else
217             {
218                 period = System.currentTimeMillis();
219                 deleteArticlesStress(m_arr);
220                 period = System.currentTimeMillis() - period;
221                 test.addTime(4, period);
222             }
223             m_arr = null;
224             tearDown();
225         }
226         catch (Exception JavaDoc e)
227         {
228             e.printStackTrace();
229             test.registerException(PREFIX_LOG + "(Unexpected behaviour) ", e);
230             test.interruptThreads();
231         }
232     }
233
234     private void modifyPerfArticle(PerfArticle[] m_arr)
235     {
236         PerfArticle article;
237         String JavaDoc prefix = "updated_";
238         for (int i = 0; i < m_arr.length; i++)
239         {
240             article = m_arr[i];
241             article.setArticleName(prefix + article.getArticleName());
242         }
243     }
244
245     private void checkQueryResult(Collection JavaDoc col, PerfArticle[] m_arr) throws Exception JavaDoc
246     {
247         if(col.size() > 0)
248         {
249             Iterator JavaDoc it = col.iterator();
250             Object JavaDoc obj = it.next();
251             if(!(obj instanceof PerfArticle))
252             {
253                 throw new Exception JavaDoc("Wrong object type found. Expected instance of"+
254                     PerfArticle.class.getName() + ", found " + obj.getClass().getName());
255             }
256         }
257         if (col.size() != m_arr.length)
258         {
259             throw new Exception JavaDoc("Read objects: Wrong number of objects found. Expected " +
260                     (m_arr.length) + ", found " + col.size());
261         }
262     }
263 }
264
Popular Tags