KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

38     public static void main(String JavaDoc[] args)
39     {
40         if(args.length > 0)
41         {
42             articleCount = Integer.parseInt(args[0]);
43         }
44         if(args.length > 1)
45         {
46             iterations = Integer.parseInt(args[1]);
47         }
48         String JavaDoc[] arr = {PerformanceODMGTest.class.getName()};
49         junit.textui.TestRunner.main(arr);
50     }
51
52     public void testBenchmark() throws Exception JavaDoc
53     {
54         super.testBenchmark();
55     }
56
57     public void setUp() throws Exception JavaDoc
58     {
59         // madatory to call super class method
60
super.setUp();
61
62         odmg = OJB.getInstance();
63         db = odmg.newDatabase();
64         db.open(TestHelper.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE);
65     }
66
67     public void tearDown() throws Exception JavaDoc
68     {
69         super.tearDown();
70     }
71
72     /**
73      * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
74      */

75     protected void deleteArticles() throws Exception JavaDoc
76     {
77         Transaction tx = odmg.newTransaction();
78         long start = System.currentTimeMillis();
79         tx.begin();
80         for(int i = 0; i < articleCount; i++)
81         {
82             db.deletePersistent(arr[i]);
83         }
84         tx.commit();
85         long stop = System.currentTimeMillis();
86         logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec");
87     }
88
89     /**
90      * create new PerformanceArticle objects and insert them into the RDBMS.
91      * <p/>
92      * The number of objects to create is defined by <code>articleCount</code>.
93      */

94     protected void insertNewArticles() throws Exception JavaDoc
95     {
96         Transaction tx = odmg.newTransaction();
97         long start = System.currentTimeMillis();
98         tx.begin();
99         for(int i = 0; i < articleCount; i++)
100         {
101             db.makePersistent(arr[i]);
102         }
103         tx.commit();
104         long stop = System.currentTimeMillis();
105         logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec");
106     }
107
108     /**
109      * read in all the PerformanceArticles from the RDBMS that have
110      * been inserted by <code>insertNewArticles()</code>.
111      * The lookup is done one by one, that is: a primary key based lookup is used.
112      */

113     protected void readArticles() throws Exception JavaDoc
114     {
115         TransactionExt tx = (TransactionExt) odmg.newTransaction();
116         // we don't want implicite locks when compare performance
117
tx.setImplicitLocking(false);
118         String JavaDoc sql = "select allArticles from " + PerformanceArticle.class.getName() + " where articleId=$1";
119         long start = System.currentTimeMillis();
120         tx.begin();
121         for(int i = 0; i < articleCount; i++)
122         {
123             OQLQuery query = odmg.newOQLQuery();
124             query.create(sql);
125             query.bind(arr[i].getArticleId());
126             query.execute();
127         }
128         tx.commit();
129         long stop = System.currentTimeMillis();
130         logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec");
131     }
132
133     /**
134      * read in all the PerformanceArticles from the RDBMS that have
135      * been inserted by <code>insertNewArticles()</code>.
136      * The lookup is done with a cursor fetch,
137      * that is: a between Statement is used to select all inserted PerformanceArticles
138      * and Objects are read in by fetching from the cursor (JDBC ResultSet).
139      */

140     protected void readArticlesByCursor() throws Exception JavaDoc
141     {
142         TransactionExt tx = (TransactionExt) odmg.newTransaction();
143         // we don't want implicite locks when compare performance
144
tx.setImplicitLocking(false);
145         tx.begin();
146         // clear cache to read from DB
147
tx.getBroker().clearCache();
148
149         long start = System.currentTimeMillis();
150         OQLQuery query = odmg.newOQLQuery();
151         String JavaDoc sql = "select allArticles from " + PerformanceArticle.class.getName()
152                 + " where articleId between " + new Integer JavaDoc(offsetId) + " and "
153                 + new Integer JavaDoc(offsetId + articleCount);
154         query.create(sql);
155         ManageableCollection collection = (ManageableCollection) query.execute();
156         Iterator JavaDoc iter = collection.ojbIterator();
157         int fetchCount = 0;
158         while(iter.hasNext())
159         {
160             fetchCount++;
161             iter.next();
162         }
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         Transaction tx = odmg.newTransaction();
174         long start = System.currentTimeMillis();
175         tx.begin();
176         // update all objects
177
for(int i = 0; i < articleCount; i++)
178         {
179             tx.lock(arr[i], Transaction.WRITE);
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