KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.compare;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6
7 import org.apache.ojb.broker.metadata.ClassDescriptor;
8 import org.apache.ojb.broker.query.Criteria;
9 import org.apache.ojb.broker.query.Query;
10 import org.apache.ojb.broker.query.QueryByCriteria;
11
12 /**
13  * This TestCase contains the OJB single-threaded performance benchmarks for the
14  * JDBC API. This is the reference for other benchmarks.
15  *
16  * @author Thomas Mahler
17  */

18 public class PerformanceJdbcTest extends PerformanceBaseTest
19 {
20     /**
21      * BrokerTests constructor comment.
22      *
23      * @param name java.lang.String
24      */

25     public PerformanceJdbcTest(String JavaDoc name)
26     {
27         super(name);
28         setNameOfTest("Test for JDBC");
29     }
30
31     /**
32      * launches the TestCase.
33      * The number of Objects to work with and the number of iterations
34      * to be performed can be adjusted by setting them as commandline parameters.
35      *
36      * @param args the String[] holding the commandline parameters.
37      */

38     public static void main(String JavaDoc[] args)
39     {
40         if(args.length > 0)
41         {
42             articleCount = Integer.parseInt(args[0]);
43         }
44         if(args.length > 1)
45         {
46             iterations = Integer.parseInt(args[1]);
47         }
48
49         String JavaDoc[] arr = {PerformanceJdbcTest.class.getName()};
50         junit.textui.TestRunner.main(arr);
51     }
52
53     public void testBenchmark() throws Exception JavaDoc
54     {
55         super.testBenchmark();
56     }
57
58     /**
59      * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
60      */

61     protected void deleteArticles() throws Exception JavaDoc
62     {
63         Connection JavaDoc conn = getConnection();
64
65         // Use the OJB SqlGenerator to generate SQL Statements. All details about
66
// Table and column names are read from the repository.xml file.
67
ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class);
68         String JavaDoc sql = broker.serviceSqlGenerator().getPreparedDeleteStatement(cld).getStatement();
69
70         logger.debug("delete stmt: " + sql);
71
72         long start = System.currentTimeMillis();
73         try
74         {
75             conn.setAutoCommit(false);
76             PreparedStatement JavaDoc stmt = conn.prepareStatement(sql);
77             for(int i = 0; i < articleCount; i++)
78             {
79                 PerformanceArticle a = arr[i];
80                 stmt.setInt(1, a.articleId.intValue());
81                 stmt.execute();
82             }
83             conn.commit();
84         }
85         catch(Throwable JavaDoc t)
86         {
87             logger.error(t);
88             fail(t.getMessage());
89         }
90         finally
91         {
92             if(conn != null)
93                 returnConnection(conn);
94         }
95         long stop = System.currentTimeMillis();
96         logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec");
97     }
98
99     /**
100      * create new PerformanceArticle objects and insert them into the RDBMS.
101      * The number of objects to create is defined by <code>articleCount</code>.
102      */

103     protected void insertNewArticles() throws Exception JavaDoc
104     {
105         Connection JavaDoc conn = getConnection();
106         // Use the OJB SqlGenerator to generate SQL Statements. All details about
107
// Table and column names are read from the repository.xml file.
108
ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class);
109         String JavaDoc sql = broker.serviceSqlGenerator().getPreparedInsertStatement(cld).getStatement();
110
111         logger.debug("insert stmt: " + sql);
112
113         long start = System.currentTimeMillis();
114         try
115         {
116             conn.setAutoCommit(false);
117             PreparedStatement JavaDoc stmt = conn.prepareStatement(sql);
118
119             for(int i = 0; i < articleCount; i++)
120             {
121                 PerformanceArticle a = arr[i];
122
123                 stmt.setInt(1, a.articleId.intValue());
124                 stmt.setString(2, a.articleName);
125                 stmt.setInt(3, a.supplierId);
126                 stmt.setInt(4, a.productGroupId);
127                 stmt.setString(5, a.unit);
128                 stmt.setDouble(6, a.price);
129                 stmt.setInt(7, a.stock);
130                 stmt.setInt(8, a.orderedUnits);
131                 stmt.setInt(9, a.minimumStock);
132
133                 stmt.execute();
134             }
135             conn.commit();
136         }
137         catch(Throwable JavaDoc t)
138         {
139             logger.error(t);
140             fail(t.getMessage());
141         }
142         finally
143         {
144             if(conn != null)
145                 returnConnection(conn);
146         }
147         long stop = System.currentTimeMillis();
148         logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec");
149
150     }
151
152     protected void clearTable() throws Exception JavaDoc
153     {
154         Connection JavaDoc conn = getConnection();
155         ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class);
156         String JavaDoc table = cld.getFullTableName();
157         String JavaDoc column = cld.getFieldDescriptorByName("articleId").getColumnName();
158         String JavaDoc sql = "DELETE FROM " + table + " WHERE " + column + " >= " + offsetId;
159         PreparedStatement JavaDoc stmt = conn.prepareStatement(sql);
160         stmt.execute();
161         returnConnection(conn);
162     }
163
164     /**
165      * read in all the PerformanceArticles from the RDBMS that have
166      * been inserted by <code>insertNewArticles()</code>.
167      * The lookup is done one by one, that is: a primary key based lookup is used.
168      */

