1 package org.apache.ojb.otm; 2 3 import java.util.Iterator ; 4 5 import junit.framework.TestCase; 6 import org.apache.ojb.otm.core.Transaction; 7 import org.apache.ojb.otm.lock.LockType; 8 import org.apache.ojb.broker.Identity; 9 import org.apache.ojb.broker.PerformanceArticle; 10 import org.apache.ojb.broker.PersistenceBrokerFactory; 11 import org.apache.ojb.broker.query.Criteria; 12 import org.apache.ojb.broker.query.Query; 13 import org.apache.ojb.broker.query.QueryByCriteria; 14 import org.apache.ojb.broker.util.logging.Logger; 15 import org.apache.ojb.broker.util.logging.LoggerFactory; 16 17 22 public class PerformanceTest extends TestCase 23 { 24 private Logger logger = LoggerFactory.getLogger("performance"); 25 26 private TestKit _kit; 27 28 private OTMConnection _conn; 29 30 private PerformanceArticle[] arr; 31 34 static int articleCount = 10000; 35 38 static int iterations = 2; 39 42 int offsetId = 123456; 43 44 public PerformanceTest(String name) 45 { 46 super(name); 47 } 48 49 55 public static void main(String [] args) 56 { 57 if (args.length > 0) 58 { 59 articleCount = Integer.parseInt(args[0]); 60 } 61 if (args.length > 1) 62 { 63 iterations = Integer.parseInt(args[1]); 64 } 65 String [] arr = {PerformanceTest.class.getName()}; 66 junit.textui.TestRunner.main(arr); 67 } 68 69 public void setUp() 70 { 71 _kit = TestKit.getTestInstance(); 72 _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey()); 73 arr = new PerformanceArticle[articleCount]; 74 for (int i = 0; i < articleCount; i++) 75 { 76 PerformanceArticle a = createArticle(offsetId + i); 77 arr[i] = a; 78 } 79 } 80 81 public void tearDown() 82 { 83 _conn.close(); 84 _conn = null; 85 } 86 87 92 private PerformanceArticle createArticle(int id) 93 { 94 PerformanceArticle a = new PerformanceArticle(); 95 a.setArticleId(id); 96 a.setArticleName("New Performance Article " + id); 97 a.setMinimumStock(100); 98 a.setOrderedUnits(17); 99 a.setPrice(0.45); 100 a.setProductGroupId(1); 101 a.setStock(234); 102 a.setSupplierId(4); 103 a.setUnit("bottle"); 104 return a; 105 } 106 107 110 protected void deleteArticles() throws Exception 111 { 112 long start = System.currentTimeMillis(); 113 Transaction tx = _kit.getTransaction(_conn); 114 tx.begin(); 115 for (int i = 0; i < articleCount; i++) 116 { 117 _conn.deletePersistent(arr[i]); 118 } 119 tx.commit(); 120 long stop = System.currentTimeMillis(); 121 logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec"); 122 } 123 124 131 protected void insertNewArticles() throws Exception 132 { 133 long start = System.currentTimeMillis(); 134 Transaction tx = _kit.getTransaction(_conn); 135 tx.begin(); 136 for (int i = 0; i < articleCount; i++) 137 { 138 _conn.makePersistent(arr[i]); 139 } 140 tx.commit(); 141 long stop = System.currentTimeMillis(); 142 logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec"); 143 } 144 145 150 protected void readArticles() throws Exception 151 { 152 long start = System.currentTimeMillis(); 153 Transaction tx = _kit.getTransaction(_conn); 154 tx.begin(); 155 for (int i = 0; i < articleCount; i++) 156 { 157 Object [] pks = {new Integer (offsetId + i)}; 158 Identity oid = new Identity(PerformanceArticle.class, PerformanceArticle.class,pks); 159 PerformanceArticle a = (PerformanceArticle) _conn.getObjectByIdentity(oid, LockType.NO_LOCK); 160 } 161 tx.commit(); 162 long stop = System.currentTimeMillis(); 163 logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec"); 164 } 165 166 173 protected void readArticlesByCursor() throws Exception 174 { 175 _conn.invalidateAll(); 177 178 Transaction tx = _kit.getTransaction(_conn); 179 Criteria c = new Criteria(); 180 c.addBetween("articleId", new Integer (offsetId), new Integer (offsetId + articleCount)); 181 Query q = new QueryByCriteria(PerformanceArticle.class, c); 182 long start = System.currentTimeMillis(); 183 tx.begin(); 184 Iterator iter = _conn.getIteratorByQuery(q, LockType.NO_LOCK); 185 int fetchCount = 0; 186 while (iter.hasNext()) 187 { 188 fetchCount++; 189 PerformanceArticle a = (PerformanceArticle) iter.next(); 190 } 191 tx.commit(); 192 long stop = System.currentTimeMillis(); 193 logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec"); 194 } 195 196 200 protected void updateExistingArticles() throws Exception 201 { 202 long start = System.currentTimeMillis(); 203 Transaction tx = _kit.getTransaction(_conn); 204 tx.begin(); 205 for (int i = 0; i < articleCount; i++) 207 { 208 _conn.lockForWrite(arr[i]); 209 arr[i].setPrice(arr[i].getPrice() * 1.95583); 210 } 211 tx.commit(); 212 long stop = System.currentTimeMillis(); 213 logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec"); 214 } 215 216 227 public void testBenchmark() 228 { 229 try 230 { 231 logger.info("Test for OTM-api"); 232 for (int i = 0; i < iterations; i++) 233 { 234 logger.info(""); 235 insertNewArticles(); 237 updateExistingArticles(); 239 _conn.invalidateAll(); 241 242 readArticles(); 243 readArticles(); 245 readArticlesByCursor(); 247 deleteArticles(); 249 } 250 } 251 catch (Throwable t) 252 { 253 logger.error(t); 254 fail(t.getMessage()); 255 } 256 } 257 } 258 259 | Popular Tags |