KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.odmg;
2
3 import java.util.Iterator JavaDoc;
4
5 import junit.framework.TestCase;
6 import org.apache.ojb.broker.ManageableCollection;
7 import org.apache.ojb.broker.PBFactoryException;
8 import org.apache.ojb.broker.PerformanceArticle;
9 import org.apache.ojb.broker.PersistenceBroker;
10 import org.apache.ojb.broker.PersistenceBrokerFactory;
11 import org.apache.ojb.broker.TestHelper;
12 import org.apache.ojb.broker.util.logging.Logger;
13 import org.apache.ojb.broker.util.logging.LoggerFactory;
14 import org.odmg.Database;
15 import org.odmg.Implementation;
16 import org.odmg.OQLQuery;
17 import org.odmg.Transaction;
18
19 /**
20  * This TestCase contains the OJB performance benchmarks for the
21  * ODMG API.
22  * @author Matthew Baird, borrowing heavily from Thomas Mahler
23  */

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

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

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

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

54     public static void main(String JavaDoc[] args)
55     {
56         if (args.length > 0)
57         {
58             articleCount = Integer.parseInt(args[0]);
59         }
60         if (args.length > 1)
61         {
62             iterations = Integer.parseInt(args[1]);
63         }
64         String JavaDoc[] arr = {PerformanceTest.class.getName()};
65         junit.textui.TestRunner.main(arr);
66     }
67
68     /**
69      * Insert the method's description here.
70      * Creation date: (06.12.2000 21:58:53)
71      */

72     public void setUp() throws PBFactoryException
73     {
74         databaseName = TestHelper.DEF_DATABASE_NAME;
75         arr = new PerformanceArticle[articleCount];
76         for (int i = 0; i < articleCount; i++)
77         {
78             PerformanceArticle a = createArticle(offsetId + i);
79             arr[i] = a;
80         }
81     }
82
83     /**
84      * Insert the method's description here.
85      * Creation date: (06.12.2000 21:59:14)
86      */

87     public void tearDown()
88     {
89         databaseName = null;
90     }
91
92     /**
93      * factory method that createa an PerformanceArticle with a given id.
94      * @return the created PerformanceArticle object
95      * @param id the primary key value for the new object
96      */

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

115     protected void deleteArticles() throws Exception JavaDoc
116     {
117         Implementation odmg = OJB.getInstance();
118         Database db = odmg.newDatabase();
119         db.open(databaseName, Database.OPEN_READ_WRITE);
120         Transaction tx = odmg.newTransaction();
121         long start = System.currentTimeMillis();
122         tx.begin();
123         for (int i = 0; i < articleCount; i++)
124         {
125             db.deletePersistent(arr[i]);
126         }
127         tx.commit();
128         long stop = System.currentTimeMillis();
129         db.close();
130         logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec");
131     }
132
133     /**
134
135      * create new PerformanceArticle objects and insert them into the RDBMS.
136
137      * The number of objects to create is defined by <code>articleCount</code>.
138
139      */

140     protected void insertNewArticles() throws Exception JavaDoc
141     {
142         Implementation odmg = OJB.getInstance();
143         Database db = odmg.newDatabase();
144         db.open(databaseName, Database.OPEN_READ_WRITE);
145         Transaction tx = odmg.newTransaction();
146         long start = System.currentTimeMillis();
147         tx.begin();
148         for (int i = 0; i < articleCount; i++)
149         {
150             db.makePersistent(arr[i]);
151         }
152         tx.commit();
153         long stop = System.currentTimeMillis();
154         db.close();
155         logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec");
156     }
157
158     /**
159      * read in all the PerformanceArticles from the RDBMS that have
160      * been inserted by <code>insertNewArticles()</code>.
161      * The lookup is done one by one, that is: a primary key based lookup is used.
162      */

163     protected void readArticles() throws Exception JavaDoc
164     {
165         Implementation odmg = OJB.getInstance();
166         Database db = odmg.newDatabase();
167         long start = System.currentTimeMillis();
168         db.open(databaseName, Database.OPEN_READ_ONLY);
169         Transaction tx = odmg.newTransaction();
170         tx.begin();
171         for (int i = 0; i < articleCount; i++)
172         {
173             OQLQuery query = odmg.newOQLQuery();
174             String JavaDoc sql = "select allArticles from " + PerformanceArticle.class.getName() + " where articleId=" + i;
175             query.create(sql);
176             query.execute();
177         }
178         tx.commit();
179         long stop = System.currentTimeMillis();
180         db.close();
181         logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec");
182     }
183
184     /**
185      * read in all the PerformanceArticles from the RDBMS that have
186      * been inserted by <code>insertNewArticles()</code>.
187      * The lookup is done with a cursor fetch,
188      * that is: a between Statement is used to select all inserted PerformanceArticles
189      * and Objects are read in by fetching from the cursor (JDBC ResultSet).
190      */

191     protected void readArticlesByCursor() throws Exception JavaDoc
192     {
193         // clear the cache
194
PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
195         broker.clearCache();
196         broker.close();
197
198         Implementation odmg = OJB.getInstance();
199         Database db = odmg.newDatabase();
200         db.open(databaseName, Database.OPEN_READ_WRITE);
201         OQLQuery query = odmg.newOQLQuery();
202         String JavaDoc sql = "select allArticles from " + PerformanceArticle.class.getName() + " where articleId between " + new Integer JavaDoc(offsetId) + " and " + new Integer JavaDoc(offsetId + articleCount);
203         query.create(sql);
204         long start = System.currentTimeMillis();
205         ManageableCollection collection = (ManageableCollection) query.execute();
206         Iterator JavaDoc iter = collection.ojbIterator();
207         int fetchCount = 0;
208         while (iter.hasNext())
209         {
210             fetchCount++;
211             PerformanceArticle a = (PerformanceArticle) iter.next();
212         }
213         long stop = System.currentTimeMillis();
214         db.close();
215         logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec");
216     }
217
218     /**
219      * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
220      * All objects are modified and changes are written to the RDBMS with an UPDATE.
221      */

222     protected void updateExistingArticles() throws Exception JavaDoc
223     {
224         Implementation odmg = OJB.getInstance();
225         Database db = odmg.newDatabase();
226         db.open(databaseName, Database.OPEN_READ_WRITE);
227         Transaction tx = odmg.newTransaction();
228         long start = System.currentTimeMillis();
229         tx.begin();
230         // update all objects
231
for (int i = 0; i < articleCount; i++)
232         {
233             tx.lock(arr[i], Transaction.WRITE);
234             arr[i].setPrice(arr[i].getPrice() * 1.95583);
235         }
236         tx.commit();
237         long stop = System.currentTimeMillis();
238         logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec");
239     }
240
241     /**
242      * this method is the driver for the complete Benchmark.
243      * It performs the following steps:
244      *
245      * 1.) n objects are created and inserted to the RDBMS.
246      * 2.) the created objects are modified. Modifications are written to the RDBMS with updates.
247      * 3.) All objects created in 1.) are read in by primary key based SELECT statements.
248      * 4.) Step 3.) is repeated to test caching facilities.
249      * 5.) All objects created in 1.) are read by iterating over a ResultSet.
250      * 6.) All objects created in 1.) are deleted with n separate DELETE Statements.
251      */

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