KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.compare;
2
3 import java.util.Iterator JavaDoc;
4
5 import org.apache.ojb.broker.Identity;
6 import org.apache.ojb.broker.PersistenceBrokerException;
7 import org.apache.ojb.broker.query.Criteria;
8 import org.apache.ojb.broker.query.Query;
9 import org.apache.ojb.broker.query.QueryByCriteria;
10 import org.apache.ojb.broker.util.ObjectModification;
11
12 /**
13  * This TestCase contains the OJB single-threaded performance benchmarks for the
14  * PersistenceBroker-API.
15  *
16  * @author Thomas Mahler
17  */

18 public class PerformancePBTest extends PerformanceBaseTest
19 {
20     public PerformancePBTest(String JavaDoc name)
21     {
22         super(name);
23         setNameOfTest("Test for PB-api");
24     }
25
26     /**
27      * launches the TestCase.
28      * The number of Objects to work with and the number of iterations
29      * to be performed can be adjusted by setting them as commandline parameters.
30      *
31      * @param args the String[] holding the commandline parameters.
32      */

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

56     protected void deleteArticles() throws PersistenceBrokerException
57     {
58         long start = System.currentTimeMillis();
59         broker.beginTransaction();
60         for(int i = 0; i < articleCount; i++)
61         {
62             broker.delete(arr[i]);
63         }
64         broker.commitTransaction();
65         long stop = System.currentTimeMillis();
66         logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec");
67     }
68
69
70     /**
71      * create new PerformanceArticle objects and insert them into the RDBMS.
72      * The number of objects to create is defined by <code>articleCount</code>.
73      */

74     protected void insertNewArticles() throws PersistenceBrokerException
75     {
76         long start = System.currentTimeMillis();
77         broker.beginTransaction();
78         for(int i = 0; i < articleCount; i++)
79         {
80             broker.store(arr[i], ObjectModification.INSERT);
81         }
82         broker.commitTransaction();
83         long stop = System.currentTimeMillis();
84         logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec");
85
86     }
87
88     /**
89      * read in all the PerformanceArticles from the RDBMS that have
90      * been inserted by <code>insertNewArticles()</code>.
91      * The lookup is done one by one, that is: a primary key based lookup is used.
92      */

93     protected void readArticles() throws PersistenceBrokerException
94     {
95         long start = System.currentTimeMillis();
96         broker.beginTransaction();
97         for(int i = 0; i < articleCount; i++)
98         {
99             Identity oid = broker.serviceIdentity().
100                     buildIdentity(PerformanceArticle.class, arr[i].getArticleId());
101             broker.getObjectByIdentity(oid);
102         }
103         broker.commitTransaction();
104         long stop = System.currentTimeMillis();
105         logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec");
106
107     }
108
109     /**
110      * read in all the PerformanceArticles from the RDBMS that have
111      * been inserted by <code>insertNewArticles()</code>.
112      * The lookup is done with a cursor fetch,
113      * that is: a between Statement is used to select all inserted PerformanceArticles
114      * and Objects are read in by fetching from the cursor (JDBC ResultSet).
115      */

116     protected void readArticlesByCursor() throws PersistenceBrokerException
117
118     {
119         broker.clearCache();
120         Criteria c = new Criteria();
121         c.addBetween("articleId", new Integer JavaDoc(offsetId), new Integer JavaDoc(offsetId + articleCount));
122         Query q = new QueryByCriteria(PerformanceArticle.class, c);
123
124         long start = System.currentTimeMillis();
125         Iterator JavaDoc iter = broker.getIteratorByQuery(q);
126         int fetchCount = 0;
127         while(iter.hasNext())
128         {
129             fetchCount++;
130             iter.next();
131         }
132         long stop = System.currentTimeMillis();
133         logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec");
134
135     }
136
137     /**
138      * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
139      * All objects are modified and changes are written to the RDBMS with an UPDATE.
140      */

141     protected void updateExistingArticles() throws PersistenceBrokerException
142     {
143         // update all objects
144
for(int i = 0; i < articleCount; i++)
145         {
146             arr[i].setPrice(arr[i].getPrice() * 1.95583);
147         }
148
149         long start = System.currentTimeMillis();
150         broker.beginTransaction();
151         for(int i = 0; i < articleCount; i++)
152         {
153             broker.store(arr[i], ObjectModification.UPDATE);
154         }
155         broker.commitTransaction();
156         long stop = System.currentTimeMillis();
157         logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec");
158     }
159 }
160
Popular Tags