1 package org.apache.ojb.compare; 2 3 import java.sql.Connection ; 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 7 import org.apache.ojb.broker.metadata.ClassDescriptor; 8 import org.apache.ojb.broker.query.Criteria; 9 import org.apache.ojb.broker.query.Query; 10 import org.apache.ojb.broker.query.QueryByCriteria; 11 12 18 public class PerformanceJdbcTest extends PerformanceBaseTest 19 { 20 25 public PerformanceJdbcTest(String name) 26 { 27 super(name); 28 setNameOfTest("Test for JDBC"); 29 } 30 31 38 public static void main(String [] args) 39 { 40 if(args.length > 0) 41 { 42 articleCount = Integer.parseInt(args[0]); 43 } 44 if(args.length > 1) 45 { 46 iterations = Integer.parseInt(args[1]); 47 } 48 49 String [] arr = {PerformanceJdbcTest.class.getName()}; 50 junit.textui.TestRunner.main(arr); 51 } 52 53 public void testBenchmark() throws Exception 54 { 55 super.testBenchmark(); 56 } 57 58 61 protected void deleteArticles() throws Exception 62 { 63 Connection conn = getConnection(); 64 65 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 68 String sql = broker.serviceSqlGenerator().getPreparedDeleteStatement(cld).getStatement(); 69 70 logger.debug("delete stmt: " + sql); 71 72 long start = System.currentTimeMillis(); 73 try 74 { 75 conn.setAutoCommit(false); 76 PreparedStatement stmt = conn.prepareStatement(sql); 77 for(int i = 0; i < articleCount; i++) 78 { 79 PerformanceArticle a = arr[i]; 80 stmt.setInt(1, a.articleId.intValue()); 81 stmt.execute(); 82 } 83 conn.commit(); 84 } 85 catch(Throwable t) 86 { 87 logger.error(t); 88 fail(t.getMessage()); 89 } 90 finally 91 { 92 if(conn != null) 93 returnConnection(conn); 94 } 95 long stop = System.currentTimeMillis(); 96 logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec"); 97 } 98 99 103 protected void insertNewArticles() throws Exception 104 { 105 Connection conn = getConnection(); 106 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 109 String sql = broker.serviceSqlGenerator().getPreparedInsertStatement(cld).getStatement(); 110 111 logger.debug("insert stmt: " + sql); 112 113 long start = System.currentTimeMillis(); 114 try 115 { 116 conn.setAutoCommit(false); 117 PreparedStatement stmt = conn.prepareStatement(sql); 118 119 for(int i = 0; i < articleCount; i++) 120 { 121 PerformanceArticle a = arr[i]; 122 123 stmt.setInt(1, a.articleId.intValue()); 124 stmt.setString(2, a.articleName); 125 stmt.setInt(3, a.supplierId); 126 stmt.setInt(4, a.productGroupId); 127 stmt.setString(5, a.unit); 128 stmt.setDouble(6, a.price); 129 stmt.setInt(7, a.stock); 130 stmt.setInt(8, a.orderedUnits); 131 stmt.setInt(9, a.minimumStock); 132 133 stmt.execute(); 134 } 135 conn.commit(); 136 } 137 catch(Throwable t) 138 { 139 logger.error(t); 140 fail(t.getMessage()); 141 } 142 finally 143 { 144 if(conn != null) 145 returnConnection(conn); 146 } 147 long stop = System.currentTimeMillis(); 148 logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec"); 149 150 } 151 152 protected void clearTable() throws Exception 153 { 154 Connection conn = getConnection(); 155 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 156 String table = cld.getFullTableName(); 157 String column = cld.getFieldDescriptorByName("articleId").getColumnName(); 158 String sql = "DELETE FROM " + table + " WHERE " + column + " >= " + offsetId; 159 PreparedStatement stmt = conn.prepareStatement(sql); 160 stmt.execute(); 161 returnConnection(conn); 162 } 163 164 169 protected void readArticles() throws Exception 170 { 171 Connection conn = getConnection(); 172 173 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 176 String sql = broker.serviceSqlGenerator().getPreparedSelectByPkStatement(cld).getStatement(); 177 logger.debug("select stmt: " + sql); 178 179 String colId = cld.getFieldDescriptorByName("articleId").getColumnName(); 180 String colName = cld.getFieldDescriptorByName("articleName").getColumnName(); 181 String colSupplier = cld.getFieldDescriptorByName("supplierId").getColumnName(); 182 String colGroup = cld.getFieldDescriptorByName("productGroupId").getColumnName(); 183 String colUnit = cld.getFieldDescriptorByName("unit").getColumnName(); 184 String colPrice = cld.getFieldDescriptorByName("price").getColumnName(); 185 String colStock = cld.getFieldDescriptorByName("stock").getColumnName(); 186 String colOrdered = cld.getFieldDescriptorByName("orderedUnits").getColumnName(); 187 String colMin = cld.getFieldDescriptorByName("minimumStock").getColumnName(); 188 189 long start = System.currentTimeMillis(); 190 try 191 { 192 conn.setAutoCommit(false); 193 PreparedStatement stmt = conn.prepareStatement(sql); 194 for(int i = 0; i < articleCount; i++) 195 { 196 stmt.setInt(1, offsetId + i); 197 ResultSet rs = stmt.executeQuery(); 198 rs.next(); 199 200 PerformanceArticle a = new PerformanceArticle(); 201 a.articleId = new Integer (rs.getInt(colId)); 202 a.articleName = rs.getString(colName); 203 a.supplierId = rs.getInt(colSupplier); 204 a.productGroupId = rs.getInt(colGroup); 205 a.unit = rs.getString(colUnit); 206 a.price = rs.getFloat(colPrice); 207 a.stock = rs.getInt(colStock); 208 a.orderedUnits = rs.getInt(colOrdered); 209 a.minimumStock = rs.getInt(colMin); 210 } 211 } 212 catch(Throwable t) 213 { 214 logger.error(t); 215 fail(t.getMessage()); 216 } 217 finally 218 { 219 if(conn != null) 220 returnConnection(conn); 221 } 222 223 long stop = System.currentTimeMillis(); 224 logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec"); 225 226 } 227 228 235 protected void readArticlesByCursor() throws Exception 236 { 237 Connection conn = getConnection(); 238 239 Criteria c = new Criteria(); 240 c.addBetween("articleId", new Integer (offsetId), new Integer (offsetId + articleCount)); 241 Query query = new QueryByCriteria(PerformanceArticle.class, c); 242 243 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 246 String sql = broker.serviceSqlGenerator().getPreparedSelectStatement(query, cld).getStatement(); 247 248 logger.debug("select stmt: " + sql); 249 250 String colId = cld.getFieldDescriptorByName("articleId").getColumnName(); 251 String colName = cld.getFieldDescriptorByName("articleName").getColumnName(); 252 String colSupplier = cld.getFieldDescriptorByName("supplierId").getColumnName(); 253 String colGroup = cld.getFieldDescriptorByName("productGroupId").getColumnName(); 254 String colUnit = cld.getFieldDescriptorByName("unit").getColumnName(); 255 String colPrice = cld.getFieldDescriptorByName("price").getColumnName(); 256 String colStock = cld.getFieldDescriptorByName("stock").getColumnName(); 257 String colOrdered = cld.getFieldDescriptorByName("orderedUnits").getColumnName(); 258 String colMin = cld.getFieldDescriptorByName("minimumStock").getColumnName(); 259 260 261 int fetchCount = 0; 262 long start = System.currentTimeMillis(); 263 try 264 { 265 conn.setAutoCommit(false); 266 PreparedStatement stmt = conn.prepareStatement(sql); 267 stmt.setInt(1, offsetId); 268 stmt.setInt(2, offsetId + articleCount); 269 ResultSet rs = stmt.executeQuery(); 270 while(rs.next()) 271 { 272 fetchCount++; 273 274 PerformanceArticle a = new PerformanceArticle(); 275 a.articleId = new Integer (rs.getInt(colId)); 276 a.articleName = rs.getString(colName); 277 a.supplierId = rs.getInt(colSupplier); 278 a.productGroupId = rs.getInt(colGroup); 279 a.unit = rs.getString(colUnit); 280 a.price = rs.getFloat(colPrice); 281 a.stock = rs.getInt(colStock); 282 a.orderedUnits = rs.getInt(colOrdered); 283 a.minimumStock = rs.getInt(colMin); 284 } 285 } 286 catch(Throwable t) 287 { 288 logger.error(t); 289 fail(t.getMessage()); 290 } 291 finally 292 { 293 if(conn != null) 294 returnConnection(conn); 295 } 296 297 long stop = System.currentTimeMillis(); 298 logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec"); 299 300 } 301 302 306 protected void updateExistingArticles() throws Exception 307 { 308 Connection conn = getConnection(); 309 310 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 313 String sql = broker.serviceSqlGenerator().getPreparedUpdateStatement(cld).getStatement(); 314 logger.debug("update stmt: " + sql); 315 316 for(int i = 0; i < articleCount; i++) 318 { 319 arr[i].setPrice(arr[i].getPrice() * 1.95583); 320 } 321 322 long start = System.currentTimeMillis(); 323 try 324 { 325 conn.setAutoCommit(false); 326 PreparedStatement stmt = conn.prepareStatement(sql); 327 for(int i = 0; i < articleCount; i++) 328 { 329 PerformanceArticle a = arr[i]; 330 stmt.setString(1, a.articleName); 331 stmt.setInt(2, a.supplierId); 332 stmt.setInt(3, a.productGroupId); 333 stmt.setString(4, a.unit); 334 stmt.setDouble(5, a.price); 335 stmt.setInt(6, a.stock); 336 stmt.setInt(7, a.orderedUnits); 337 stmt.setInt(8, a.minimumStock); 338 stmt.setInt(9, a.articleId.intValue()); 339 stmt.execute(); 340 } 341 conn.commit(); 342 } 343 catch(Throwable t) 344 { 345 logger.error(t); 346 fail(t.getMessage()); 347 } 348 finally 349 { 350 if(conn != null) 351 returnConnection(conn); 352 } 353 long stop = System.currentTimeMillis(); 354 logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec"); 355 } 356 } 357 | Popular Tags |