KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > compare > PerformanceBaseTest


1 package org.apache.ojb.compare;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.Statement JavaDoc;
6 import java.sql.SQLException JavaDoc;
7
8 import org.apache.ojb.broker.metadata.ClassDescriptor;
9 import org.apache.ojb.broker.util.logging.Logger;
10 import org.apache.ojb.broker.util.logging.LoggerFactory;
11 import org.apache.ojb.broker.platforms.PlatformHsqldbImpl;
12 import org.apache.ojb.broker.platforms.Platform;
13 import org.apache.ojb.broker.query.Query;
14 import org.apache.ojb.broker.query.QueryFactory;
15 import org.apache.ojb.broker.HsqldbShutdown;
16 import org.apache.ojb.junit.PBTestCase;
17
18 /**
19  * This is the base class for single-threaded performance benchmarks.
20  *
21  * @author Thomas Mahler
22  */

23 abstract class PerformanceBaseTest extends PBTestCase
24 {
25     protected Logger logger = LoggerFactory.getLogger("performance");
26
27     private String JavaDoc nameOfTest = "Test performance";
28
29     /**
30      * the number of PerformanceArticle objects to work with.
31      */

32     protected static int articleCount = 500;
33
34     /**
35      * the number of iterations to perform.
36      */

37     protected static int iterations = 2;
38
39     /**
40      * the offset value for PerformanceArticle primary keys
41      */

42     protected final static int offsetId = 12000;
43
44     protected PerformanceArticle[] arr;
45
46     /**
47      * BrokerTests constructor comment.
48      *
49      * @param name java.lang.String
50      */

51     public PerformanceBaseTest(String JavaDoc name)
52     {
53         super(name);
54         // setNameOfTest("No name");
55
}
56
57
58     /**
59      * setting up the test fixture.
60      */

61     public void setUp() throws Exception JavaDoc
62     {
63         try
64         {
65             super.setUp();
66             clearTable();
67
68             arr = new PerformanceArticle[articleCount];
69             for(int i = 0; i < articleCount; i++)
70             {
71                 PerformanceArticle a = createArticle(offsetId + i);
72                 arr[i] = a;
73             }
74         }
75         catch(Exception JavaDoc e)
76         {
77             e.printStackTrace();
78         }
79     }
80
81     /**
82      * tearing down the test fixture.
83      */

84     public void tearDown() throws Exception JavaDoc
85     {
86         try
87         {
88             clearTable();
89             shutdown();
90             super.tearDown();
91         }
92         catch(Exception JavaDoc e)
93         {
94             e.printStackTrace();
95         }
96     }
97
98     /**
99      * Set the name of the test.
100      *
101      * @param nameOfTest
102      */

103     public void setNameOfTest(String JavaDoc nameOfTest)
104     {
105         this.nameOfTest = nameOfTest;
106     }
107
108     protected void clearTable() throws Exception JavaDoc
109     {
110         Connection JavaDoc conn = getConnection();
111         ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class);
112         String JavaDoc table = cld.getFullTableName();
113         String JavaDoc column = cld.getFieldDescriptorByName("articleId").getColumnName();
114         String JavaDoc sql = "DELETE FROM " + table + " WHERE " + column + " >= " + offsetId;
115         PreparedStatement JavaDoc stmt = conn.prepareStatement(sql);
116         stmt.execute();
117         returnConnection(conn);
118     }
119
120     /**
121      * factory method that createa an PerformanceArticle with a given id.
122      *
123      * @param id the primary key value for the new object
124      * @return the created PerformanceArticle object
125      */

126     protected PerformanceArticle createArticle(int id)
127     {
128         PerformanceArticle a = new PerformanceArticle();
129         a.setArticleId(new Integer JavaDoc(id));
130         a.setArticleName("New Performance Article " + id);
131         a.setMinimumStock(100);
132         a.setOrderedUnits(17);
133         a.setPrice(100.0);
134         a.setProductGroupId(1);
135         a.setStock(234);
136         a.setSupplierId(4);
137         a.setUnit("bottle");
138         return a;
139     }
140
141     /**
142      * obtain a JDBC Connection. OJB API is used to make this code portable for
143      * other target dabases and different lookup methods.
144      *
145      * @return the Connection to be used
146      */

