1 package org.apache.ojb.broker; 2 3 import junit.framework.TestCase; 4 import org.apache.ojb.broker.accesslayer.ConnectionFactory; 5 import org.apache.ojb.broker.accesslayer.ConnectionFactoryFactory; 6 import org.apache.ojb.broker.metadata.ClassDescriptor; 7 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; 8 import org.apache.ojb.broker.metadata.MetadataManager; 9 import org.apache.ojb.broker.query.Criteria; 10 import org.apache.ojb.broker.query.Query; 11 import org.apache.ojb.broker.query.QueryByCriteria; 12 import org.apache.ojb.broker.util.logging.Logger; 13 import org.apache.ojb.broker.util.logging.LoggerFactory; 14 15 import java.sql.Connection ; 16 import java.sql.PreparedStatement ; 17 import java.sql.ResultSet ; 18 19 25 public class PerformanceJdbcReferenceTest extends TestCase 26 { 27 private Logger logger = LoggerFactory.getLogger("performance");; 28 29 32 static int articleCount = 10000; 33 34 37 static int iterations = 2; 38 39 42 int offsetId = 10000; 43 PersistenceBroker broker; 44 private PerformanceArticle[] arr; 45 46 50 public PerformanceJdbcReferenceTest(String name) 51 52 { 53 super(name); 54 } 55 56 62 public static void main(String [] args) 63 { 64 if (args.length > 0) 65 { 66 articleCount = Integer.parseInt(args[0]); 67 } 68 if (args.length > 1) 69 { 70 iterations = Integer.parseInt(args[1]); 71 } 72 73 String [] arr = {PerformanceJdbcReferenceTest.class.getName()}; 74 junit.textui.TestRunner.main(arr); 75 } 76 77 80 public void setUp() throws Exception 81 { 82 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 83 84 arr = new PerformanceArticle[articleCount]; 85 for (int i = 0; i < articleCount; i++) 86 { 87 PerformanceArticle a = createArticle(offsetId + i); 88 arr[i] = a; 89 } 90 } 91 92 95 public void tearDown() 96 { 97 broker.close(); 98 } 99 100 105 private PerformanceArticle createArticle(int id) 106 { 107 PerformanceArticle a = new PerformanceArticle(); 108 a.setArticleId(id); 109 a.setArticleName("New Performance Article " + id); 110 a.setMinimumStock(100); 111 a.setOrderedUnits(17); 112 a.setPrice(100.0); 113 a.setProductGroupId(1); 114 a.setStock(234); 115 a.setSupplierId(4); 116 a.setUnit("bottle"); 117 return a; 118 } 119 120 125 private Connection getConnection() throws Exception 126 { 127 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 130 JdbcConnectionDescriptor jcd = MetadataManager.getInstance().connectionRepository() 131 .getDescriptor(TestHelper.DEF_KEY); 132 ConnectionFactory cf = ConnectionFactoryFactory.getInstance().createConnectionFactory(); 133 Connection conn = cf.lookupConnection(jcd); 134 return conn; 135 } 136 137 private void returnConnection(Connection conn) throws Exception 138 { 139 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 142 JdbcConnectionDescriptor jcd = MetadataManager.getInstance().connectionRepository() 143 .getDescriptor(TestHelper.DEF_KEY); 144 ConnectionFactory cf = ConnectionFactoryFactory.getInstance().createConnectionFactory(); 145 cf.releaseConnection(jcd, conn); 146 } 147 148 151 protected void deleteArticles() throws Exception 152 { 153 Connection conn = getConnection(); 154 155 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 158 String sql = broker.serviceSqlGenerator().getPreparedDeleteStatement(cld); 159 160 logger.debug("delete stmt: " + sql); 161 162 long start = System.currentTimeMillis(); 163 try 164 { 165 conn.setAutoCommit(false); 166 PreparedStatement stmt = conn.prepareStatement(sql); 167 for (int i = 0; i < articleCount; i++) 168 { 169 PerformanceArticle a = arr[i]; 170 stmt.setInt(1, a.articleId); 171 stmt.execute(); 172 } 173 conn.commit(); 174 } 175 catch (Throwable t) 176 { 177 logger.error(t); 178 fail(t.getMessage()); 179 } 180 finally 181 { 182 if (conn != null) 183 returnConnection(conn); 184 } 185 long stop = System.currentTimeMillis(); 186 logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec"); 187 } 188 189 193 protected void insertNewArticles() throws Exception 194 { 195 clearTable(); 196 197 Connection conn = getConnection(); 198 199 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 202 String sql = broker.serviceSqlGenerator().getPreparedInsertStatement(cld); 203 204 logger.debug("insert stmt: " + sql); 205 206 long start = System.currentTimeMillis(); 207 try 208 { 209 conn.setAutoCommit(false); 210 PreparedStatement stmt = conn.prepareStatement(sql); 211 212 for (int i = 0; i < articleCount; i++) 213 { 214 PerformanceArticle a = arr[i]; 215 216 stmt.setInt(1, a.articleId); 217 stmt.setString(2, a.articleName); 218 stmt.setInt(3, a.supplierId); 219 stmt.setInt(4, a.productGroupId); 220 stmt.setString(5, a.unit); 221 stmt.setDouble(6, a.price); 222 stmt.setInt(7, a.stock); 223 stmt.setInt(8, a.orderedUnits); 224 stmt.setInt(9, a.minimumStock); 225 226 stmt.execute(); 227 } 228 conn.commit(); 229 } 230 catch (Throwable t) 231 { 232 logger.error(t); 233 fail(t.getMessage()); 234 } 235 finally 236 { 237 if (conn != null) 238 returnConnection(conn); 239 } 240 long stop = System.currentTimeMillis(); 241 logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec"); 242 243 } 244 245 protected void clearTable() throws Exception 246 { 247 Connection conn = getConnection(); 248 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 249 String table = cld.getFullTableName(); 250 String column = cld.getFieldDescriptorByName("articleId").getColumnName(); 251 String sql = "DELETE FROM " + table + " WHERE " + column + " >= " + offsetId; 252 PreparedStatement stmt = conn.prepareStatement(sql); 253 stmt.execute(); 254 returnConnection(conn); 255 } 256 257 262 protected void readArticles() throws Exception 263 { 264 Connection conn = getConnection(); 265 266 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 269 String sql = broker.serviceSqlGenerator().getPreparedSelectByPkStatement(cld); 270 logger.debug("select stmt: " + sql); 271 272 String colId = cld.getFieldDescriptorByName("articleId").getColumnName(); 273 String colName = cld.getFieldDescriptorByName("articleName").getColumnName(); 274 String colSupplier = cld.getFieldDescriptorByName("supplierId").getColumnName(); 275 String colGroup = cld.getFieldDescriptorByName("productGroupId").getColumnName(); 276 String colUnit = cld.getFieldDescriptorByName("unit").getColumnName(); 277 String colPrice = cld.getFieldDescriptorByName("price").getColumnName(); 278 String colStock = cld.getFieldDescriptorByName("stock").getColumnName(); 279 String colOrdered = cld.getFieldDescriptorByName("orderedUnits").getColumnName(); 280 String colMin = cld.getFieldDescriptorByName("minimumStock").getColumnName(); 281 282 long start = System.currentTimeMillis(); 283 try 284 { 285 conn.setAutoCommit(false); 286 PreparedStatement stmt = conn.prepareStatement(sql); 287 for (int i = 0; i < articleCount; i++) 288 { 289 stmt.setInt(1, offsetId + i); 290 ResultSet rs = stmt.executeQuery(); 291 rs.next(); 292 293 PerformanceArticle a = new PerformanceArticle(); 294 a.articleId = rs.getInt(colId); 295 a.articleName = rs.getString(colName); 296 a.supplierId = rs.getInt(colSupplier); 297 a.productGroupId = rs.getInt(colGroup); 298 a.unit = rs.getString(colUnit); 299 a.price = rs.getFloat(colPrice); 300 a.stock = rs.getInt(colStock); 301 a.orderedUnits = rs.getInt(colOrdered); 302 a.minimumStock = rs.getInt(colMin); 303 } 304 } 305 catch (Throwable t) 306 { 307 logger.error(t); 308 fail(t.getMessage()); 309 } 310 finally 311 { 312 if (conn != null) 313 returnConnection(conn); 314 } 315 316 long stop = System.currentTimeMillis(); 317 logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec"); 318 319 } 320 321 328 protected void readArticlesByCursor() throws Exception 329 { 330 Connection conn = getConnection(); 331 332 Criteria c = new Criteria(); 333 c.addBetween("articleId", new Integer (offsetId), new Integer (offsetId + articleCount)); 334 Query query = new QueryByCriteria(PerformanceArticle.class, c); 335 336 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 339 String sql = broker.serviceSqlGenerator().getPreparedSelectStatement(query, cld); 340 341 logger.debug("select stmt: " + sql); 342 343 String colId = cld.getFieldDescriptorByName("articleId").getColumnName(); 344 String colName = cld.getFieldDescriptorByName("articleName").getColumnName(); 345 String colSupplier = cld.getFieldDescriptorByName("supplierId").getColumnName(); 346 String colGroup = cld.getFieldDescriptorByName("productGroupId").getColumnName(); 347 String colUnit = cld.getFieldDescriptorByName("unit").getColumnName(); 348 String colPrice = cld.getFieldDescriptorByName("price").getColumnName(); 349 String colStock = cld.getFieldDescriptorByName("stock").getColumnName(); 350 String colOrdered = cld.getFieldDescriptorByName("orderedUnits").getColumnName(); 351 String colMin = cld.getFieldDescriptorByName("minimumStock").getColumnName(); 352 353 354 int fetchCount = 0; 355 long start = System.currentTimeMillis(); 356 try 357 { 358 conn.setAutoCommit(false); 359 PreparedStatement stmt = conn.prepareStatement(sql); 360 stmt.setInt(1, offsetId); 361 stmt.setInt(2, offsetId + articleCount); 362 ResultSet rs = stmt.executeQuery(); 363 while (rs.next()) 364 { 365 fetchCount++; 366 367 PerformanceArticle a = new PerformanceArticle(); 368 a.articleId = rs.getInt(colId); 369 a.articleName = rs.getString(colName); 370 a.supplierId = rs.getInt(colSupplier); 371 a.productGroupId = rs.getInt(colGroup); 372 a.unit = rs.getString(colUnit); 373 a.price = rs.getFloat(colPrice); 374 a.stock = rs.getInt(colStock); 375 a.orderedUnits = rs.getInt(colOrdered); 376 a.minimumStock = rs.getInt(colMin); 377 } 378 } 379 catch (Throwable t) 380 { 381 logger.error(t); 382 fail(t.getMessage()); 383 } 384 finally 385 { 386 if (conn != null) 387 returnConnection(conn); 388 } 389 390 long stop = System.currentTimeMillis(); 391 logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec"); 392 393 } 394 395 399 protected void updateExistingArticles() throws Exception 400 { 401 Connection conn = getConnection(); 402 403 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 406 String sql = broker.serviceSqlGenerator().getPreparedUpdateStatement(cld); 407 logger.debug("update stmt: " + sql); 408 409 for (int i = 0; i < articleCount; i++) 411 { 412 arr[i].setPrice(arr[i].getPrice() * 1.95583); 413 } 414 415 long start = System.currentTimeMillis(); 416 try 417 { 418 conn.setAutoCommit(false); 419 PreparedStatement stmt = conn.prepareStatement(sql); 420 for (int i = 0; i < articleCount; i++) 421 { 422 PerformanceArticle a = arr[i]; 423 stmt.setString(1, a.articleName); 424 stmt.setInt(2, a.supplierId); 425 stmt.setInt(3, a.productGroupId); 426 stmt.setString(4, a.unit); 427 stmt.setDouble(5, a.price); 428 stmt.setInt(6, a.stock); 429 stmt.setInt(7, a.orderedUnits); 430 stmt.setInt(8, a.minimumStock); 431 stmt.setInt(9, a.articleId); 432 stmt.execute(); 433 } 434 conn.commit(); 435 } 436 catch (Throwable t) 437 { 438 logger.error(t); 439 fail(t.getMessage()); 440 } 441 finally 442 { 443 if (conn != null) 444 returnConnection(conn); 445 } 446 long stop = System.currentTimeMillis(); 447 logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec"); 448 449 } 450 451 462 public void testBenchmark() 463 { 464 try 465 { 466 logger.info("Test for native JDBC"); 467 for (int i = 0; i < iterations; i++) 468 { 469 logger.info(""); 470 471 insertNewArticles(); 473 474 updateExistingArticles(); 476 477 readArticles(); 479 480 readArticles(); 481 482 readArticlesByCursor(); 484 485 deleteArticles(); 487 } 488 } 489 catch (Throwable t) 490 { 491 logger.error(t); 492 fail(t.getMessage()); 493 } 494 } 495 496 } 497 | Popular Tags |