KickJava   Java API By Example, From Geeks To Geeks.

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


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.PersistenceBrokerFactory;
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.otm.OTMConnection;
11 import org.apache.ojb.otm.TestKit;
12 import org.apache.ojb.otm.core.Transaction;
13 import org.apache.ojb.otm.lock.LockType;
14
15 /**
16  * This TestCase contains the OJB performance benchmarks for the
17  * OTM API.
18  *
19  * @author Oleg Nitz, Matthew Baird, borrowing heavily from Thomas Mahler
20  */

21 public class PerformanceOTMTest extends PerformanceBaseTest
22 {
23     private TestKit _kit;
24     private OTMConnection _conn;
25
26     public PerformanceOTMTest(String JavaDoc name)
27     {
28         super(name);
29         setNameOfTest("Test for OTM-api");
30     }
31
32     /**
33      * launches the TestCase.
34      * The number of Objects to work with and the number of iterations
35      * to be performed can be adjusted by setting them as commandline parameters.
36      *
37      * @param args the String[] holding the commandline parameters.
38      */

39     public static void main(String JavaDoc[] args)
40     {
41         if(args.length > 0)
42         {
43             articleCount = Integer.parseInt(args[0]);
44         }
45         if(args.length > 1)
46         {
47             iterations = Integer.parseInt(args[1]);
48         }
49         String JavaDoc[] arr = {PerformanceOTMTest.class.getName()};
50         junit.textui.TestRunner.main(arr);
51     }
52
53     public void setUp() throws Exception JavaDoc
54     {
55         super.setUp();
56
57         _kit = TestKit.getTestInstance();
58         _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
59         arr = new PerformanceArticle[articleCount];
60         for(int i = 0; i < articleCount; i++)
61         {
62             PerformanceArticle a = createArticle(offsetId + i);
63             arr[i] = a;
64         }
65     }
66
67     public void tearDown() throws Exception JavaDoc
68     {
69         _conn.close();
70         _conn = null;
71
72         super.tearDown();
73     }
74
75     public void testBenchmark() throws Exception JavaDoc
76     {
77         super.testBenchmark();
78     }
79
80     /**
81      * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
82      */

83     protected void deleteArticles() throws Exception JavaDoc
84     {
85         long start = System.currentTimeMillis();
86         Transaction tx = _kit.getTransaction(_conn);
87         tx.begin();
88         for(int i = 0; i < articleCount; i++)
89         {
90             _conn.deletePersistent(arr[i]);
91         }
92         tx.commit();
93         long stop = System.currentTimeMillis();
94         logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec");
95     }
96
97     /**
98      * create new PerformanceArticle objects and insert them into the RDBMS.
99      * <p/>
100      * The number of objects to create is defined by <code>articleCount</code>.
101      */

102     protected void insertNewArticles() throws Exception JavaDoc
103     {
104         long start = System.currentTimeMillis();
105         Transaction tx = _kit.getTransaction(_conn);
106         tx.begin();
107         for(int i = 0; i < articleCount; i++)
108         {
109             _conn.makePersistent(arr[i]);
110         }
111         tx.commit();
112         long stop = System.currentTimeMillis();
113         logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec");
114     }
115
116     /**
117      * read in all the PerformanceArticles from the RDBMS that have
118      * been inserted by <code>insertNewArticles()</code>.
119      * The lookup is done one by one, that is: a primary key based lookup is used.
120      */

121     protected void readArticles() throws Exception JavaDoc
122     {
123         long start = System.currentTimeMillis();
124         Transaction tx = _kit.getTransaction(_conn);
125         tx.begin();
126         for(int i = 0; i < articleCount; i++)
127         {
128             Object JavaDoc[] pks = {new Integer JavaDoc(offsetId + i)};
129             Identity oid = new Identity(PerformanceArticle.class, PerformanceArticle.class, pks);
130             _conn.getObjectByIdentity(oid, LockType.NO_LOCK);
131         }
132         tx.commit();
133         long stop = System.currentTimeMillis();
134         logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec");
135     }
136
137     /**
138      * read in all the PerformanceArticles from the RDBMS that have
139      * been inserted by <code>insertNewArticles()</code>.
140      * The lookup is done with a cursor fetch,
141      * that is: a between Statement is used to select all inserted PerformanceArticles
142      * and Objects are read in by fetching from the cursor (JDBC ResultSet).
143      */

144     protected void readArticlesByCursor() throws Exception JavaDoc
145     {
146         // clear the cache
147
_conn.invalidateAll();
148
149         Transaction tx = _kit.getTransaction(_conn);
150         Criteria c = new Criteria();
151         c.addBetween("articleId", new Integer JavaDoc(offsetId), new Integer JavaDoc(offsetId + articleCount));
152         Query q = new QueryByCriteria(PerformanceArticle.class, c);
153         long start = System.currentTimeMillis();
154         tx.begin();
155         Iterator JavaDoc iter = _conn.getIteratorByQuery(q, LockType.NO_LOCK);
156         int fetchCount = 0;
157         while(iter.hasNext())
158         {
159             fetchCount++;
160             iter.next();
161         }
162         tx.commit();
163         long stop = System.currentTimeMillis();
164         logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec");
165     }
166
167     /**
168      * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
169      * All objects are modified and changes are written to the RDBMS with an UPDATE.
170      */

171     protected void updateExistingArticles() throws Exception JavaDoc
172     {
173         long start = System.currentTimeMillis();
174         Transaction tx = _kit.getTransaction(_conn);
175         tx.begin();
176         // update all objects
177
for(int i = 0; i < articleCount; i++)
178         {
179             _conn.lockForWrite(arr[i]);
180             arr[i].setPrice(arr[i].getPrice() * 1.95583);
181         }
182         tx.commit();
183         long stop = System.currentTimeMillis();
184         logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec");
185     }
186 }
187
188
Popular Tags