169     protected void readArticles() throws Exception JavaDoc
170     {
171         Connection JavaDoc conn = getConnection();
172
173         // Use the OJB SqlGenerator to generate SQL Statements. All details about
174
// Table and column names are read from the repository.xml file.
175
ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class);
176         String JavaDoc sql = broker.serviceSqlGenerator().getPreparedSelectByPkStatement(cld).getStatement();
177         logger.debug("select stmt: " + sql);
178
179         String JavaDoc colId = cld.getFieldDescriptorByName("articleId").getColumnName();
180         String JavaDoc colName = cld.getFieldDescriptorByName("articleName").getColumnName();
181         String JavaDoc colSupplier = cld.getFieldDescriptorByName("supplierId").getColumnName();
182         String JavaDoc colGroup = cld.getFieldDescriptorByName("productGroupId").getColumnName();
183         String JavaDoc colUnit = cld.getFieldDescriptorByName("unit").getColumnName();
184         String JavaDoc colPrice = cld.getFieldDescriptorByName("price").getColumnName();
185         String JavaDoc colStock = cld.getFieldDescriptorByName("stock").getColumnName();
186         String JavaDoc colOrdered = cld.getFieldDescriptorByName("orderedUnits").getColumnName();
187         String JavaDoc colMin = cld.getFieldDescriptorByName("minimumStock").getColumnName();
188
189         long start = System.currentTimeMillis();
190         try
191         {
192             conn.setAutoCommit(false);
193             PreparedStatement JavaDoc stmt = conn.prepareStatement(sql);
194             for(int i = 0; i < articleCount; i++)
195             {
196                 stmt.setInt(1, offsetId + i);
197                 ResultSet JavaDoc rs = stmt.executeQuery();
198                 rs.next();
199
200                 PerformanceArticle a = new PerformanceArticle();
201                 a.articleId = new Integer JavaDoc(rs.getInt(colId));
202                 a.articleName = rs.getString(colName);
203                 a.supplierId = rs.getInt(colSupplier);
204                 a.productGroupId = rs.getInt(colGroup);
205                 a.unit = rs.getString(colUnit);
206                 a.price = rs.getFloat(colPrice);
207                 a.stock = rs.getInt(colStock);
208                 a.orderedUnits = rs.getInt(colOrdered);
209                 a.minimumStock = rs.getInt(colMin);
210             }
211         }
212         catch(Throwable JavaDoc t)
213         {
214             logger.error(t);
215             fail(t.getMessage());
216         }
217         finally
218         {
219             if(conn != null)
220                 returnConnection(conn);
221         }
222
223         long stop = System.currentTimeMillis();
224         logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec");
225
226     }
227
228     /**
229      * read in all the PerformanceArticles from the RDBMS that have
230      * been inserted by <code>insertNewArticles()</code>.
231      * The lookup is done with a cursor fetch,
232      * that is: a between Statement is used to select all inserted PerformanceArticles
233      * and Objects are read in by fetching from the cursor (JDBC ResultSet).
234      */

