1 package org.apache.ojb.performance; 2 3 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 21 26 public abstract class PerfTest implements Runnable 27 { 28 private String PREFIX_LOG = "[" + this.getClass().getName() + "] "; 29 private PerfRunner test; 30 private String objectName; 31 32 public PerfTest() 33 { 34 } 35 36 39 public abstract String testName(); 40 41 48 public abstract int articleCount(); 49 50 53 public abstract void init() throws Exception ; 54 55 58 public abstract void tearDown() throws Exception ; 59 60 64 public abstract void insertNewArticles(PerfArticle[] arr) throws Exception ; 65 66 70 public abstract void insertNewArticlesStress(PerfArticle[] arr) throws Exception ; 71 72 79 public abstract Collection readArticlesByCursor(String articleName) throws Exception ; 80 81 88 public abstract PerfArticle getArticleByIdentity(Long articleId) throws Exception ; 89 90 94 public abstract void deleteArticles(PerfArticle[] arr) throws Exception ; 95 96 100 public abstract void deleteArticlesStress(PerfArticle[] arr) throws Exception ; 101 102 106 public abstract void updateArticles(PerfArticle[] arr) throws Exception ; 107 108 112 public abstract void updateArticlesStress(PerfArticle[] arr) throws Exception ; 113 114 123 public PerfArticle newPerfArticle() 124 { 125 return new PerfArticleImpl(); 126 } 127 128 135 public String getTestObjectName() 136 { 137 if (objectName == null) 138 objectName = testName() + "_" + 139 Thread.currentThread().toString() + "_" + test.getPerfTestId(); 140 return objectName; 141 } 142 143 152 public PerfArticle getPreparedPerfArticle(String articleName) 153 { 154 PerfArticle a = newPerfArticle(); 155 a.setArticleName(articleName); 156 a.setMinimumStock(100); 157 a.setPrice(0.45); 158 a.setProductGroupId(1); 159 a.setStock(234); 160 a.setSupplierId(4); 161 a.setUnit("bottle"); 162 return a; 163 } 164 165 void setPerfRunner(PerfRunner test) 166 { 167 this.test = test; 168 } 169 170 173 public void run() 174 { 175 PerfArticle[] m_arr = new PerfArticle[PerfMain.getIterationsPerThread()]; 176 for (int i = 0; i < PerfMain.getIterationsPerThread(); i++) 177 { 178 m_arr[i] = getPreparedPerfArticle(getTestObjectName()); 179 } 180 181 try 182 { 183 long totalTime = 0; 184 long period; 185 init(); 186 187 if (PerfMain.isUseStressMode()) 189 { 190 period = System.currentTimeMillis(); 191 insertNewArticlesStress(m_arr); 192 period = System.currentTimeMillis() - period; 193 test.addTime(PerfMain.TIME_INSERT, period); 194 totalTime+=period; 195 } 196 else 197 { 198 period = System.currentTimeMillis(); 199 insertNewArticles(m_arr); 200 period = System.currentTimeMillis() - period; 201 test.addTime(PerfMain.TIME_INSERT, period); 202 totalTime+=period; 203 } 205 checkInsertResult(m_arr); 206 207 period = System.currentTimeMillis(); 209 Collection col = readArticlesByCursor(objectName); 210 period = System.currentTimeMillis() - period; 211 try 212 { 213 checkQueryResult(col, m_arr); 214 } 215 catch (Exception e) 216 { 217 test.registerException(PREFIX_LOG 218 + "(Something wrong with query result or with object insert operation) ", e); 219 } 220 test.addTime(PerfMain.TIME_FETCH, period); 221 totalTime+=period; 222 224 225 period = System.currentTimeMillis(); 227 col = readArticlesByCursor(objectName); 228 period = System.currentTimeMillis() - period; 229 try 230 { 231 checkQueryResult(col, m_arr); 232 } 233 catch (Exception e) 234 { 235 test.registerException(PREFIX_LOG 236 + "(Something wrong with query result or with object insert operation) ", e); 237 } 238 test.addTime(PerfMain.TIME_FETCH_2, period); 239 totalTime+=period; 240 242 243 period = System.currentTimeMillis(); 245 PerfArticle result; 246 for(int i = 0; i < m_arr.length; i++) 247 { 248 if(i%4==0) 249 { 250 PerfArticle perfArticle = m_arr[i]; 251 result = getArticleByIdentity(perfArticle.getArticleId()); 252 if(result == null) 253 { 254 test.registerException("Unexpected result: Get by Identity is 'null' for " 255 + PerfArticle.class.getName() + " with primary key " 256 + perfArticle.getArticleId(), null); 257 } 258 } 259 } 260 period = (System.currentTimeMillis() - period); 261 test.addTime(PerfMain.TIME_BY_IDENTITY, period); 262 totalTime+=period; 263 265 266 modifyPerfArticle(m_arr); 268 if (PerfMain.isUseStressMode()) 269 { 270 period = System.currentTimeMillis(); 271 updateArticlesStress(m_arr); 272 period = System.currentTimeMillis() - period; 273 test.addTime(PerfMain.TIME_UPDATE, period); 274 totalTime+=period; 275 } 276 else 277 { 278 period = System.currentTimeMillis(); 279 updateArticles(m_arr); 280 period = System.currentTimeMillis() - period; 281 test.addTime(PerfMain.TIME_UPDATE, period); 282 totalTime+=period; 283 } 285 286 if (PerfMain.isUseStressMode()) 288 { 289 period = System.currentTimeMillis(); 290 deleteArticlesStress(m_arr); 291 period = System.currentTimeMillis() - period; 292 test.addTime(PerfMain.TIME_DELETE, period); 293 totalTime+=period; 294 } 295 else 296 { 297 period = System.currentTimeMillis(); 298 deleteArticles(m_arr); 299 period = System.currentTimeMillis() - period; 300 test.addTime(PerfMain.TIME_DELETE, period); 301 totalTime+=period; 302 } 304 test.addTime(PerfMain.TIME_TOTAL, totalTime); 305 tearDown(); 306 } 307 catch (Exception e) 308 { 309 e.printStackTrace(); 310 test.registerException(PREFIX_LOG + "(Unexpected behaviour) ", e); 311 test.interruptThreads(); 312 } 313 } 314 315 private void modifyPerfArticle(PerfArticle[] m_arr) 316 { 317 PerfArticle article; 318 String prefix = "updated_"; 319 for (int i = 0; i < m_arr.length; i++) 320 { 321 article = m_arr[i]; 322 article.setArticleName(prefix + article.getArticleName()); 323 } 324 } 325 326 private void checkQueryResult(Collection col, PerfArticle[] m_arr) throws Exception 327 { 328 if(col.size() > 0) 329 { 330 Iterator it = col.iterator(); 331 Object obj = it.next(); 332 if(!(obj instanceof PerfArticle)) 333 { 334 throw new Exception ("Wrong object type found. Expected instance of"+ 335 PerfArticle.class.getName() + ", found " + obj.getClass().getName()); 336 } 337 } 338 if (col.size() != m_arr.length) 339 { 340 throw new Exception ("Read objects: Wrong number of objects found. Expected " + 341 (m_arr.length) + ", found " + col.size()); 342 } 343 } 344 345 private void checkInsertResult(PerfArticle[] m_arr) throws Exception 346 { 347 for(int i = 0; i < m_arr.length; i++) 348 { 349 PerfArticle perfArticle = m_arr[i]; 350 if(perfArticle.getArticleId() == null) 351 { 352 throw new Exception ("Insert objects: Object with 'null' PK found"); 353 } 354 } 355 } 356 } 357 | Popular Tags |