1 package org.apache.ojb.broker; 2 3 import junit.framework.TestCase; 4 5 import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectAccessImpl; 6 import org.apache.ojb.broker.query.Criteria; 7 import org.apache.ojb.broker.query.Query; 8 import org.apache.ojb.broker.query.QueryByCriteria; 9 import org.apache.ojb.broker.util.ObjectModificationDefaultImpl; 10 import org.apache.ojb.broker.util.configuration.impl.OjbConfiguration; 11 import org.apache.ojb.broker.util.logging.Logger; 12 import org.apache.ojb.broker.util.logging.LoggerFactory; 13 14 import java.util.Iterator ; 15 16 21 public class PerformanceTest extends TestCase 22 { 23 private Logger logger = LoggerFactory.getLogger("performance"); 24 27 static int articleCount = 10000; 28 29 32 static int iterations = 2; 33 34 37 int offsetId = 123456; 38 PersistenceBroker broker; 39 private PerformanceArticle[] arr; 40 41 public PerformanceTest(String name) 42 { 43 super(name); 44 } 45 46 52 public static void main(String [] args) 53 { 54 if (args.length > 0) 55 { 56 articleCount = Integer.parseInt(args[0]); 57 } 58 if (args.length > 1) 59 { 60 iterations = Integer.parseInt(args[1]); 61 } 62 63 String [] arr = {PerformanceTest.class.getName()}; 64 junit.textui.TestRunner.main(arr); 65 } 66 67 70 public void setUp() 71 { 72 try 73 { 74 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 76 OjbConfiguration conf = (OjbConfiguration) PersistenceBrokerFactory. 78 getConfigurator().getConfigurationFor(null); 79 conf.setPersistentFieldClass(PersistentFieldDirectAccessImpl.class); 80 } 81 catch (PBFactoryException e) 82 { 83 } 84 85 arr = new PerformanceArticle[articleCount]; 86 for (int i = 0; i < articleCount; i++) 87 { 88 PerformanceArticle a = createArticle(offsetId + i); 89 arr[i] = a; 90 } 91 } 92 93 96 public void tearDown() 97 { 98 broker.close(); 99 } 100 101 106 private PerformanceArticle createArticle(int id) 107 { 108 PerformanceArticle a = new PerformanceArticle(); 109 a.setArticleId(id); 110 a.setArticleName("New Performance Article " + id); 111 a.setMinimumStock(100); 112 a.setOrderedUnits(17); 113 a.setPrice(0.45); 114 a.setProductGroupId(1); 115 a.setStock(234); 116 a.setSupplierId(4); 117 a.setUnit("bottle"); 118 return a; 119 } 120 121 122 125 protected void deleteArticles() throws PersistenceBrokerException 126 { 127 long start = System.currentTimeMillis(); 128 broker.beginTransaction(); 129 for (int i = 0; i < articleCount; i++) 130 { 131 broker.delete(arr[i]); 132 } 133 broker.commitTransaction(); 134 long stop = System.currentTimeMillis(); 135 logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec"); 136 } 137 138 139 143 protected void insertNewArticles() throws PersistenceBrokerException 144 { 145 ObjectModificationDefaultImpl needsInsert = new ObjectModificationDefaultImpl(); 146 needsInsert.setNeedsInsert(true); 147 148 long start = System.currentTimeMillis(); 149 broker.beginTransaction(); 150 for (int i = 0; i < articleCount; i++) 151 { 152 broker.store(arr[i], needsInsert); 153 } 154 broker.commitTransaction(); 155 long stop = System.currentTimeMillis(); 156 logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec"); 157 158 } 159 160 165 protected void readArticles() throws PersistenceBrokerException 166 { 167 long start = System.currentTimeMillis(); 168 broker.beginTransaction(); 169 for (int i = 0; i < articleCount; i++) 170 { 171 Object [] pks = {new Integer (offsetId + i)}; 172 Identity oid = new Identity(PerformanceArticle.class, PerformanceArticle.class,pks); 173 174 PerformanceArticle a = (PerformanceArticle) broker.getObjectByIdentity(oid); 175 } 176 broker.commitTransaction(); 177 long stop = System.currentTimeMillis(); 178 logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec"); 179 180 } 181 182 189 protected void readArticlesByCursor() throws PersistenceBrokerException 190 191 { 192 broker.clearCache(); 193 Criteria c = new Criteria(); 194 c.addBetween("articleId", new Integer (offsetId), new Integer (offsetId + articleCount)); 195 Query q = new QueryByCriteria(PerformanceArticle.class, c); 196 197 long start = System.currentTimeMillis(); 198 Iterator iter = broker.getIteratorByQuery(q); 199 int fetchCount = 0; 200 while (iter.hasNext()) 201 { 202 fetchCount++; 203 PerformanceArticle a = (PerformanceArticle) iter.next(); 204 } 205 long stop = System.currentTimeMillis(); 206 logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec"); 207 208 } 209 210 214 protected void updateExistingArticles() throws PersistenceBrokerException 215 { 216 217 for (int i = 0; i < articleCount; i++) 219 { 220 arr[i].setPrice(arr[i].getPrice() * 1.95583); 221 } 222 223 ObjectModificationDefaultImpl needsUpdate = new ObjectModificationDefaultImpl(); 225 needsUpdate.setNeedsUpdate(true); 226 227 long start = System.currentTimeMillis(); 228 broker.beginTransaction(); 229 for (int i = 0; i < articleCount; i++) 230 { 231 broker.store(arr[i], needsUpdate); 232 } 233 broker.commitTransaction(); 234 long stop = System.currentTimeMillis(); 235 logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec"); 236 237 } 238 239 250 public void testBenchmark() 251 { 252 try 253 { 254 logger.info("Test for PB-api"); 255 for (int i = 0; i < iterations; i++) 256 { 257 logger.info(""); 258 259 insertNewArticles(); 261 262 updateExistingArticles(); 264 265 broker.clearCache(); 267 readArticles(); 268 269 readArticles(); 271 272 readArticlesByCursor(); 274 275 deleteArticles(); 277 } 278 } 279 catch (Throwable t) 280 { 281 logger.error(t); 282 fail(t.getMessage()); 283 } 284 } 285 286 } 287 | Popular Tags |