235     protected void readArticlesByCursor() throws Exception JavaDoc
236     {
237         Connection JavaDoc conn = getConnection();
238
239         Criteria c = new Criteria();
240         c.addBetween("articleId", new Integer JavaDoc(offsetId), new Integer JavaDoc(offsetId + articleCount));
241         Query query = new QueryByCriteria(PerformanceArticle.class, c);
242
243         // Use the OJB SqlGenerator to generate SQL Statements. All details about
244
// Table and column names are read from the repository.xml file.
245
ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class);
246         String JavaDoc sql = broker.serviceSqlGenerator().getPreparedSelectStatement(query, cld).getStatement();
247
248         logger.debug("select stmt: " + sql);
249
250         String JavaDoc colId = cld.getFieldDescriptorByName("articleId").getColumnName();
251         String JavaDoc colName = cld.getFieldDescriptorByName("articleName").getColumnName();
252         String JavaDoc colSupplier = cld.getFieldDescriptorByName("supplierId").getColumnName();
253         String JavaDoc colGroup = cld.getFieldDescriptorByName("productGroupId").getColumnName();
254         String JavaDoc colUnit = cld.getFieldDescriptorByName("unit").getColumnName();
255         String JavaDoc colPrice = cld.getFieldDescriptorByName("price").getColumnName();
256         String JavaDoc colStock = cld.getFieldDescriptorByName("stock").getColumnName();
257         String JavaDoc colOrdered = cld.getFieldDescriptorByName("orderedUnits").getColumnName();
258         String JavaDoc colMin = cld.getFieldDescriptorByName("minimumStock").getColumnName();
259
260
261         int fetchCount = 0;
262         long start = System.currentTimeMillis();
263         try
264         {
265             conn.setAutoCommit(false);
266             PreparedStatement JavaDoc stmt = conn.prepareStatement(sql);
267             stmt.setInt(1, offsetId);
268             stmt.setInt(2, offsetId + articleCount);
269             ResultSet JavaDoc rs = stmt.executeQuery();
270             while(rs.next())
271             {
272                 fetchCount++;
273
274                 PerformanceArticle a = new PerformanceArticle();
275                 a.articleId = new Integer JavaDoc(rs.getInt(colId));
276                 a.articleName = rs.getString(colName);
277                 a.supplierId = rs.getInt(colSupplier);
278                 a.productGroupId = rs.getInt(colGroup);
279                 a.unit = rs.getString(colUnit);
280                 a.price = rs.getFloat(colPrice);
281                 a.stock = rs.getInt(colStock);
282                 a.orderedUnits = rs.getInt(colOrdered);
283                 a.minimumStock = rs.getInt(colMin);
284             }
285         }
286         catch(Throwable JavaDoc t)
287         {
288             logger.error(t);
289             fail(t.getMessage());
290         }
291         finally
292         {
293             if(conn != null)
294                 returnConnection(conn);
295         }
296
297         long stop = System.currentTimeMillis();
298         logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec");
299
300     }
301
302     /**
303      * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
304      * All objects are modified and changes are written to the RDBMS with an UPDATE.
305      */

306     protected void updateExistingArticles() throws Exception JavaDoc
307     {
308         Connection JavaDoc conn = getConnection();
309
310         // Use the OJB SqlGenerator to generate SQL Statements. All details about
311
// Table and column names are read from the repository.xml file.
312
ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class);
313         String JavaDoc sql = broker.serviceSqlGenerator().getPreparedUpdateStatement(cld).getStatement();
314         logger.debug("update stmt: " + sql);
315
316         // update all objects
317
for(int i = 0; i < articleCount; i++)
318         {
319             arr[i].setPrice(arr[i].getPrice() * 1.95583);
320         }
321
322         long start = System.currentTimeMillis();
323         try
324         {
325             conn.setAutoCommit(false);
326             PreparedStatement JavaDoc stmt = conn.prepareStatement(sql);
327             for(int i = 0; i < articleCount; i++)
328             {
329                 PerformanceArticle a = arr[i];
330                 stmt.setString(1, a.articleName);
331                 stmt.setInt(2, a.supplierId);
332                 stmt.setInt(3, a.productGroupId);
333                 stmt.setString(4, a.unit);
334                 stmt.setDouble(5, a.price);
335                 stmt.setInt(6, a.stock);
336                 stmt.setInt(7, a.orderedUnits);
337                 stmt.setInt(8, a.minimumStock);
338                 stmt.setInt(9, a.articleId.intValue());
339                 stmt.execute();
340             }
341             conn.commit();
342         }
343         catch(Throwable JavaDoc t)
344         {
345             logger.error(t);
346             fail(t.getMessage());
347         }
348         finally
349         {
350             if(conn != null)
351                 returnConnection(conn);
352         }
353         long stop = System.currentTimeMillis();
354         logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec");
355     }
356 }
357
Popular Tags