1 package org.apache.ojb.compare; 2 3 import java.util.Iterator ; 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 21 public class PerformanceOTMTest extends PerformanceBaseTest 22 { 23 private TestKit _kit; 24 private OTMConnection _conn; 25 26 public PerformanceOTMTest(String name) 27 { 28 super(name); 29 setNameOfTest("Test for OTM-api"); 30 } 31 32 39 public static void main(String [] 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 [] arr = {PerformanceOTMTest.class.getName()}; 50 junit.textui.TestRunner.main(arr); 51 } 52 53 public void setUp() throws Exception 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 68 { 69 _conn.close(); 70 _conn = null; 71 72 super.tearDown(); 73 } 74 75 public void testBenchmark() throws Exception 76 { 77 super.testBenchmark(); 78 } 79 80 83 protected void deleteArticles() throws Exception 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 102 protected void insertNewArticles() throws Exception 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 121 protected void readArticles() throws Exception 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 [] pks = {new Integer (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 144 protected void readArticlesByCursor() throws Exception 145 { 146 _conn.invalidateAll(); 148 149 Transaction tx = _kit.getTransaction(_conn); 150 Criteria c = new Criteria(); 151 c.addBetween("articleId", new Integer (offsetId), new Integer (offsetId + articleCount)); 152 Query q = new QueryByCriteria(PerformanceArticle.class, c); 153 long start = System.currentTimeMillis(); 154 tx.begin(); 155 Iterator 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 171 protected void updateExistingArticles() throws Exception 172 { 173 long start = System.currentTimeMillis(); 174 Transaction tx = _kit.getTransaction(_conn); 175 tx.begin(); 176 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 |