1 package org.apache.ojb.odmg; 2 3 import java.util.Iterator ; 4 5 import junit.framework.TestCase; 6 import org.apache.ojb.broker.ManageableCollection; 7 import org.apache.ojb.broker.PBFactoryException; 8 import org.apache.ojb.broker.PerformanceArticle; 9 import org.apache.ojb.broker.PersistenceBroker; 10 import org.apache.ojb.broker.PersistenceBrokerFactory; 11 import org.apache.ojb.broker.TestHelper; 12 import org.apache.ojb.broker.util.logging.Logger; 13 import org.apache.ojb.broker.util.logging.LoggerFactory; 14 import org.odmg.Database; 15 import org.odmg.Implementation; 16 import org.odmg.OQLQuery; 17 import org.odmg.Transaction; 18 19 24 public class PerformanceTest extends TestCase 25 { 26 private Logger logger = LoggerFactory.getLogger("performance"); 27 28 private PerformanceArticle[] arr; 29 private String databaseName; 30 33 static int articleCount = 10000; 34 37 static int iterations = 2; 38 41 int offsetId = 123456; 42 43 public PerformanceTest(String name) 44 { 45 super(name); 46 } 47 48 54 public static void main(String [] args) 55 { 56 if (args.length > 0) 57 { 58 articleCount = Integer.parseInt(args[0]); 59 } 60 if (args.length > 1) 61 { 62 iterations = Integer.parseInt(args[1]); 63 } 64 String [] arr = {PerformanceTest.class.getName()}; 65 junit.textui.TestRunner.main(arr); 66 } 67 68 72 public void setUp() throws PBFactoryException 73 { 74 databaseName = TestHelper.DEF_DATABASE_NAME; 75 arr = new PerformanceArticle[articleCount]; 76 for (int i = 0; i < articleCount; i++) 77 { 78 PerformanceArticle a = createArticle(offsetId + i); 79 arr[i] = a; 80 } 81 } 82 83 87 public void tearDown() 88 { 89 databaseName = null; 90 } 91 92 97 private PerformanceArticle createArticle(int id) 98 { 99 PerformanceArticle a = new PerformanceArticle(); 100 a.setArticleId(id); 101 a.setArticleName("New Performance Article " + id); 102 a.setMinimumStock(100); 103 a.setOrderedUnits(17); 104 a.setPrice(0.45); 105 a.setProductGroupId(1); 106 a.setStock(234); 107 a.setSupplierId(4); 108 a.setUnit("bottle"); 109 return a; 110 } 111 112 115 protected void deleteArticles() throws Exception 116 { 117 Implementation odmg = OJB.getInstance(); 118 Database db = odmg.newDatabase(); 119 db.open(databaseName, Database.OPEN_READ_WRITE); 120 Transaction tx = odmg.newTransaction(); 121 long start = System.currentTimeMillis(); 122 tx.begin(); 123 for (int i = 0; i < articleCount; i++) 124 { 125 db.deletePersistent(arr[i]); 126 } 127 tx.commit(); 128 long stop = System.currentTimeMillis(); 129 db.close(); 130 logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec"); 131 } 132 133 140 protected void insertNewArticles() throws Exception 141 { 142 Implementation odmg = OJB.getInstance(); 143 Database db = odmg.newDatabase(); 144 db.open(databaseName, Database.OPEN_READ_WRITE); 145 Transaction tx = odmg.newTransaction(); 146 long start = System.currentTimeMillis(); 147 tx.begin(); 148 for (int i = 0; i < articleCount; i++) 149 { 150 db.makePersistent(arr[i]); 151 } 152 tx.commit(); 153 long stop = System.currentTimeMillis(); 154 db.close(); 155 logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec"); 156 } 157 158 163 protected void readArticles() throws Exception 164 { 165 Implementation odmg = OJB.getInstance(); 166 Database db = odmg.newDatabase(); 167 long start = System.currentTimeMillis(); 168 db.open(databaseName, Database.OPEN_READ_ONLY); 169 Transaction tx = odmg.newTransaction(); 170 tx.begin(); 171 for (int i = 0; i < articleCount; i++) 172 { 173 OQLQuery query = odmg.newOQLQuery(); 174 String sql = "select allArticles from " + PerformanceArticle.class.getName() + " where articleId=" + i; 175 query.create(sql); 176 query.execute(); 177 } 178 tx.commit(); 179 long stop = System.currentTimeMillis(); 180 db.close(); 181 logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec"); 182 } 183 184 191 protected void readArticlesByCursor() throws Exception 192 { 193 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 195 broker.clearCache(); 196 broker.close(); 197 198 Implementation odmg = OJB.getInstance(); 199 Database db = odmg.newDatabase(); 200 db.open(databaseName, Database.OPEN_READ_WRITE); 201 OQLQuery query = odmg.newOQLQuery(); 202 String sql = "select allArticles from " + PerformanceArticle.class.getName() + " where articleId between " + new Integer (offsetId) + " and " + new Integer (offsetId + articleCount); 203 query.create(sql); 204 long start = System.currentTimeMillis(); 205 ManageableCollection collection = (ManageableCollection) query.execute(); 206 Iterator iter = collection.ojbIterator(); 207 int fetchCount = 0; 208 while (iter.hasNext()) 209 { 210 fetchCount++; 211 PerformanceArticle a = (PerformanceArticle) iter.next(); 212 } 213 long stop = System.currentTimeMillis(); 214 db.close(); 215 logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec"); 216 } 217 218 222 protected void updateExistingArticles() throws Exception 223 { 224 Implementation odmg = OJB.getInstance(); 225 Database db = odmg.newDatabase(); 226 db.open(databaseName, Database.OPEN_READ_WRITE); 227 Transaction tx = odmg.newTransaction(); 228 long start = System.currentTimeMillis(); 229 tx.begin(); 230 for (int i = 0; i < articleCount; i++) 232 { 233 tx.lock(arr[i], Transaction.WRITE); 234 arr[i].setPrice(arr[i].getPrice() * 1.95583); 235 } 236 tx.commit(); 237 long stop = System.currentTimeMillis(); 238 logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec"); 239 } 240 241 252 public void testBenchmark() 253 { 254 try 255 { 256 logger.info("Test for ODMG-api"); 257 for (int i = 0; i < iterations; i++) 258 { 259 logger.info(""); 260 insertNewArticles(); 262 updateExistingArticles(); 264 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 266 broker.clearCache(); 267 broker.close(); 268 269 readArticles(); 270 readArticles(); 272 readArticlesByCursor(); 274 deleteArticles(); 276 } 277 } 278 catch (Throwable t) 279 { 280 logger.error(t); 281 fail(t.getMessage()); 282 } 283 } 284 } 285 286 | Popular Tags |