KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.otm;
2
3 import java.util.Iterator JavaDoc;
4
5 import junit.framework.TestCase;
6 import org.apache.ojb.otm.core.Transaction;
7 import org.apache.ojb.otm.lock.LockType;
8 import org.apache.ojb.broker.Identity;
9 import org.apache.ojb.broker.PerformanceArticle;
10 import org.apache.ojb.broker.PersistenceBrokerFactory;
11 import org.apache.ojb.broker.query.Criteria;
12 import org.apache.ojb.broker.query.Query;
13 import org.apache.ojb.broker.query.QueryByCriteria;
14 import org.apache.ojb.broker.util.logging.Logger;
15 import org.apache.ojb.broker.util.logging.LoggerFactory;
16
17 /**
18  * This TestCase contains the OJB performance benchmarks for the
19  * OTM API.
20  * @author Oleg Nitz, Matthew Baird, borrowing heavily from Thomas Mahler
21  */

22 public class PerformanceTest extends TestCase
23 {
24     private Logger logger = LoggerFactory.getLogger("performance");
25
26     private TestKit _kit;
27
28     private OTMConnection _conn;
29
30     private PerformanceArticle[] arr;
31     /**
32      * the number of PerformanceArticle objects to work with.
33      */

34     static int articleCount = 10000;
35     /**
36      * the number of iterations to perform.
37      */

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

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

55     public static void main(String JavaDoc[] args)
56     {
57         if (args.length > 0)
58         {
59             articleCount = Integer.parseInt(args[0]);
60         }
61         if (args.length > 1)
62         {
63             iterations = Integer.parseInt(args[1]);
64         }
65         String JavaDoc[] arr = {PerformanceTest.class.getName()};
66         junit.textui.TestRunner.main(arr);
67     }
68
69     public void setUp()
70     {
71         _kit = TestKit.getTestInstance();
72         _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
73         arr = new PerformanceArticle[articleCount];
74         for (int i = 0; i < articleCount; i++)
75         {
76             PerformanceArticle a = createArticle(offsetId + i);
77             arr[i] = a;
78         }
79     }
80
81     public void tearDown()
82     {
83         _conn.close();
84         _conn = null;
85     }
86
87     /**
88      * factory method that createa an PerformanceArticle with a given id.
89      * @return the created PerformanceArticle object
90      * @param id the primary key value for the new object
91      */

92     private PerformanceArticle createArticle(int id)
93     {
94         PerformanceArticle a = new PerformanceArticle();
95         a.setArticleId(id);
96         a.setArticleName("New Performance Article " + id);
97         a.setMinimumStock(100);
98         a.setOrderedUnits(17);
99         a.setPrice(0.45);
100         a.setProductGroupId(1);
101         a.setStock(234);
102         a.setSupplierId(4);
103         a.setUnit("bottle");
104         return a;
105     }
106
107     /**
108      * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
109      */

110     protected void deleteArticles() throws Exception JavaDoc
111     {
112         long start = System.currentTimeMillis();
113         Transaction tx = _kit.getTransaction(_conn);
114         tx.begin();
115         for (int i = 0; i < articleCount; i++)
116         {
117             _conn.deletePersistent(arr[i]);
118         }
119         tx.commit();
120         long stop = System.currentTimeMillis();
121         logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec");
122     }
123
124     /**
125
126      * create new PerformanceArticle objects and insert them into the RDBMS.
127
128      * The number of objects to create is defined by <code>articleCount</code>.
129
130      */

131     protected void insertNewArticles() throws Exception JavaDoc
132     {
133         long start = System.currentTimeMillis();
134         Transaction tx = _kit.getTransaction(_conn);
135         tx.begin();
136         for (int i = 0; i < articleCount; i++)
137         {
138             _conn.makePersistent(arr[i]);
139         }
140         tx.commit();
141         long stop = System.currentTimeMillis();
142         logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec");
143     }
144
145     /**
146      * read in all the PerformanceArticles from the RDBMS that have
147      * been inserted by <code>insertNewArticles()</code>.
148      * The lookup is done one by one, that is: a primary key based lookup is used.
149      */

150     protected void readArticles() throws Exception JavaDoc
151     {
152         long start = System.currentTimeMillis();
153         Transaction tx = _kit.getTransaction(_conn);
154         tx.begin();
155         for (int i = 0; i < articleCount; i++)
156         {
157             Object JavaDoc[] pks = {new Integer JavaDoc(offsetId + i)};
158             Identity oid = new Identity(PerformanceArticle.class, PerformanceArticle.class,pks);
159             PerformanceArticle a = (PerformanceArticle) _conn.getObjectByIdentity(oid, LockType.NO_LOCK);
160         }
161         tx.commit();
162         long stop = System.currentTimeMillis();
163         logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec");
164     }
165
166     /**
167      * read in all the PerformanceArticles from the RDBMS that have
168      * been inserted by <code>insertNewArticles()</code>.
169      * The lookup is done with a cursor fetch,
170      * that is: a between Statement is used to select all inserted PerformanceArticles
171      * and Objects are read in by fetching from the cursor (JDBC ResultSet).
172      */

173     protected void readArticlesByCursor() throws Exception JavaDoc
174     {
175         // clear the cache
176
_conn.invalidateAll();
177
178         Transaction tx = _kit.getTransaction(_conn);
179         Criteria c = new Criteria();
180         c.addBetween("articleId", new Integer JavaDoc(offsetId), new Integer JavaDoc(offsetId + articleCount));
181         Query q = new QueryByCriteria(PerformanceArticle.class, c);
182         long start = System.currentTimeMillis();
183         tx.begin();
184         Iterator JavaDoc iter = _conn.getIteratorByQuery(q, LockType.NO_LOCK);
185         int fetchCount = 0;
186         while (iter.hasNext())
187         {
188             fetchCount++;
189             PerformanceArticle a = (PerformanceArticle) iter.next();
190         }
191         tx.commit();
192         long stop = System.currentTimeMillis();
193         logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec");
194     }
195
196     /**
197      * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
198      * All objects are modified and changes are written to the RDBMS with an UPDATE.
199      */

200     protected void updateExistingArticles() throws Exception JavaDoc
201     {
202         long start = System.currentTimeMillis();
203         Transaction tx = _kit.getTransaction(_conn);
204         tx.begin();
205         // update all objects
206
for (int i = 0; i < articleCount; i++)
207         {
208             _conn.lockForWrite(arr[i]);
209             arr[i].setPrice(arr[i].getPrice() * 1.95583);
210         }
211         tx.commit();
212         long stop = System.currentTimeMillis();
213         logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec");
214     }
215
216     /**
217      * this method is the driver for the complete Benchmark.
218      * It performs the following steps:
219      *
220      * 1.) n objects are created and inserted to the RDBMS.
221      * 2.) the created objects are modified. Modifications are written to the RDBMS with updates.
222      * 3.) All objects created in 1.) are read in by primary key based SELECT statements.
223      * 4.) Step 3.) is repeated to test caching facilities.
224      * 5.) All objects created in 1.) are read by iterating over a ResultSet.
225      * 6.) All objects created in 1.) are deleted with n separate DELETE Statements.
226      */

227     public void testBenchmark()
228     {
229         try
230         {
231             logger.info("Test for OTM-api");
232             for (int i = 0; i < iterations; i++)
233             {
234                 logger.info("");
235                 // store all Article objects
236
insertNewArticles();
237                 // update all objects
238
updateExistingArticles();
239                 // querying with empty cache
240
_conn.invalidateAll();
241
242                 readArticles();
243                 // querying with hot cache
244
readArticles();
245                 // fetching through cursor
246
readArticlesByCursor();
247                 // delete all objects
248
deleteArticles();
249             }
250         }
251         catch (Throwable JavaDoc t)
252         {
253             logger.error(t);
254             fail(t.getMessage());
255         }
256     }
257 }
258
259
Popular Tags