147     protected Connection JavaDoc getConnection() throws Exception JavaDoc
148     {
149         // use the PB instance to get access to connection, this doesn't impact performance
150
Connection JavaDoc conn = broker.serviceConnectionManager().getConnection();
151         return conn;
152     }
153
154     protected void returnConnection(Connection JavaDoc conn) throws Exception JavaDoc
155     {
156
157     }
158
159     /**
160      * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
161      */

162     protected abstract void deleteArticles() throws Exception JavaDoc;
163
164     /**
165      * create new PerformanceArticle objects and insert them into the RDBMS.
166      * The number of objects to create is defined by <code>articleCount</code>.
167      */

168     protected abstract void insertNewArticles() throws Exception JavaDoc;
169
170     /**
171      * read in all the PerformanceArticles from the RDBMS that have
172      * been inserted by <code>insertNewArticles()</code>.
173      * The lookup is done one by one, that is: a primary key based lookup is used.
174      */

175     protected abstract void readArticles() throws Exception JavaDoc;
176
177     /**
178      * read in all the PerformanceArticles from the RDBMS that have
179      * been inserted by <code>insertNewArticles()</code>.
180      * The lookup is done with a cursor fetch,
181      * that is: a between Statement is used to select all inserted PerformanceArticles
182      * and Objects are read in by fetching from the cursor (JDBC ResultSet).
183      */

184     protected abstract void readArticlesByCursor() throws Exception JavaDoc;
185
186     /**
187      * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
188      * All objects are modified and changes are written to the RDBMS with an UPDATE.
189      */

190     protected abstract void updateExistingArticles() throws Exception JavaDoc;
191
192     /**
193      * This method is the driver for the complete Benchmark.
194      * It performs the following steps:
195      * <p/>
196      * 1.) n objects are created and inserted to the RDBMS.
197      * 2.) the created objects are modified. Modifications are written to the RDBMS with updates.
198      * 3.) All objects created in 1.) are read in by primary key based SELECT statements.
199      * 4.) Step 3.) is repeated to test caching facilities.
200      * 5.) All objects created in 1.) are read by iterating over a ResultSet.
201      * 6.) All objects created in 1.) are deleted with n separate DELETE Statements.
202      */

203     public void testBenchmark() throws Exception JavaDoc
204     {
205         try
206         {
207             logger.info(nameOfTest);
208             for(int i = 0; i < iterations; i++)
209             {
210                 logger.info("");
211
212                 // store all Article objects
213
insertNewArticles();
214
215                 // update all objects
216
updateExistingArticles();
217
218                 // querying
219
readArticles();
220
221                 readArticles();
222
223                 // fetching objects
224
readArticlesByCursor();
225
226                 // delete all objects
227
deleteArticles();
228             }
229         }
230         catch(Exception JavaDoc e)
231         {
232             logger.error(e);
233             throw e;
234         }
235     }
236
237     public void shutdown()
238     {
239         Platform platform = broker.serviceConnectionManager().getSupportedPlatform();
240
241         if(platform instanceof PlatformHsqldbImpl)
242         {
243             Connection JavaDoc con = null;
244             Statement JavaDoc stmt = null;
245
246             try
247             {
248                 con = broker.serviceConnectionManager().getConnection();
249                 stmt = con.createStatement();
250                 stmt.execute("shutdown");
251             }
252             catch (Exception JavaDoc e)
253             {
254                 e.printStackTrace();
255             }
256             finally
257             {
258                 try
259                 {
260                     if(con != null) con.close();
261                     if(stmt != null) stmt.close();
262
263                 }
264                 catch (SQLException JavaDoc e1)
265                 {
266                     e1.printStackTrace();
267                 }
268             }
269         }
270
271     }
272 }
273
Popular Tags