1 package org.apache.ojb.compare; 2 3 import java.sql.Connection ; 4 import java.sql.PreparedStatement ; 5 import java.sql.Statement ; 6 import java.sql.SQLException ; 7 8 import org.apache.ojb.broker.metadata.ClassDescriptor; 9 import org.apache.ojb.broker.util.logging.Logger; 10 import org.apache.ojb.broker.util.logging.LoggerFactory; 11 import org.apache.ojb.broker.platforms.PlatformHsqldbImpl; 12 import org.apache.ojb.broker.platforms.Platform; 13 import org.apache.ojb.broker.query.Query; 14 import org.apache.ojb.broker.query.QueryFactory; 15 import org.apache.ojb.broker.HsqldbShutdown; 16 import org.apache.ojb.junit.PBTestCase; 17 18 23 abstract class PerformanceBaseTest extends PBTestCase 24 { 25 protected Logger logger = LoggerFactory.getLogger("performance"); 26 27 private String nameOfTest = "Test performance"; 28 29 32 protected static int articleCount = 500; 33 34 37 protected static int iterations = 2; 38 39 42 protected final static int offsetId = 12000; 43 44 protected PerformanceArticle[] arr; 45 46 51 public PerformanceBaseTest(String name) 52 { 53 super(name); 54 } 56 57 58 61 public void setUp() throws Exception 62 { 63 try 64 { 65 super.setUp(); 66 clearTable(); 67 68 arr = new PerformanceArticle[articleCount]; 69 for(int i = 0; i < articleCount; i++) 70 { 71 PerformanceArticle a = createArticle(offsetId + i); 72 arr[i] = a; 73 } 74 } 75 catch(Exception e) 76 { 77 e.printStackTrace(); 78 } 79 } 80 81 84 public void tearDown() throws Exception 85 { 86 try 87 { 88 clearTable(); 89 shutdown(); 90 super.tearDown(); 91 } 92 catch(Exception e) 93 { 94 e.printStackTrace(); 95 } 96 } 97 98 103 public void setNameOfTest(String nameOfTest) 104 { 105 this.nameOfTest = nameOfTest; 106 } 107 108 protected void clearTable() throws Exception 109 { 110 Connection conn = getConnection(); 111 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 112 String table = cld.getFullTableName(); 113 String column = cld.getFieldDescriptorByName("articleId").getColumnName(); 114 String sql = "DELETE FROM " + table + " WHERE " + column + " >= " + offsetId; 115 PreparedStatement stmt = conn.prepareStatement(sql); 116 stmt.execute(); 117 returnConnection(conn); 118 } 119 120 126 protected PerformanceArticle createArticle(int id) 127 { 128 PerformanceArticle a = new PerformanceArticle(); 129 a.setArticleId(new Integer (id)); 130 a.setArticleName("New Performance Article " + id); 131 a.setMinimumStock(100); 132 a.setOrderedUnits(17); 133 a.setPrice(100.0); 134 a.setProductGroupId(1); 135 a.setStock(234); 136 a.setSupplierId(4); 137 a.setUnit("bottle"); 138 return a; 139 } 140 141 147 protected Connection getConnection() throws Exception 148 { 149 Connection conn = broker.serviceConnectionManager().getConnection(); 151 return conn; 152 } 153 154 protected void returnConnection(Connection conn) throws Exception 155 { 156 157 } 158 159 162 protected abstract void deleteArticles() throws Exception ; 163 164 168 protected abstract void insertNewArticles() throws Exception ; 169 170 175 protected abstract void readArticles() throws Exception ; 176 177 184 protected abstract void readArticlesByCursor() throws Exception ; 185 186 190 protected abstract void updateExistingArticles() throws Exception ; 191 192 203 public void testBenchmark() throws Exception 204 { 205 try 206 { 207 logger.info(nameOfTest); 208 for(int i = 0; i < iterations; i++) 209 { 210 logger.info(""); 211 212 insertNewArticles(); 214 215 updateExistingArticles(); 217 218 readArticles(); 220 221 readArticles(); 222 223 readArticlesByCursor(); 225 226 deleteArticles(); 228 } 229 } 230 catch(Exception e) 231 { 232 logger.error(e); 233 throw e; 234 } 235 } 236 237 public void shutdown() 238 { 239 Platform platform = broker.serviceConnectionManager().getSupportedPlatform(); 240 241 if(platform instanceof PlatformHsqldbImpl) 242 { 243 Connection con = null; 244 Statement stmt = null; 245 246 try 247 { 248 con = broker.serviceConnectionManager().getConnection(); 249 stmt = con.createStatement(); 250 stmt.execute("shutdown"); 251 } 252 catch (Exception e) 253 { 254 e.printStackTrace(); 255 } 256 finally 257 { 258 try 259 { 260 if(con != null) con.close(); 261 if(stmt != null) stmt.close(); 262 263 } 264 catch (SQLException e1) 265 { 266 e1.printStackTrace(); 267 } 268 } 269 } 270 271 } 272 } 273 | Popular Tags |