KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > PerformanceTest


1 package org.apache.ojb.broker;
2
3 import junit.framework.TestCase;
4
5 import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectAccessImpl;
6 import org.apache.ojb.broker.query.Criteria;
7 import org.apache.ojb.broker.query.Query;
8 import org.apache.ojb.broker.query.QueryByCriteria;
9 import org.apache.ojb.broker.util.ObjectModificationDefaultImpl;
10 import org.apache.ojb.broker.util.configuration.impl.OjbConfiguration;
11 import org.apache.ojb.broker.util.logging.Logger;
12 import org.apache.ojb.broker.util.logging.LoggerFactory;
13
14 import java.util.Iterator JavaDoc;
15
16 /**
17  * This TestCase contains the OJB performance benchmarks for the
18  * PersistenceBroker API.
19  * @author Thomas Mahler
20  */

21 public class PerformanceTest extends TestCase
22 {
23     private Logger logger = LoggerFactory.getLogger("performance");
24     /**
25      * the number of PerformanceArticle objects to work with.
26      */

27     static int articleCount = 10000;
28
29     /**
30      * the number of iterations to perform.
31      */

32     static int iterations = 2;
33
34     /**
35      * the offset value for PerformanceArticle primary keys
36      */

37     int offsetId = 123456;
38     PersistenceBroker broker;
39     private PerformanceArticle[] arr;
40
41     public PerformanceTest(String JavaDoc name)
42     {
43         super(name);
44     }
45
46     /**
47      * launches the TestCase.
48      * The number of Objects to work with and the number of iterations
49      * to be performed can be adjusted by setting them as commandline parameters.
50      * @param args the String[] holding the commandline parameters.
51      */

52     public static void main(String JavaDoc[] args)
53     {
54         if (args.length > 0)
55         {
56             articleCount = Integer.parseInt(args[0]);
57         }
58         if (args.length > 1)
59         {
60             iterations = Integer.parseInt(args[1]);
61         }
62
63         String JavaDoc[] arr = {PerformanceTest.class.getName()};
64         junit.textui.TestRunner.main(arr);
65     }
66
67     /**
68      * Setting up the test fixture.
69      */

70     public void setUp()
71     {
72         try
73         {
74             // obtain broker instance
75
broker = PersistenceBrokerFactory.defaultPersistenceBroker();
76             // manipulate configuration to use maximum performance field access
77
OjbConfiguration conf = (OjbConfiguration) PersistenceBrokerFactory.
78                     getConfigurator().getConfigurationFor(null);
79             conf.setPersistentFieldClass(PersistentFieldDirectAccessImpl.class);
80         }
81         catch (PBFactoryException e)
82         {
83         }
84
85         arr = new PerformanceArticle[articleCount];
86         for (int i = 0; i < articleCount; i++)
87         {
88             PerformanceArticle a = createArticle(offsetId + i);
89             arr[i] = a;
90         }
91     }
92
93     /**
94      * tearing down the test fixture.
95      */

96     public void tearDown()
97     {
98         broker.close();
99     }
100
101     /**
102      * factory method that createa an PerformanceArticle with a given id.
103      * @return the created PerformanceArticle object
104      * @param id the primary key value for the new object
105      */

106     private PerformanceArticle createArticle(int id)
107     {
108         PerformanceArticle a = new PerformanceArticle();
109         a.setArticleId(id);
110         a.setArticleName("New Performance Article " + id);
111         a.setMinimumStock(100);
112         a.setOrderedUnits(17);
113         a.setPrice(0.45);
114         a.setProductGroupId(1);
115         a.setStock(234);
116         a.setSupplierId(4);
117         a.setUnit("bottle");
118         return a;
119     }
120
121
122     /**
123      * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
124      */

125     protected void deleteArticles() throws PersistenceBrokerException
126     {
127         long start = System.currentTimeMillis();
128         broker.beginTransaction();
129         for (int i = 0; i < articleCount; i++)
130         {
131             broker.delete(arr[i]);
132         }
133         broker.commitTransaction();
134         long stop = System.currentTimeMillis();
135         logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec");
136     }
137
138
139     /**
140      * create new PerformanceArticle objects and insert them into the RDBMS.
141      * The number of objects to create is defined by <code>articleCount</code>.
142      */

143     protected void insertNewArticles() throws PersistenceBrokerException
144     {
145         ObjectModificationDefaultImpl needsInsert = new ObjectModificationDefaultImpl();
146         needsInsert.setNeedsInsert(true);
147
148         long start = System.currentTimeMillis();
149         broker.beginTransaction();
150         for (int i = 0; i < articleCount; i++)
151         {
152             broker.store(arr[i], needsInsert);
153         }
154         broker.commitTransaction();
155         long stop = System.currentTimeMillis();
156         logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec");
157
158     }
159
160     /**
161      * read in all the PerformanceArticles from the RDBMS that have
162      * been inserted by <code>insertNewArticles()</code>.
163      * The lookup is done one by one, that is: a primary key based lookup is used.
164      */

165     protected void readArticles() throws PersistenceBrokerException
166     {
167         long start = System.currentTimeMillis();
168         broker.beginTransaction();
169         for (int i = 0; i < articleCount; i++)
170         {
171             Object JavaDoc[] pks = {new Integer JavaDoc(offsetId + i)};
172             Identity oid = new Identity(PerformanceArticle.class, PerformanceArticle.class,pks);
173
174             PerformanceArticle a = (PerformanceArticle) broker.getObjectByIdentity(oid);
175         }
176         broker.commitTransaction();
177         long stop = System.currentTimeMillis();
178         logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec");
179
180     }
181
182     /**
183      * read in all the PerformanceArticles from the RDBMS that have
184      * been inserted by <code>insertNewArticles()</code>.
185      * The lookup is done with a cursor fetch,
186      * that is: a between Statement is used to select all inserted PerformanceArticles
187      * and Objects are read in by fetching from the cursor (JDBC ResultSet).
188      */

189     protected void readArticlesByCursor() throws PersistenceBrokerException
190
191     {
192         broker.clearCache();
193         Criteria c = new Criteria();
194         c.addBetween("articleId", new Integer JavaDoc(offsetId), new Integer JavaDoc(offsetId + articleCount));
195         Query q = new QueryByCriteria(PerformanceArticle.class, c);
196
197         long start = System.currentTimeMillis();
198         Iterator JavaDoc iter = broker.getIteratorByQuery(q);
199         int fetchCount = 0;
200         while (iter.hasNext())
201         {
202             fetchCount++;
203             PerformanceArticle a = (PerformanceArticle) iter.next();
204         }
205         long stop = System.currentTimeMillis();
206         logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec");
207
208     }
209
210     /**
211      * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
212      * All objects are modified and changes are written to the RDBMS with an UPDATE.
213      */

214     protected void updateExistingArticles() throws PersistenceBrokerException
215     {
216
217         // update all objects
218
for (int i = 0; i < articleCount; i++)
219         {
220             arr[i].setPrice(arr[i].getPrice() * 1.95583);
221         }
222
223         // build mod object that tell OJB to use UPDATE
224
ObjectModificationDefaultImpl needsUpdate = new ObjectModificationDefaultImpl();
225         needsUpdate.setNeedsUpdate(true);
226
227         long start = System.currentTimeMillis();
228         broker.beginTransaction();
229         for (int i = 0; i < articleCount; i++)
230         {
231             broker.store(arr[i], needsUpdate);
232         }
233         broker.commitTransaction();
234         long stop = System.currentTimeMillis();
235         logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec");
236
237     }
238
239     /**
240      * this method is the driver for the complete Benchmark.
241      * It performs the following steps:
242      *
243      * 1.) n objects are created and inserted to the RDBMS.
244      * 2.) the created objects are modified. Modifications are written to the RDBMS with updates.
245      * 3.) All objects created in 1.) are read in by primary key based SELECT statements.
246      * 4.) Step 3.) is repeated to test caching facilities.
247      * 5.) All objects created in 1.) are read by iterating over a ResultSet.
248      * 6.) All objects created in 1.) are deleted with n separate DELETE Statements.
249      */

250     public void testBenchmark()
251     {
252         try
253         {
254             logger.info("Test for PB-api");
255             for (int i = 0; i < iterations; i++)
256             {
257                 logger.info("");
258
259                 // store all Article objects
260
insertNewArticles();
261
262                 // update all objects
263
updateExistingArticles();
264
265                 // querying with empty cache
266
broker.clearCache();
267                 readArticles();
268
269                 // querying with hot cache
270
readArticles();
271
272                 // fetching through cursor
273
readArticlesByCursor();
274
275                 // delete all objects
276
deleteArticles();
277             }
278         }
279         catch (Throwable JavaDoc t)
280         {
281             logger.error(t);
282             fail(t.getMessage());
283         }
284     }
285
286 }
287
Popular Tags